View Source Kaffy.ResourceAdmin (Kaffy v0.9.3)
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.
Link to this section 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.
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.
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
fields to search against when typing in the search box.
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.
Link to this section Functions
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
Examples
def authorized?(_schema, _conn) do
true
end
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
Examples
def create_changeset(schema, attrs) do
MyApp.Blog.Post.create_changeset(schema, attrs)
end
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
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
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
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
ordering/1
takes a schema and returns how the entries should be ordered.
If ordering/1
is not defined, Kaffy will return [desc: :id]
.
examples
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
Examples
def plural_name(_schema) do
"Categories"
end
search_fields/1
takes a schema and must return a list of :string
fields to search against when typing in the search box.
If search_fields/1
is not defined, Kaffy will return all the :string
fields of the schema.
examples
Examples
def search_fields(_schema) do
[:title, :slug, :body]
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
Examples
def singular_name(_schema) do
"Article"
end
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
Examples
def update_changeset(schema, attrs) do
MyApp.Blog.Post.create_changeset(schema, attrs)
end