Skip to content

Commit

Permalink
fix: don't reorder global validations/changes
Browse files Browse the repository at this point in the history
fix: use latest spark, and new persisters callback
fix: properly validate belongs_to relationships
  • Loading branch information
zachdaniel committed Oct 25, 2023
1 parent 539f5a1 commit e680867
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 89 deletions.
4 changes: 2 additions & 2 deletions documentation/dsls/DSL:-Ash.Flow.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(module) | module</code>
<code class="inline">module | list(module)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -2470,7 +2470,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(list(atom) | atom)</code>
<code class="inline">list(atom | list(atom))</code>
</td>
<td style="text-align: left">

Expand Down
4 changes: 2 additions & 2 deletions documentation/dsls/DSL:-Ash.Policy.Authorizer.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ A field policy that, if passed, will skip all following field policies for that

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -1807,7 +1807,7 @@ for more.

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down
36 changes: 18 additions & 18 deletions documentation/dsls/DSL:-Ash.Resource.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -5235,7 +5235,7 @@ validate changing(:email)

</td>
<td style="text-align: left">
<code class="inline">list((any -> any) | module) | (any -> any) | module</code>
<code class="inline">(any -> any) | module | list((any -> any) | module)</code>
</td>
<td style="text-align: left">
<code class="inline">[]</code>
Expand Down Expand Up @@ -5915,7 +5915,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -7187,7 +7187,7 @@ validate changing(:email)

</td>
<td style="text-align: left">
<code class="inline">list((any -> any) | module) | (any -> any) | module</code>
<code class="inline">(any -> any) | module | list((any -> any) | module)</code>
</td>
<td style="text-align: left">
<code class="inline">[]</code>
Expand Down Expand Up @@ -8268,7 +8268,7 @@ validate changing(:email)

</td>
<td style="text-align: left">
<code class="inline">list((any -> any) | module) | (any -> any) | module</code>
<code class="inline">(any -> any) | module | list((any -> any) | module)</code>
</td>
<td style="text-align: left">
<code class="inline">[]</code>
Expand Down Expand Up @@ -9001,7 +9001,7 @@ define :get_user_by_id, action: :get_by_id, args: [:id], get?: true

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -9458,7 +9458,7 @@ identity :full_name, [:first_name, :last_name]

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -9682,7 +9682,7 @@ change {MyCustomChange, :foo}

</td>
<td style="text-align: left">
<code class="inline">list(:create | :update | :destroy) | :create | :update | :destroy</code>
<code class="inline">:create | :update | :destroy | list(:create | :update | :destroy)</code>
</td>
<td style="text-align: left">
<code class="inline">[:create, :update]</code>
Expand Down Expand Up @@ -9958,7 +9958,7 @@ validate at_least_one_of_present([:first_name, :last_name])

</td>
<td style="text-align: left">
<code class="inline">list((any -> any) | module) | (any -> any) | module</code>
<code class="inline">(any -> any) | module | list((any -> any) | module)</code>
</td>
<td style="text-align: left">
<code class="inline">[]</code>
Expand All @@ -9979,7 +9979,7 @@ validate at_least_one_of_present([:first_name, :last_name])

</td>
<td style="text-align: left">
<code class="inline">list(:create | :update | :destroy) | :create | :update | :destroy</code>
<code class="inline">:create | :update | :destroy | list(:create | :update | :destroy)</code>
</td>
<td style="text-align: left">
<code class="inline">[:create, :update]</code>
Expand Down Expand Up @@ -10187,7 +10187,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -10492,7 +10492,7 @@ exists :has_ticket, :assigned_tickets

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -10761,7 +10761,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -11068,7 +11068,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -11356,7 +11356,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -11683,7 +11683,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -11970,7 +11970,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -12257,7 +12257,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down Expand Up @@ -12546,7 +12546,7 @@ end

