From 2fa8ddbf637d6cb23472bc32682bb54a322fb91a Mon Sep 17 00:00:00 2001 From: Shigeaki Matsumura Date: Wed, 27 Sep 2023 18:18:41 +0900 Subject: [PATCH 1/2] Support filtering :id type fields --- lib/torch/pagination.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/torch/pagination.ex b/lib/torch/pagination.ex index 32ce1829..ad6be76f 100644 --- a/lib/torch/pagination.ex +++ b/lib/torch/pagination.ex @@ -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) @@ -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 From 8ae45a6ab80c397d8233d12e4e96d420f1c7f663 Mon Sep 17 00:00:00 2001 From: Shigeaki Matsumura Date: Wed, 27 Sep 2023 22:53:48 +0900 Subject: [PATCH 2/2] Fix test cases --- test/torch/pagination_test.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/torch/pagination_test.exs b/test/torch/pagination_test.exs index e7350dca..fbbe3b26 100644 --- a/test/torch/pagination_test.exs +++ b/test/torch/pagination_test.exs @@ -88,7 +88,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) @@ -105,7 +105,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 @@ -122,7 +122,7 @@ defmodule PaginationTest do assert [] == attr_keys :number -> - assert [] == attr_keys + assert ~w(id) == attr_keys :boolean -> assert [] == attr_keys