Skip to content

Commit

Permalink
Support filtering id type fields
Browse files Browse the repository at this point in the history
This will allow using filters such as `filter_assoc/4`.

Ecto uses integer values for the `id` type in a schema.

The `uuid` type in Ecto is a string value, and is not
included in the scope of this PR change.
  • Loading branch information
matsu911 authored and cpjolicoeur committed Sep 28, 2023
1 parent a334cf3 commit 583f15b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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

0 comments on commit 583f15b

Please sign in to comment.