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) ->
do_set_argument(changeset, argument.name, value, true)
attr = Ash.Resource.Info.public_attribute(changeset.resource, name) ->
if attr.writable? do
attr = Ash.Resource.Info.attribute(changeset.resource, name) ->
if attr.writable? && attr.name in changeset.action.accept do
do_change_attribute(changeset, attr.name, value, true)
else
cond do

View file

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