Current section

Files

Jump to
ecto_context priv templates ecto_context list_for.ex.eex
Raw

priv/templates/ecto_context/list_for.ex.eex

@doc """
Returns all `<%= inspect(schema) %>` records belonging to the given parent association within `scope`.
## Options
* `:preload` - associations to preload
* `:order_by` - field or keyword list passed to `Ecto.Query.order_by/2`
* `:limit` - maximum number of results
* `:select` - list of fields to select
* `:query` - 1-arity function for additional query composition
"""
def list_for(scope, assoc_atom, parent_id, opts \\ []) when is_map(scope) do
EctoContext.Validate.validate_opts!(opts, [:preload, :order_by, :limit, :select, :query])
EctoContext.Validate.validate_assoc!(<%= inspect(schema) %>, assoc_atom)
<%= inspect(schema) %>
|> (<%= inspect(scope) %>).(scope)
|> EctoContext.Query.assoc_where(<%= inspect(schema) %>, assoc_atom, parent_id)
|> EctoContext.Query.maybe_query(opts[:query])
|> EctoContext.Query.maybe_preload(opts[:preload])
|> EctoContext.Query.maybe_order_by(opts[:order_by])
|> EctoContext.Query.maybe_limit(opts[:limit])
|> EctoContext.Query.maybe_select(opts[:select])
|> <%= inspect(repo) %>.all()
end