Skip to content

Commit

Permalink
Change docs and tests to use new style parameterized types
Browse files Browse the repository at this point in the history
  • Loading branch information
martosaur committed Aug 18, 2024
1 parent 051526e commit 6280423
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/flop/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ defmodule Flop.Filter do
end

defp expand_type({:ecto_enum, values}) do
{:parameterized, Ecto.Enum, Ecto.Enum.init(values: values)}
Ecto.ParameterizedType.init(Ecto.Enum, values: values)

Check warning on line 198 in lib/flop/filter.ex

View check run for this annotation

Codecov / codecov/patch

lib/flop/filter.ex#L198

Added line #L198 was not covered by tests
end

defp expand_type(type), do: type
Expand Down Expand Up @@ -348,7 +348,7 @@ defmodule Flop.Filter do
[:==, :!=, :empty, :not_empty, :<=, :<, :>=, :>, :in, :not_in]
end

def allowed_operators({:parameterized, Ecto.Enum, _}) do
def allowed_operators({:parameterized, {Ecto.Enum, _}}) do
[
:==,
:!=,
Expand Down
4 changes: 2 additions & 2 deletions lib/flop/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ defprotocol Flop.Schema do
For parameterized types, use the following syntax:
- `ecto_type: {:parameterized, Ecto.Enum, Ecto.Enum.init(values: [:one, :two])}`
- `ecto_type: Ecto.ParameterizedType.init(Ecto.Enum, values: [:one, :two])`
If you're working with `Ecto.Enum` types, you can use a more convenient
syntax:
Expand Down Expand Up @@ -613,7 +613,7 @@ defprotocol Flop.Schema do
- `:string`
- `:integer`
- `Ecto.UUID`
- `{:parameterized, Ecto.Enum, Ecto.Enum.init(values: [:one, :two])}`
- `{:parameterized, {Ecto.Enum, Ecto.Enum.init(values: [:one, :two])}}`
Or reference a schema field:
Expand Down
2 changes: 1 addition & 1 deletion test/flop/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Flop.FilterTest do
:naive_datetime_usec,
:utc_datetime,
:utc_datetime_usec,
{:parameterized, Ecto.Enum, type: :string}
{:parameterized, {Ecto.Enum, %{type: :string}}}
]

for type <- types do
Expand Down
18 changes: 18 additions & 0 deletions test/flop/validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,5 +1050,23 @@ defmodule Flop.ValidationTest do
assert {:error, changeset} = validate(params, for: Owner)
assert [%{value: ["is invalid"]}] = errors_on(changeset)[:filters]
end

test "casts filter values as ecto enums when using parameterized type" do
field = :pet_mood_as_parameterized_type

params = %{filters: [%{field: field, op: :==, value: "happy"}]}
assert %{filters: [%{value: :happy}]} = validate!(params, for: Owner)

params = %{filters: [%{field: field, op: :==, value: :happy}]}
assert %{filters: [%{value: :happy}]} = validate!(params, for: Owner)

params = %{filters: [%{field: field, op: :==, value: "joyful"}]}
assert {:error, changeset} = validate(params, for: Owner)
assert [%{value: ["is invalid"]}] = errors_on(changeset)[:filters]

params = %{filters: [%{field: field, op: :==, value: :joyful}]}
assert {:error, changeset} = validate(params, for: Owner)
assert [%{value: ["is invalid"]}] = errors_on(changeset)[:filters]
end
end
end
13 changes: 12 additions & 1 deletion test/support/owner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ defmodule MyApp.Owner do

@derive {
Flop.Schema,
filterable: [:name, :pet_mood_as_reference, :pet_mood_as_enum],
filterable: [
:name,
:pet_mood_as_reference,
:pet_mood_as_enum,
:pet_mood_as_parameterized_type
],
sortable: [:name, :age],
join_fields: [
pet_age: [
Expand All @@ -24,6 +29,12 @@ defmodule MyApp.Owner do
binding: :pets,
field: :mood,
ecto_type: {:ecto_enum, [:happy, :playful]}
],
pet_mood_as_parameterized_type: [
binding: :pets,
field: :mood,
ecto_type:
Ecto.ParameterizedType.init(Ecto.Enum, values: [:happy, :playful])
]
],
compound_fields: [age_and_pet_age: [:age, :pet_age]],
Expand Down

0 comments on commit 6280423

Please sign in to comment.