Skip to content

Commit

Permalink
fix: fix tenant attribute must allow_nil?: true (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
nallwhy authored Apr 26, 2024
1 parent 527765f commit 19cb75f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
34 changes: 22 additions & 12 deletions lib/resource/transformers/create_version_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
|> Ash.Resource.Info.attributes()
|> Enum.filter(&(&1.name in attributes_as_attributes))

accept =
[
:version_action_type,
if(store_action_name?, do: :version_action_name, else: nil),
attributes |> Enum.map(& &1.name),
:version_source_id,
:changes,
belongs_to_actors |> Enum.map(&:"#{&1.name}_id")
]
|> List.flatten()
|> Enum.reject(&is_nil/1)

sensitive_changes? =
dsl_state
|> Ash.Resource.Info.attributes()
Expand All @@ -56,6 +44,28 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do

multitenant? = not is_nil(Ash.Resource.Info.multitenancy_strategy(dsl_state))

accept =
[
:version_action_type,
if(store_action_name?, do: :version_action_name, else: nil),
attributes |> Enum.map(& &1.name),
:version_source_id,
:changes,
belongs_to_actors |> Enum.map(&:"#{&1.name}_id")
]
|> List.flatten()
|> Enum.reject(&is_nil/1)
|> then(fn accept ->
case multitenant? do
true ->
multitenancy_attribute = Ash.Resource.Info.multitenancy_attribute(dsl_state)
accept |> Enum.reject(&(&1 == multitenancy_attribute))

false ->
accept
end
end)

mixin =
case AshPaperTrail.Resource.Info.mixin(dsl_state) do
{m, f, a} ->
Expand Down
5 changes: 3 additions & 2 deletions test/ash_paper_trail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ defmodule AshPaperTrailTest do
}
describe "operations over resource api" do
test "creates work as normal" do
assert %{subject: "subject", body: "body"} =
assert %{subject: "subject", body: "body", tenant: "acme"} =
Posts.Post.create!(@valid_attrs, tenant: "acme")

assert [%{subject: "subject", body: "body"}] = Posts.Post.read!(tenant: "acme")
assert [%{subject: "subject", body: "body", tenant: "acme"}] =
Posts.Post.read!(tenant: "acme")
end

test "updates work as normal" do
Expand Down
2 changes: 1 addition & 1 deletion test/support/posts/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule AshPaperTrail.Test.Posts.Post do
uuid_primary_key :id, writable?: true

attribute :tenant, :string do
public? true
allow_nil? false
end

attribute :subject, :string do
Expand Down

0 comments on commit 19cb75f

Please sign in to comment.