Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering id type fields #430

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/torch/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ defmodule Torch.Pagination do
defp build_filter_config(model, schema_attrs) do
schema_attrs
|> Enum.reduce(
Map.from_keys(~w(date number text boolean)a, []),
Map.from_keys(~w(date number text boolean id)a, []),
&collect_attributes_by_type(&1, model.__schema__(:type, &1), &2)
)
|> Enum.reduce([], &build_filtrex_configs/2)
Expand All @@ -173,7 +173,7 @@ defmodule Torch.Pagination do
do: collection

defp collect_attributes_by_type(attr, attr_type, collection)
when attr_type in [:integer, :number] do
when attr_type in [:integer, :number, :id] do
Map.update(collection, :number, [attr], fn curr_value -> [attr | curr_value] end)
end

Expand Down
40 changes: 38 additions & 2 deletions test/torch/pagination_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ defmodule NoAttrsMockModel do
end
end

defmodule AssocMockModel do
use Ecto.Schema

schema "assoc_mocks" do
field(:title, :string)
field(:small_mock_id, :integer, references: SmallMockModel)
belongs_to(:mock, MockModel, foreign_key: :mock_id)
end
end

defmodule MockContext do
import Ecto.Query, warn: false

Expand All @@ -61,6 +71,15 @@ defmodule MultiMockContext do
name: :no_mocks
end

defmodule AssocMockContext do
import Ecto.Query, warn: false

use Torch.Pagination,
repo: MockRepo,
model: AssocMockModel,
name: :assoc_mocks
end

defmodule PaginationTest do
use ExUnit.Case

Expand Down Expand Up @@ -88,7 +107,7 @@ defmodule PaginationTest do
assert ~w(body title) == Enum.sort(attr_keys)

:number ->
assert ~w(view_count) == Enum.sort(attr_keys)
assert ~w(id view_count) == Enum.sort(attr_keys)

:boolean ->
assert ~w(archived) == Enum.sort(attr_keys)
Expand All @@ -105,7 +124,7 @@ defmodule PaginationTest do
assert ~w(body title) == Enum.sort(attr_keys)

:number ->
assert [] == attr_keys
assert ~w(id) == attr_keys

:boolean ->
assert [] == attr_keys
Expand All @@ -122,8 +141,25 @@ defmodule PaginationTest do
assert [] == attr_keys

:number ->
assert ~w(id) == attr_keys

:boolean ->
assert [] == attr_keys

:date ->
assert [] == attr_keys
end
end

for %Filtrex.Type.Config{type: t, keys: attr_keys} <-
AssocMockContext.filter_config(:assoc_mocks) do
case t do
:text ->
assert ~w(title) == Enum.sort(attr_keys)

:number ->
assert ~w(mock_id small_mock_id id) == attr_keys

:boolean ->
assert [] == attr_keys

Expand Down
Loading