View Source CMS behaviour (cms v0.2.1)

TODO

examples

Examples

lookup-a-page-by-path

Lookup a page by path

defmodule MyApp.Page do
  use CMS, lookup_keys: [:path]

  # This is an example of what a document from Sanity CMS might look like.
  @dummy_result %{
    _id: "page-1",
    display_order: 2,
    path: %{
      current: "/"
    }
  }

  @impl true
  def fetch_by([{:path, path}]) do
    # Make an API call to the headless CMS and return document...

    case path do
      "/" -> {:ok, @dummy_result}
      _ -> {:error, :not_found}
    end
  end

  @impl true
  def list do
    # Make an API call to the headless CMS and return documents...
    [
      @dummy_result
      # ...
    ]
  end

  @impl true
  def lookup_key(:path, item), do: item.path.current

  @impl true
  def primary_key(item), do: item._id
end

To look up a page by path:

iex> CMS.get_by!(MyApp.Page, path: "/")
%{_id: "page-1", display_order: 2, path: %{current: "/"}}

list-blog-posts

List blog posts

defmodule MyApp.Post do
  use CMS, list_keys: [:display_order]

  @impl true
  def list do
    # Make an API call to the headless CMS and return document...
    [
      %{
        _id: "post-1",
        display_order: 2
      },
      %{
        _id: "post-2",
        display_order: 1
      }
    ]
  end

  @impl true
  def order_by(:display_order, items), do: Enum.sort_by(items, & &1.display_order)

  @impl true
  def primary_key(item), do: item._id
end

To list pages:

iex> CMS.list_by(MyApp.Post, :display_order)
[%{_id: "post-2", display_order: 1}, %{_id: "post-1", display_order: 2}]

Link to this section Summary

Callbacks

Fetches a single CMS document given one or more keys. Generally implementations of this callback will make an API call to the headless CMS. Returns {:ok, doc} or {:error, :not_found}. This callback is optional and is only needed if you intend to call CMS.get_by/2 or CMS.get_by!/2 without having initialized the cache.

Returns a list of all CMS documents. Generally implementations of this callback will make an API call to the headless CMS.

Returns the lookup key for a document given a key name and a CMS document. Only required if you will be looking up documents by key. See CMS.get_by/2 and CMS.get_by!/2.

Orders documents by list key. Only required if you will be listing documents using CMS.list_by/3.

Returns the primary key of the given CMS document.

Link to this section Callbacks

@callback fetch_by(Keyword.t()) :: {:ok, map()} | {:error, :not_found}

Fetches a single CMS document given one or more keys. Generally implementations of this callback will make an API call to the headless CMS. Returns {:ok, doc} or {:error, :not_found}. This callback is optional and is only needed if you intend to call CMS.get_by/2 or CMS.get_by!/2 without having initialized the cache.

@callback list() :: [map()]

Returns a list of all CMS documents. Generally implementations of this callback will make an API call to the headless CMS.

Link to this callback

lookup_key(atom, map)

View Source (optional)
@callback lookup_key(atom(), map()) :: any()

Returns the lookup key for a document given a key name and a CMS document. Only required if you will be looking up documents by key. See CMS.get_by/2 and CMS.get_by!/2.

Link to this callback

order_by(atom, list)

View Source (optional)
@callback order_by(atom(), [map()]) :: [any()]

Orders documents by list key. Only required if you will be listing documents using CMS.list_by/3.

@callback primary_key(map()) :: atom()

Returns the primary key of the given CMS document.

Link to this section Functions

TODO

TODO

Link to this function

list_by(mod, name, opts \\ [])

View Source

TODO docs

options

Options

  • :range

TODO docs

options

Options

  • :update_all_nodes - If true then update will be sent to all Erlang nodes in cluster. The default value is false.