Current section

Files

Jump to
blossom lib blossom schema.ex
Raw

lib/blossom/schema.ex

defmodule Blossom.Schema do
defmacro filter_allowed(bindings, id_table, opts \\ nil) do
binds = var!(bindings)
casks_table = opts[:casks_table] || id_table
allow_nil_casks = opts[:allow_nil_casks] || false
quote do
if unquote(allow_nil_casks) == true do
def allowed_casks(params) do
allowed = Map.get(params, "allowed_casks", [])
dynamic(
unquote(binds),
unquote(casks_table).cask in ^allowed or
is_nil(unquote(casks_table).cask)
)
end
else
def allowed_casks(params) do
allowed = Map.get(params, "allowed_casks", [])
dynamic(unquote(binds), unquote(casks_table).cask in ^allowed)
end
end
def allowed_ids(params) do
allowed = Map.get(params, "allowed_ids", [])
dynamic(unquote(binds), unquote(id_table).id in ^allowed)
end
def filter_by_allowed(
query,
%{"allowed_ids" => _, "allowed_casks" => _} = params
) do
casks = allowed_casks(params)
ids = allowed_ids(params)
where(query, unquote(binds), ^dynamic(unquote(binds), ^casks or ^ids))
end
def filter_by_allowed(query, %{"allowed_casks" => _} = params) do
casks = allowed_casks(params)
where(query, unquote(binds), ^dynamic(unquote(binds), ^casks))
end
def filter_by_allowed(query, %{"allowed_ids" => _} = params) do
ids = allowed_ids(params)
where(query, unquote(binds), ^dynamic(unquote(binds), ^ids))
end
def filter_by_allowed(query, params) do
query
end
end
end
defmacro __using__(opts \\ []) do
quote do
use KilnCore.Schema, unquote(opts)
import Blossom.Schema
import Djinn
end
end
end