diff --git a/lib/flop/filter.ex b/lib/flop/filter.ex index 85310b2..e7e7bc9 100644 --- a/lib/flop/filter.ex +++ b/lib/flop/filter.ex @@ -368,6 +368,22 @@ defmodule Flop.Filter do ] end + # for backward compatibility with Ecto < 3.12.0 + defp get_allowed_operators({:parameterized, Ecto.Enum, _}) do + [ + :==, + :!=, + :empty, + :not_empty, + :<=, + :<, + :>=, + :>, + :in, + :not_in + ] + end + defp get_allowed_operators(_) do [ :==, diff --git a/test/flop/filter_test.exs b/test/flop/filter_test.exs index def4814..ce7c087 100644 --- a/test/flop/filter_test.exs +++ b/test/flop/filter_test.exs @@ -52,6 +52,72 @@ defmodule Flop.FilterTest do end end + test "returns list of operators for enum" do + types = [ + # by internal representation Ecto < 3.12.0 + {:parameterized, Ecto.Enum, + %{ + on_load: %{ + "happy" => :happy, + "relaxed" => :relaxed, + "playful" => :playful + }, + type: :string, + embed_as: :self, + on_cast: %{ + "happy" => :happy, + "relaxed" => :relaxed, + "playful" => :playful + }, + on_dump: %{happy: "happy", relaxed: "relaxed", playful: "playful"}, + mappings: [happy: "one", relaxed: "relaxed", playful: "playful"] + }}, + # by internal representation Ecto >= 3.12.0 + {:parameterized, + {Ecto.Enum, + %{ + on_load: %{ + "happy" => :happy, + "relaxed" => :relaxed, + "playful" => :playful + }, + type: :string, + embed_as: :self, + on_cast: %{ + "happy" => :happy, + "relaxed" => :relaxed, + "playful" => :playful + }, + on_dump: %{happy: "happy", relaxed: "relaxed", playful: "playful"}, + mappings: [happy: "one", relaxed: "relaxed", playful: "playful"] + }}}, + # by convenience format + {:ecto_enum, [:one, :two]}, + # by reference + {:from_schema, MyApp.Pet, :mood} + ] + + expected_ops = [ + :==, + :!=, + :empty, + :not_empty, + :<=, + :<, + :>=, + :>, + :in, + :not_in + ] + + for type <- types do + assert Filter.allowed_operators(type) == expected_ops + + assert Filter.allowed_operators(%Flop.FieldInfo{ecto_type: type}) == + expected_ops + end + end + test "returns a list of operators for unknown types" do assert [op | _] = Filter.allowed_operators(:unicorn) assert is_atom(op)