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

Fix credo and dialyzer indications [#19] #20

Merged
merged 3 commits into from
Jul 30, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jobs:
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres';"
- name: Install dependencies
run: mix deps.get
- name: Create DB
run: mix do ecto.create, ecto.migrate
- name: Dialyzer
run: MIX_ENV=test mix dialyzer
- name: Credo
Expand Down
2 changes: 1 addition & 1 deletion lib/permit_ecto/operators/is_nil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Permit.Operators.IsNil.DynamicQuery do
@moduledoc false
import Ecto.Query, only: [dynamic: 2]

@spec dynamic_query_fn(term(), keyword()) :: (any() -> Ecto.Query.dynamic()) | nil
@spec dynamic_query_fn(term(), keyword()) :: (any() -> Ecto.Query.dynamic_expr()) | nil
def dynamic_query_fn(key, not?) do
if not? do
fn _ -> dynamic([r], not is_nil(field(r, ^key))) end
Expand Down
2 changes: 1 addition & 1 deletion lib/permit_ecto/permissions/conjunction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Permit.Ecto.Permissions.Conjunction do
Types.subject(),
Ecto.Query.t()
) ::
{:ok, Ecto.Query.dynamic()} | {:error, keyword()}
{:ok, Ecto.Query.dynamic_expr()} | {:error, keyword()}
def to_dynamic_query_expr(%ParsedConditionList{conditions: []}, _, _, _query),
do: {:ok, dynamic(false)}

Expand Down
26 changes: 17 additions & 9 deletions lib/permit_ecto/permissions/parsed_condition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Permit.Ecto.Permissions.ParsedCondition do
Types.subject(),
Ecto.Query.t()
) ::
{:ok, Ecto.Query.dynamic()} | {:error, term()}
{:ok, Ecto.Query.dynamic_expr()} | {:error, term()}

def to_dynamic_query(
%ParsedCondition{
Expand Down Expand Up @@ -100,15 +100,23 @@ defmodule Permit.Ecto.Permissions.ParsedCondition do
do: query_fn.(resource)

defp build_dynamic_query({root, conditions}, query) do
Enum.reduce(conditions, dynamic(true), fn {field, value}, acc ->
if Keyword.keyword?(value) do
Enum.reduce(value, acc, fn {k, v}, acc ->
add_condition(root, field, {k, v}, acc, query)
end)
else
add_single_condition(root, field, value, acc, query)
Enum.reduce(
conditions,
dynamic(true),
fn {field, value}, acc ->
add_conditions(root, field, value, acc, query)
end
end)
)
end

defp add_conditions(root, field, keyword_or_value, acc, query) do
if Keyword.keyword?(keyword_or_value) do
Enum.reduce(keyword_or_value, acc, fn {k, v}, acc ->
add_condition(root, field, {k, v}, acc, query)
end)
else
add_single_condition(root, field, keyword_or_value, acc, query)
end
end

defp add_condition(root, field, {key, v}, acc, query) when is_list(v) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Permit.EctoFakeApp.Repo.Migrations.CreateItemTable do
defmodule Permit.EctoFakeApp.Repo.Migrations.CreateItemMetadataTable do
use Ecto.Migration

def change do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Permit.EctoFakeApp.Repo.Migrations.CreateItemTable do
defmodule Permit.EctoFakeApp.Repo.Migrations.CreateReviewsTable do
use Ecto.Migration

def change do
Expand Down
Loading