View Source Kaffy.ResourceAdmin (Kaffy v0.10.2)

ResourceAdmin modules should be created for every schema you want to customize/configure in Kaffy.

If you have a schema like MyApp.Products.Product, you should create an admin module with name MyApp.Products.ProductAdmin and add functions documented in this module to customize the behavior.

All functions are optional.

Summary

Functions

authorized?/2 takes the schema and the current Plug.Conn struct and should return a boolean value.

create_changeset/2 takes the record and the changes and should return a changeset for creating a new record.

default_actions/1 takes a schema and returns the default actions for the schema.

deserialize_id/2 takes a schema and serialized id and must return a complete keyword list in the form of [{:primary_key, value}, ...].

form_fields/1 takes a schema and returns a keyword list of fields and their options for the new/edit form.

index/1 takes the schema module and should return a keyword list of fields and their options.

Allows a description to be injected into the index view of a resource's index view. This is helpful when you want to provide information to the user about the index page. Return value can be a string, or {:safe, string) tuple. If :safe tuple is used, HTML will be rendered.

ordering/1 takes a schema and returns how the entries should be ordered.

This is useful for names that cannot be plural by adding an "s" at the end.

search_fields/1 takes a schema and must return a list of :string, ':id,:integer, and:decimalfields to search against when typing in the search box. Ifsearch_fields/1` is not defined, Kaffy will return all the fields of the schema with the supported types mentioned earlier. ## Examples elixir def search_fields(_schema) do [:id, :title, :slug, :body, :price, :quantity] end

serialize_id/2 takes a schema and record and must return a string to be used in the URL and form values.

This function should return a string for the singular name of a resource.

update_changeset/2 takes the record and the changes and should return a changeset for updating an existing record.

Functions

Link to this function

authorized?(resource, conn)

View Source

authorized?/2 takes the schema and the current Plug.Conn struct and should return a boolean value.

Returning false will prevent the access of this resource for the current user/request.

If authorized?/2 is not defined, Kaffy will return true.

Examples

def authorized?(_schema, _conn) do
  true
end
Link to this function

collect_links(conn, location)

View Source
Link to this function

collect_widgets(conn, context \\ :kaffy_dashboard)

View Source
Link to this function

create_changeset(resource, changes)

View Source

create_changeset/2 takes the record and the changes and should return a changeset for creating a new record.

If create_changeset/2 is not defined, Kaffy will try to call schema.changeset/2

and if that's not defined, Ecto.Changeset.change/2 will be called.

Examples

def create_changeset(schema, attrs) do
  MyApp.Blog.Post.create_changeset(schema, attrs)
end
Link to this function

custom_index_query(conn, resource, query)

View Source
Link to this function

custom_links(resource, location \\ nil)

View Source
Link to this function

custom_pages(resource, conn)

View Source
Link to this function

custom_show_query(conn, resource, query)

View Source
Link to this function

default_actions(resource)

View Source

default_actions/1 takes a schema and returns the default actions for the schema.

If default_actions/1 is not defined, Kaffy will return [:new, :edit, :delete].

Example:

def default_actions(_schema) do
  [:new, :delete]
end
Link to this function

deserialize_id(resource, id)

View Source

deserialize_id/2 takes a schema and serialized id and must return a complete keyword list in the form of [{:primary_key, value}, ...].

If deserialize_id/2 is not defined, Kaffy will split multiple primary keys with ":" as a separator.

Examples:

# Default method with fixed keys
def deserialize_id(_schema, serialized_id) do
  Enum.zip([:post_id, :tag_id], String.split(serialized_id, ":"))
end

# Deserialize from ETF
def deserialize_id(_schema, serialized_id) do
  {product_id, tag_id} = serialized_id
  |> Base.url_decode64!()
  |> :erlang.binary_to_term()

  [product_id: product_id, tag_id: tag_id]
end

form_fields/1 takes a schema and returns a keyword list of fields and their options for the new/edit form.

Supported options are:

:label, :type, :choices, and :permission

:type can be any ecto type in addition to :file and :textarea

If :choices is provided, it must be a keyword list and the field will be rendered as a <select> element regardless of the actual field type.

Setting :permission to :read will make the field non-editable. It is :write by default.

If you want to remove a field from being rendered, just remove it from the list.

If form_fields/1 is not defined, Kaffy will return all the fields with their default types based on the schema.

Examples

def form_fields(_schema) do
  [
    title: %{label: "Subject"},
    slug: nil,
    image: %{type: :file},
    status: %{choices: [{"Pending", "pending"}, {"Published", "published"}]},
    body: %{type: :textarea, rows: 3},
    views: %{permission: :read}
  ]
end

index/1 takes the schema module and should return a keyword list of fields and their options.

Supported options are :name and :value.

Both options can be a string or an anonymous function.

If a function is provided, the current entry is passed to it.

If index/1 is not defined, Kaffy will return all the fields of the schema and their default values.

Examples

def index(_schema) do
  [
    id: %{name: "ID", value: fn post -> post.id + 100 end},
    title: nil, # this will render the default name for this field (Title) and its default value (post.title)
    views: %{name: "Hits", value: fn post -> post.views + 10 end},
    published: %{name: "Published?", value: fn post -> published?(post) end},
    comment_count: %{name: "Comments", value: fn post -> comment_count(post) end}
  ]
end
Link to this function

index_description(resource)

View Source

Allows a description to be injected into the index view of a resource's index view. This is helpful when you want to provide information to the user about the index page. Return value can be a string, or {:safe, string) tuple. If :safe tuple is used, HTML will be rendered.

index_description/1 takes a schema and must return a String.t() or tuple {:safe, String.t()}.

If index_description/1 is not defined, Kaffy will return nil.

Examples:

def index_description(resource) do
  "This will show up on the index page."
end

def index_description(resource) do
  {:safe, "This will show up on the index page.  <b>This will be bold!</b>."}
end
Link to this function

list_actions(resource, conn)

View Source

ordering/1 takes a schema and returns how the entries should be ordered.

If ordering/1 is not defined, Kaffy will return [desc: primary_key], or the first field of the primary key, if it's a composite.

Examples

def ordering(_schema) do
  [asc: :title]
end

This is useful for names that cannot be plural by adding an "s" at the end.

Like "Category" => "Categories" or "Person" => "People".

If plural_name/1 is not defined, Kaffy will use the singular name and add an "s" to it (e.g. Posts).

Examples

def plural_name(_schema) do
  "Categories"
end
Link to this function

resource_actions(resource, conn)

View Source

search_fields/1 takes a schema and must return a list of :string, ':id,:integer, and:decimalfields to search against when typing in the search box. Ifsearch_fields/1` is not defined, Kaffy will return all the fields of the schema with the supported types mentioned earlier. ## Examples elixir def search_fields(_schema) do [:id, :title, :slug, :body, :price, :quantity] end

Link to this function

serialize_id(resource, entry)

View Source

serialize_id/2 takes a schema and record and must return a string to be used in the URL and form values.

If serialize_id/2 is not defined, Kaffy will concatenate multiple primary keys with ":" as a separator.

Examples:

# Default method with fixed keys
def serialize_id(_schema, record) do
  Enum.join([record.post_id, record.tag_id], ":")
end

# ETF
def serialize_id(_schema, record) do
  {record.post_id, record.tag_id}
  |> :erlang.term_to_binary()
  |> Base.url_encode64()
end

This function should return a string for the singular name of a resource.

If singular_name/1 is not defined, Kaffy will use the name of the last part of the schema module (e.g. Post in MyApp.Blog.Post)

This is useful for when you have a schema but you want to display its name differently.

If you have "Post" and you want to display "Article" for example.

Examples

def singular_name(_schema) do
  "Article"
end
Link to this function

update_changeset(resource, entry, changes)

View Source

update_changeset/2 takes the record and the changes and should return a changeset for updating an existing record.

If update_changeset/2 is not defined, Kaffy will try to call schema.changeset/2

and if that's not defined, Ecto.Changeset.change/2 will be called.

Examples

def update_changeset(schema, attrs) do
  MyApp.Blog.Post.create_changeset(schema, attrs)
end