Rummage.Ecto v2.0.0 Rummage.Ecto.Schema View Source
This module is meant to be use
d by a module (typically an Ecto.Schema
).
This isn't a required module for using Rummage
, but it allows us to extend
its functionality.
Link to this section Summary
Functions
This macro allows us to leverage features in Rummage.Ecto.Schema
. It takes
advantage of Ecto
, rummage_field
and rummage_scope
Rummage Field is a way to define a field which can be used to search, sort,
paginate through. This field might not exist in the database or the schema,
but can be represented as a fragments
query using multiple fields.
Link to this section Functions
This macro allows us to leverage features in Rummage.Ecto.Schema
. It takes
advantage of Ecto
, rummage_field
and rummage_scope
Usage:
defmodule MySchema do
use Rummage.Ecto.Schema
schema "my_table" do
field :field1, :integer
field :field2, :integer
timestamps()
end
rummage_field :field1_or_field2 do
{:fragment, "coalesce(?, ?)", :name, :description}
end
rummage_scope :show_page, [type: :paginate], fn(page) ->
%{per_page: 10, page: page}
end
end
Rummage Field is a way to define a field which can be used to search, sort,
paginate through. This field might not exist in the database or the schema,
but can be represented as a fragments
query using multiple fields.
NOTE: Currently this feature has some limitations due to limitations put on
Ecto's fragments. Ecto 3.0 is expected to come out with unsafe_fragment
,
which will give this feature great flexibility. This feature is also quite
dependent on what database engine is being used. For now, we have made
a few fragments available (the list can be seen here) which are thoroughly
tested on postgres. If these fragments don't do it, you can use rummage_scope
to accomplish a similar functionality.
Usage:
To use upper case name as rummage field:
rummage_field :upper_case_name do
{:fragment, "upper(?)", :name}
end
To use the hour for created_at as rummage field:
rummage_field :created_at_hour do
{:fragment, "date_part('hour', ?)", :inserted_at}
end