Skip to content

Commit

Permalink
remove custom function workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Nov 14, 2023
1 parent e6aa036 commit 888312f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 83 deletions.
16 changes: 0 additions & 16 deletions lib/flop/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,4 @@ defmodule Flop.Adapter do
@callback list(queryable, opts) :: [any]

@callback get_field(any, atom, Flop.FieldInfo.t()) :: any

@doc """
Returns a quoted function to be compiled in the schema protocol
implementation.
Takes the schema options (with the nested adapter options) as argument.
This is a hacky workaround that is necessary because
`Ecto.Query.API.selected_as/1` does not accept variables, which means that we
need to compile a function that builds the `order_by` clause for configured
alias fields.
This callback will be removed as soon as a better solution is found or made
possible.
"""
@callback custom_func_builder(opts) :: Macro.t() when opts: keyword
end
60 changes: 0 additions & 60 deletions lib/flop/adapter/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -499,66 +499,6 @@ defmodule Flop.Adapter.Ecto do
Keyword.merge(default_opts, Keyword.get(opts, :query_opts, []))
end

## Compile time shenanigans

@impl Flop.Adapter
def custom_func_builder(opts) do
adapter_opts = Keyword.fetch!(opts, :adapter_opts)
compound_fields = Map.fetch!(adapter_opts, :compound_fields)
join_fields = Map.fetch!(adapter_opts, :join_fields)
alias_fields = Map.fetch!(adapter_opts, :alias_fields)

compound_field_funcs =
for {name, fields} <- compound_fields do
quote do
def custom(struct, {:apply_order_by, q, {direction, unquote(name)}}) do
Enum.reduce(unquote(fields), q, fn field, acc_q ->
Flop.Schema.custom(
struct,
{:apply_order_by, acc_q, {direction, field}}
)
end)
end
end
end

join_field_funcs =
for {join_field, %{binding: binding, field: field}} <- join_fields do
quote do
def custom(_, {:apply_order_by, q, {direction, unquote(join_field)}}) do
order_by(
q,
[{^unquote(binding), r}],
[{^direction, field(r, unquote(field))}]
)
end
end
end

alias_field_func =
for name <- alias_fields do
quote do
def custom(_, {:apply_order_by, q, {direction, unquote(name)}}) do
order_by(q, [{^direction, selected_as(unquote(name))}])
end
end
end

schema_field_func =
quote do
def custom(_, {:apply_order_by, q, direction}) do
order_by(q, ^direction)
end
end

[
compound_field_funcs,
join_field_funcs,
alias_field_func,
schema_field_func
]
end

## Filter query builder

for op <- [:like_and, :like_or, :ilike_and, :ilike_or] do
Expand Down
7 changes: 0 additions & 7 deletions lib/flop/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,6 @@ defprotocol Flop.Schema do
@doc since: "0.2.0"
@spec max_limit(any) :: pos_integer | nil
def max_limit(data)

@doc false
@spec custom(any, any) :: any
def custom(data, arg)
end

defimpl Flop.Schema, for: Any do
Expand Down Expand Up @@ -926,7 +922,6 @@ defimpl Flop.Schema, for: Any do

field_info_func = build_field_info_func(adapter, adapter_opts, struct)
get_field_func = build_get_field_func(struct, adapter, adapter_opts)
custom_func = adapter.custom_func_builder(options)

quote do
defimpl Flop.Schema, for: unquote(module) do
Expand Down Expand Up @@ -965,8 +960,6 @@ defimpl Flop.Schema, for: Any do
def sortable(_) do
unquote(sortable_fields)
end

unquote(custom_func)
end
end
end
Expand Down

0 comments on commit 888312f

Please sign in to comment.