diff --git a/lib/ash_slug/changes/slugify.ex b/lib/ash_slug/changes/slugify.ex index 44a138d..852853c 100644 --- a/lib/ash_slug/changes/slugify.ex +++ b/lib/ash_slug/changes/slugify.ex @@ -50,7 +50,7 @@ defmodule AshSlug.Changes.Slugify do Ash.Changeset.before_action(changeset, fn changeset -> with {attribute, opts} <- Keyword.pop(opts, :attribute), {into, opts} <- Keyword.pop(opts, :into, attribute), - {:ok, value} when is_binary(value) <- Ash.Changeset.fetch_argument_or_change(changeset, attribute), + {:ok, value} when is_binary(value) <- get_attribute(changeset, attribute), slug <- Slug.slugify(value, opts) do changeset |> Ash.Changeset.force_change_attribute(into, slug) @@ -61,6 +61,13 @@ defmodule AshSlug.Changes.Slugify do end) end + defp get_attribute(changeset, attribute) do + case Ash.Changeset.fetch_argument_or_change(changeset, attribute) do + {:ok, %Ash.CiString{} = value} -> {:ok, Ash.CiString.value(value)} + res -> res + end + end + @spec replace_lowercase_opts(Keyword.t()) :: Keyword.t() defp replace_lowercase_opts(opts) do Keyword.pop(opts, :lowercase?) diff --git a/test/ash_slug_test.exs b/test/ash_slug_test.exs index 566098d..a1472d1 100644 --- a/test/ash_slug_test.exs +++ b/test/ash_slug_test.exs @@ -12,6 +12,16 @@ defmodule AshSlugTest do assert resource.text1 == "Hello-World" end + test "ensure Ash.CiString value is slugified" do + resource = + AshSlugTest.Resource + |> Ash.Changeset.for_create(:create, %{text3: Ash.CiString.new("Hello, World!")}) + |> Ash.Changeset.set_context(%{foo: :bar}) + |> Ash.create!() + + assert resource.text3_slug == "hello-world" + end + test "ensure value is slugified into another attribute" do resource = AshSlugTest.Resource diff --git a/test/support/resource.ex b/test/support/resource.ex index 733c985..fb958aa 100644 --- a/test/support/resource.ex +++ b/test/support/resource.ex @@ -15,15 +15,18 @@ defmodule AshSlugTest.Resource do attribute(:text1, :string, public?: true) attribute(:text2, :string, public?: true) attribute(:text2_slug, :string) + attribute(:text3, :ci_string, public?: true) + attribute(:text3_slug, :string) attribute(:bool, :boolean, public?: true) end actions do create :create do - accept([:text1, :text2, :bool]) + accept([:text1, :text2, :text3, :bool]) change(slugify(:text1, lowercase?: false)) change(slugify(:text2, into: :text2_slug)) + change(slugify(:text3, into: :text3_slug)) change(slugify(:bool)) end end