-
-
Notifications
You must be signed in to change notification settings - Fork 33
Fix/version resource naming #220
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
Open
M-Gonzalo
wants to merge
9
commits into
ash-project:main
Choose a base branch
from
M-Gonzalo:fix/version-resource-naming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7cf6c06
fix: handle ForbiddenField in full_diff when using field_policies (#215)
M-Gonzalo 41c96b8
fix: allow explicit version_resource naming to avoid X.Version clashe…
M-Gonzalo fabe322
Discard changes to CHANGELOG.md
M-Gonzalo e798011
Discard changes to lib/change_builders/full_diff/helpers.ex
M-Gonzalo 997f042
feat: version_of function added to auto-generated version modules
M-Gonzalo c0bf66f
Discard changes to test/ash_paper_trail/full_diff_test.exs
M-Gonzalo f9798e8
fix: simplify resource name generation logic and address linter issues
M-Gonzalo 9281bfd
Discard changes to test/ash_paper_trail/full_diff_test.exs
M-Gonzalo 9ca2c67
Discard changes to test/ash_paper_trail/full_diff_test.exs
M-Gonzalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,23 +11,49 @@ defmodule AshPaperTrail do | |
| apply(m, f, a) || allow_resource_versions(nil, resource) | ||
| end | ||
|
|
||
| @regex ~r/\.Version$/ | ||
| def allow_resource_versions(nil, resource) do | ||
| resource_name = to_string(resource) | ||
|
|
||
| if String.match?(resource_name, @regex) do | ||
| original_resource = | ||
| try do | ||
| resource_name | ||
| |> String.replace(@regex, "") | ||
| |> String.to_existing_atom() | ||
| rescue | ||
| ArgumentError -> false | ||
| end | ||
| def allow_resource_versions(nil, resource) when not is_atom(resource), do: false | ||
|
|
||
| def allow_resource_versions(nil, resource) when is_atom(resource) do | ||
| if safe_resource_version?(resource) do | ||
| if function_exported?(resource, :version_of, 0) do | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, you don't need it due to |
||
| case safe_version_of(resource) do | ||
| {:ok, original_resource} -> | ||
| AshPaperTrail.Resource in Spark.extensions(original_resource) | ||
|
|
||
| original_resource && AshPaperTrail.Resource in Spark.extensions(original_resource) | ||
| :error -> | ||
| false | ||
| end | ||
| else | ||
| relationship_fallback(resource) | ||
| end | ||
| else | ||
| false | ||
| end | ||
| end | ||
|
|
||
| defp safe_resource_version?(resource) do | ||
| resource.resource_version?() | ||
| rescue | ||
| _ -> | ||
| false | ||
| end | ||
|
|
||
| defp safe_version_of(resource) do | ||
| {:ok, resource.version_of()} | ||
| rescue | ||
| _ -> | ||
| :error | ||
| end | ||
|
|
||
| defp relationship_fallback(resource) do | ||
| relationships = Ash.Resource.Info.relationships(resource) | ||
|
|
||
| case Enum.find(relationships, &(&1.name == :version_source and &1.type == :belongs_to)) do | ||
| nil -> | ||
| false | ||
|
|
||
| relationship -> | ||
| AshPaperTrail.Resource in Spark.extensions(relationship.destination) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| defmodule AshPaperTrail.VersionResourceNamingTest do | ||
| use ExUnit.Case | ||
|
|
||
| setup_all do | ||
| # Ensure the support resource modules used by these tests are compiled and loaded | ||
| # deterministically before any tests run. This avoids intermittent failures where | ||
| # compile/load ordering leaves the Source module undefined at assertion time. | ||
| for mod <- [ | ||
| AshPaperTrail.Test.VersionNaming.Source, | ||
| AshPaperTrail.Test.VersionNaming.Source.Version, | ||
| AshPaperTrail.Test.VersionNaming.SourceVersionResource | ||
| ] do | ||
| case Code.ensure_compiled(mod) do | ||
| {:module, _} -> :ok | ||
| {:error, reason} -> raise "Failed to ensure compiled #{inspect(mod)}: #{inspect(reason)}" | ||
| end | ||
| end | ||
|
|
||
| :ok | ||
| end | ||
|
|
||
| alias AshPaperTrail.Test.VersionNaming.Domain | ||
| alias AshPaperTrail.Test.VersionNaming.Source | ||
| alias AshPaperTrail.Test.VersionNaming.Source.Version, as: SourceVersion | ||
| alias AshPaperTrail.Test.VersionNaming.SourceVersionResource | ||
| alias AshPaperTrail.Test.VersionOf.ManualVersionWithoutPaperTrail | ||
|
|
||
| describe "generated version resource naming" do | ||
| test "uses explicit version_resource module and does not conflict with an app-defined Source.Version" do | ||
| # Explicit version resource behaves like a version resource | ||
| assert SourceVersionResource.resource_version?() | ||
|
|
||
| # App-defined Source.Version is a regular resource, not a version resource | ||
| refute function_exported?(SourceVersion, :resource_version?, 0) | ||
|
|
||
| # Source itself must still be a valid resource | ||
| assert function_exported?(Source, :__info__, 1) | ||
| end | ||
|
|
||
| test "version_resource/1 returns the explicit version_resource module for Source" do | ||
| assert AshPaperTrail.Resource.Info.version_resource(Source) == SourceVersionResource | ||
| end | ||
|
|
||
| test "version_resource/1 falls back to X.Version for version modules themselves" do | ||
| assert AshPaperTrail.Resource.Info.version_resource(SourceVersion) == | ||
| Module.concat([SourceVersion, Version]) | ||
| end | ||
|
|
||
| test "writes versions for Source into the explicitly configured version resource module" do | ||
| Ash.create!(Source, %{name: "named source"}, domain: Domain) | ||
|
|
||
| versions_in_explicit_module = | ||
| Ash.read!(SourceVersionResource, domain: Domain) | ||
|
|
||
| versions_in_app_defined_version = | ||
| Ash.read!(SourceVersion, domain: Domain) | ||
|
|
||
| assert length(versions_in_explicit_module) == 1 | ||
|
|
||
| assert Enum.at(versions_in_explicit_module, 0).changes[:name] == "named source" | ||
|
|
||
| assert versions_in_app_defined_version == [] | ||
| end | ||
|
|
||
| test "generated version resource defines version_of/0 returning the original resource" do | ||
| # Calling version_of/0 forces the module to be loaded and ensures the function exists | ||
| assert SourceVersionResource.version_of() == Source | ||
| end | ||
|
|
||
| test "allow_resource_versions/2 accepts generated version resources via version_of/0" do | ||
| assert AshPaperTrail.allow_resource_versions(nil, SourceVersionResource) | ||
| end | ||
| end | ||
|
|
||
| describe "allow_resource_versions/2 with manual version_of/0" do | ||
| test "rejects version modules whose version_of/0 points to a non-paper-trail resource" do | ||
| # These calls both verify the functions exist and force module loading | ||
| assert ManualVersionWithoutPaperTrail.resource_version?() | ||
|
|
||
| assert ManualVersionWithoutPaperTrail.version_of() == | ||
| AshPaperTrail.Test.VersionOf.NonPaperTrailResource | ||
|
|
||
| refute AshPaperTrail.allow_resource_versions(nil, ManualVersionWithoutPaperTrail) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| defmodule AshPaperTrail.Test.VersionNaming.Domain do | ||
| @moduledoc false | ||
|
|
||
| use Ash.Domain, | ||
| extensions: [AshPaperTrail.Domain], | ||
| validate_config_inclusion?: false | ||
|
|
||
| resources do | ||
| resource AshPaperTrail.Test.VersionNaming.Source | ||
| resource AshPaperTrail.Test.VersionNaming.Source.Version | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| defmodule AshPaperTrail.Test.VersionNaming.Source do | ||
| @moduledoc false | ||
|
|
||
| use Ash.Resource, | ||
| domain: AshPaperTrail.Test.VersionNaming.Domain, | ||
| data_layer: Ash.DataLayer.Ets, | ||
| extensions: [AshPaperTrail.Resource], | ||
| validate_domain_inclusion?: false | ||
|
|
||
| ets do | ||
| private? true | ||
| end | ||
|
|
||
| attributes do | ||
| uuid_primary_key :id | ||
|
|
||
| attribute :name, :string do | ||
| allow_nil? false | ||
| public? true | ||
| end | ||
| end | ||
|
|
||
| actions do | ||
| default_accept :* | ||
| defaults [:create, :read, :update, :destroy] | ||
| end | ||
|
|
||
| paper_trail do | ||
| # Explicitly configure a different version resource module to avoid | ||
| # colliding with the app-defined Source.Version. | ||
| version_resource(AshPaperTrail.Test.VersionNaming.SourceVersionResource) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| defmodule AshPaperTrail.Test.VersionNaming.Source.Version do | ||
| @moduledoc """ | ||
| Test version resource for version naming tests. | ||
| """ | ||
| use Ash.Resource, | ||
| domain: AshPaperTrail.Test.VersionNaming.Domain, | ||
| data_layer: Ash.DataLayer.Ets, | ||
| validate_domain_inclusion?: false | ||
|
|
||
| ets do | ||
| private? true | ||
| end | ||
|
|
||
| attributes do | ||
| uuid_primary_key :id | ||
|
|
||
| attribute :label, :string do | ||
| allow_nil? false | ||
| public? true | ||
| end | ||
| end | ||
|
|
||
| actions do | ||
| default_accept :* | ||
| defaults [:create, :read, :update, :destroy] | ||
| end | ||
| end |
37 changes: 37 additions & 0 deletions
37
test/support/version_of/manual_version_without_paper_trail.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| defmodule AshPaperTrail.Test.VersionOf.NonPaperTrailResource do | ||
| @moduledoc false | ||
|
|
||
| use Ash.Resource, | ||
| domain: nil, | ||
| data_layer: Ash.DataLayer.Ets, | ||
| validate_domain_inclusion?: false | ||
|
|
||
| ets do | ||
| private? true | ||
| end | ||
|
|
||
| attributes do | ||
| uuid_primary_key :id | ||
|
|
||
| attribute :name, :string do | ||
| allow_nil? false | ||
| public? true | ||
| end | ||
| end | ||
|
|
||
| actions do | ||
| default_accept :* | ||
| defaults [:create, :read, :update, :destroy] | ||
| end | ||
| end | ||
|
|
||
| defmodule AshPaperTrail.Test.VersionOf.ManualVersionWithoutPaperTrail do | ||
| @moduledoc false | ||
|
|
||
| alias AshPaperTrail.Test.VersionOf.NonPaperTrailResource | ||
|
|
||
| # Mimics a version resource (has resource_version?/0 and version_of/0) | ||
| # but its underlying resource does NOT use AshPaperTrail.Resource. | ||
| def resource_version?, do: true | ||
| def version_of, do: NonPaperTrailResource | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.