Skip to content

Instantly share code, notes, and snippets.

@smpallen99
Last active May 15, 2017 15:39
Show Gist options
  • Save smpallen99/25bbba6241d69764d1a1421bc0d048c3 to your computer and use it in GitHub Desktop.
Save smpallen99/25bbba6241d69764d1a1421bc0d048c3 to your computer and use it in GitHub Desktop.
Example of ExAdminRedux resource file
# ex_admin/lib/ex_admin/resource.ex
# Here is the prototype of the resoruce.ex library in ex_admin.
defmodule ExAdmin.Resource do
defmacro __using__(opts) do
schema = opts[:schema]
unless schema do
raise ":schema is required"
end
schema_adapter = opts[:adapter] || Application.get_env(:ex_admin, :schema_adapter)
unless schema_adapter do
raise "schema_adapter required"
end
quote do
@__module__ unquote(schema)
@__adapter__ unquote(schema_adapter)
def index_columns do
@__module__.__schema__(:fields) -- ~w(id inserted_at updated_at)a
end
def card_title do
"#{@__module__}s"
end
def tool_bar do
"Listing of #{card_title()}"
end
def route_name do
Module.split(@__module__) |> to_string |> String.downcase |> Inflex.Pluralize.pluralize
end
def schema, do: @__module__
def adapter, do: @__adapter__
defoverridable [index_columns: 0, card_title: 0, tool_bar: 0, route_name: 0, adapter: 0]
end
end
end
# lib/new_admin/admin/user.ex
defmodule NewAdmin.ExAdmin.User do
use ExAdmin.Resource, schema: NewAdmin.User
# out of the box, nothing else is required in this file unless you
# want to customize the resource.
# we are customizing the fields that will be displayed on the index page
# by default, all fields are displayed except [:id, :inserted_at, :updated_at]
# this adds :id and remotes :active, :birthdate, :height
def index_columns do
[:id | super()] -- [:active, :birthdate, :height]
end
# we are overriding the default card title function
def card_title do
# "#{@__module__}s" # the default
"User Accounts"
end
end
@smpallen99
Copy link
Author

Further notes on what [:id | super()] -- [:active, :birthdate, :height] does...

The API in ExAdmin.Resource defines a default implementation and overrides the following functions:

  defoverridable [index_columns: 0, card_title: 0, tool_bar: 0, route_name: 0, adapter: 0]

Calling super() brings down the default implementation which includes all fields in the schema except [:id, :inserted_at, :updated_at]. So, I'm adding the :idfield, and removing the [:active, :birthdate, :height]` fields which are part of the User schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment