Skip to content

Commit

Permalink
improvement: only_when_valid? on changes
Browse files Browse the repository at this point in the history
fix: set storage_type to `:uuid` for Ash.Type.UUID
  • Loading branch information
zachdaniel committed Nov 1, 2021
1 parent a36da24 commit ca631d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ locals_without_parens = [
name: 1,
not_found_message: 1,
on: 1,
only_when_valid?: 1,
pagination: 1,
parse_attribute: 1,
prefix: 1,
Expand Down
3 changes: 3 additions & 0 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ defmodule Ash.Changeset do
changes = changes ++ Ash.Resource.Info.changes(changeset.resource, changeset.action_type)

Enum.reduce(changes, changeset, fn
%{only_when_valid?: true}, %{valid?: false} = changeset ->
changeset

%{change: {module, opts}}, changeset ->
module.change(changeset, opts, %{actor: actor})

Expand Down
11 changes: 10 additions & 1 deletion lib/ash/resource/change/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Ash.Resource.Change do
when this change was configured on a resource, and the context, which currently only has
the actor.
"""
defstruct [:change, :on]
defstruct [:change, :on, :only_when_valid?]

@type t :: %__MODULE__{}

Expand All @@ -26,6 +26,15 @@ defmodule Ash.Resource.Change do
Many validations don't make sense in the context of deletion, so by default it is left out of the list.
"""
],
only_when_valid?: [
type: :boolean,
default: false,
doc: """
If the change should only be run on valid changes. By default, all changes are run unless stated otherwise here.
For 2.0 this may become the default.
"""
],
change: [
type: {:ash_behaviour, Ash.Resource.Change, Ash.Resource.Change.Builtins},
doc: """
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ defmodule Ash.Type do

# @callback equal?(term, term) :: boolean

defmacro __using__(_) do
defmacro __using__(_opts) do
quote location: :keep do
@behaviour Ash.Type

Expand Down
2 changes: 1 addition & 1 deletion lib/ash/type/uuid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Ash.Type.UUID do
use Ash.Type

@impl true
def storage_type, do: Ecto.UUID
def storage_type, do: :uuid

@impl true
def cast_input(value, _) when is_binary(value) do
Expand Down

0 comments on commit ca631d2

Please sign in to comment.