</td>
<td style="text-align: left">
<code class="inline">list(atom) | atom</code>
<code class="inline">atom | list(atom)</code>
</td>
<td style="text-align: left">

Expand Down
3 changes: 1 addition & 2 deletions lib/ash/actions/create/create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ defmodule Ash.Actions.Create do

{changeset, _} =
Ash.Actions.ManagedRelationships.validate_required_belongs_to(
{changeset, []},
false
{changeset, []}
)

changeset
Expand Down
25 changes: 1 addition & 24 deletions lib/ash/actions/managed_relationships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@ defmodule Ash.Actions.ManagedRelationships do
end)
end

def setup_managed_belongs_to_relationships(%{relationships: relationships} = changeset, _, _)
when relationships in [%{}, nil] do
{changeset, %{notifications: []}}
|> validate_required_belongs_to()
|> case do
{:error, error} ->
{:error, error}

{changeset, instructions} ->
changeset =
Map.update!(changeset, :relationships, fn relationships ->
Map.new(relationships, fn {rel, inputs} ->
{rel,
Enum.map(inputs, fn {input, config} ->
{input, Keyword.put(config, :handled?, true)}
end)}
end)
end)

{changeset, instructions}
end
end

def setup_managed_belongs_to_relationships(changeset, actor, engine_opts) do
changeset.relationships
|> Enum.map(fn {relationship, val} ->
Expand Down Expand Up @@ -318,7 +295,7 @@ defmodule Ash.Actions.ManagedRelationships do
{:halt, {:error, error}}
end
end)
|> validate_required_belongs_to()
|> validate_required_belongs_to(false)
|> case do
{:error, error} ->
{:error, error}
Expand Down
7 changes: 7 additions & 0 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ defmodule Ash.Changeset do
def prepare_changeset_for_action(changeset, action, opts) do
changeset
|> Map.put(:action, action)
|> reset_arguments()
|> handle_errors(action.error_handler)
|> set_actor(opts)
|> set_authorize(opts)
Expand All @@ -895,6 +896,12 @@ defmodule Ash.Changeset do
|> set_tenant(opts[:tenant] || changeset.tenant || changeset.data.__metadata__[:tenant])
end

defp reset_arguments(%{arguments: arguments} = changeset) do
Enum.reduce(arguments, changeset, fn {key, value}, changeset ->
set_argument(changeset, key, value)
end)
end

def handle_params(changeset, action, params, handle_params_opts \\ []) do
if Keyword.get(handle_params_opts, :cast_params?, true) do
cast_params(changeset, action, params || %{})
Expand Down
12 changes: 8 additions & 4 deletions lib/ash/resource/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1314,9 +1314,6 @@ defmodule Ash.Resource.Dsl do
]

@transformers [
Ash.Resource.Transformers.CacheRelationships,
Ash.Resource.Transformers.AttributesByName,
Ash.Resource.Transformers.ValidationsAndChangesForType,
Ash.Resource.Transformers.RequireUniqueActionNames,
Ash.Resource.Transformers.SetRelationshipSource,
Ash.Resource.Transformers.BelongsToAttribute,
Expand All @@ -1333,6 +1330,12 @@ defmodule Ash.Resource.Dsl do
Ash.Resource.Transformers.GetByReadActions
]

@persisters [
Ash.Resource.Transformers.CacheRelationships,
Ash.Resource.Transformers.AttributesByName,
Ash.Resource.Transformers.ValidationsAndChangesForType
]

@verifiers [
Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch,
Ash.Resource.Verifiers.VerifyReservedCalculationArguments,
Expand All @@ -1356,7 +1359,8 @@ defmodule Ash.Resource.Dsl do
use Spark.Dsl.Extension,
sections: @sections,
transformers: @transformers,
verifiers: @verifiers
verifiers: @verifiers,
persisters: @persisters

@doc false
def identity(x), do: x
Expand Down
Loading

0 comments on commit e680867

Please sign in to comment.