chore: wrap up private attribtue acceptance feature

This commit is contained in:
Zach Daniel 2024-04-01 16:56:54 -04:00
parent b1c7c01701
commit ae5a9609ac
2 changed files with 11 additions and 21 deletions

View file

@ -1822,8 +1822,8 @@ defmodule Ash.Changeset do
argument = get_action_argument(action, name) -> argument = get_action_argument(action, name) ->
do_set_argument(changeset, argument.name, value, true) do_set_argument(changeset, argument.name, value, true)
attr = Ash.Resource.Info.public_attribute(changeset.resource, name) -> attr = Ash.Resource.Info.attribute(changeset.resource, name) ->
if attr.writable? do if attr.writable? && attr.name in changeset.action.accept do
do_change_attribute(changeset, attr.name, value, true) do_change_attribute(changeset, attr.name, value, true)
else else
cond do cond do

View file

@ -7,30 +7,23 @@ defmodule Ash.Resource.Verifiers.ValidateAccept do
@impl true @impl true
def verify(dsl_state) do def verify(dsl_state) do
{public_attributes, private_attributes} = attributes =
dsl_state dsl_state
|> Verifier.get_entities([:attributes]) |> Verifier.get_entities([:attributes])
|> Enum.split_with(& &1.public?)
public_attribute_names = MapSet.new(public_attributes, & &1.name) attribute_names = MapSet.new(attributes, & &1.name)
private_attribute_names = MapSet.new(private_attributes, & &1.name)
initial_errors = %{private: [], not_attribute: []} initial_errors = %{not_attribute: []}
result = result =
Verifier.get_entities(dsl_state, [:actions]) Verifier.get_entities(dsl_state, [:actions])
|> Enum.reduce(%{}, fn |> Enum.reduce(%{}, fn
%{name: action_name, accept: accept}, acc -> %{name: action_name, accept: accept}, acc ->
validate_attribute_name = fn attribute_name -> validate_attribute_name = fn attribute_name ->
cond do if MapSet.member?(attribute_names, attribute_name) do
MapSet.member?(private_attribute_names, attribute_name) -> :ok
:private else
:not_attribute
MapSet.member?(public_attribute_names, attribute_name) ->
:ok
true ->
:not_attribute
end end
end end
@ -38,16 +31,13 @@ defmodule Ash.Resource.Verifiers.ValidateAccept do
Enum.reduce( Enum.reduce(
accept, accept,
initial_errors, initial_errors,
fn attribute, %{private: private, not_attribute: not_attribute} = acc -> fn attribute, %{not_attribute: not_attribute} = acc ->
case validate_attribute_name.(attribute) do case validate_attribute_name.(attribute) do
:ok -> :ok ->
acc acc
:private ->
%{private: [attribute | private], not_attribute: not_attribute}
:not_attribute -> :not_attribute ->
%{private: private, not_attribute: [attribute | not_attribute]} %{not_attribute: [attribute | not_attribute]}
end end
end end
) )