Skip to content

Commit

Permalink
Improvements to schema:
Browse files Browse the repository at this point in the history
- Make __rummage_defaults__ public
- Add scope through Map instead of Tuple
  • Loading branch information
thebugcatcher committed Oct 8, 2018
1 parent 9cdf956 commit 2943e1d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/rummage_ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ defmodule Rummage.Ecto do
alias Rummage.Ecto.Config, as: RConfig

def rummage(rummage, opts \\ []) do
Rummage.Ecto.rummage(__MODULE__, rummage, uniq_merge(opts, defaults()))
Rummage.Ecto.rummage(__MODULE__, rummage, uniq_merge(opts, __rummage_defaults__()))
end

def rummageq(queryable, rummage, opts \\ []) do
Rummage.Ecto.rummage(queryable, rummage, uniq_merge(opts, defaults()))
Rummage.Ecto.rummage(queryable, rummage, uniq_merge(opts, __rummage_defaults__()))
end

defp defaults() do
def __rummage_defaults__ do
keys = ~w{repo per_page search sort paginate}a
Enum.map(keys, &get_defs/1)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/rummage_ecto/hooks/custom_sort.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ defmodule Rummage.Ecto.Hook.CustomSort do

# Helper function which handles addition of paginated query on top of
# the sent queryable variable
defp handle_sort(queryable, {field, order}) do
defp handle_sort(queryable, %{scope: scope, order: order}) do
module = get_module(queryable)
name = :"__rummage_custom_sort_#{field}"
name = :"__rummage_custom_sort_#{scope}"

case function_exported?(module, name, 1) do
true -> apply(module, name, [{queryable, order}])
_ -> "No scope `#{field}` of type custom_sort defined in #{module}"
_ -> "No scope `#{scope}` of type custom_sort defined in #{module}"
end
end

Expand All @@ -34,8 +34,8 @@ defmodule Rummage.Ecto.Hook.CustomSort do
iex> Sort.format_params(Parent, %{}, [])
%{assoc: [], order: :asc}
"""
@spec format_params(Ecto.Query.t(), map() | tuple(), keyword()) :: map()
def format_params(queryable, {sort_scope, order}, opts) do
@spec format_params(Ecto.Query.t(), map(), keyword()) :: map()
def format_params(queryable, %{scope: sort_scope, order: order}, opts) do
module = get_module(queryable)
name = :"__rummage_sort_#{sort_scope}"

Expand All @@ -44,7 +44,7 @@ defmodule Rummage.Ecto.Hook.CustomSort do
format_params(queryable, sort_params, opts)
_ ->
case function_exported?(module, :"__rummage_custom_sort_#{sort_scope}", 1) do
true -> {sort_scope, order}
true -> %{scope: sort_scope, order: order}
_ -> raise "No scope `#{sort_scope}` of type custom_sort defined in the #{module}"
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rummage_ecto/hooks/paginate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ defmodule Rummage.Ecto.Hook.Paginate do
|> apply(:all, [distinct(query, :true)])
|> Enum.count()
end

defp get_count(query, repo, pk) do
query = select(query, [s], count(field(s, ^pk), :distinct))
hd(apply(repo, :all, [query]))
Expand Down
4 changes: 2 additions & 2 deletions lib/rummage_ecto/hooks/sort.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ defmodule Rummage.Ecto.Hook.Sort do
iex> Sort.format_params(Parent, %{}, [])
%{assoc: [], order: :asc}
"""
@spec format_params(Ecto.Query.t(), map() | tuple(), keyword()) :: map()
def format_params(queryable, {sort_scope, order}, opts) do
@spec format_params(Ecto.Query.t(), map(), keyword()) :: map()
def format_params(queryable, %{scope: sort_scope, order: order}, opts) do
module = get_module(queryable)
name = :"__rummage_sort_#{sort_scope}"
sort_params = case function_exported?(module, name, 1) do
Expand Down
8 changes: 4 additions & 4 deletions test/rummage_ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ defmodule Rummage.EctoTest do
test "rummage call with sort scope" do
create_categories_and_products()

rummage = %{sort: {:category_name, :asc}}
rummage = %{sort: %{scope: :category_name, order: :asc}}

{queryable, rummage} = Product.rummage(rummage)

Expand All @@ -413,7 +413,7 @@ defmodule Rummage.EctoTest do
"Product 2->3", "Product 1->3",
"Product 2->4", "Product 1->4"]
)
rummage = %{sort: {:invalid_scope, :asc}}
rummage = %{sort: %{scope: :invalid_scope, order: :asc}}

assert_raise RuntimeError, ~r/No scope `invalid_scope`/, fn ->
Product.rummage(rummage)
Expand Down Expand Up @@ -467,7 +467,7 @@ defmodule Rummage.EctoTest do
test "rummage call with custom sort scope" do
create_categories_and_products()

rummage = %{sort: {:category_microseconds, :desc}}
rummage = %{sort: %{scope: :category_microseconds, order: :desc}}

{queryable, rummage} = Product.rummage(rummage)

Expand All @@ -483,7 +483,7 @@ defmodule Rummage.EctoTest do
"Product 2->4", "Product 1->4"]
)

rummage = %{sort: {:category_milliseconds, :desc}}
rummage = %{sort: %{scope: :category_milliseconds, order: :desc}}

assert_raise RuntimeError, ~r/No scope `category_milliseconds`/, fn ->
Product.rummage(rummage)
Expand Down

0 comments on commit 2943e1d

Please sign in to comment.