Skip to content

Commit

Permalink
remove auto_cast_and_apply_action
Browse files Browse the repository at this point in the history
  • Loading branch information
maltoe committed Dec 20, 2023
1 parent 823901c commit 103af66
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 59 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
### Added

* Add `BitcrowdEcto.Changeset.auto_cast/3`, a introspection-based automatic cast function.
* Add `BitcrowdEcto.Changeset.auto_cast_and_apply_action/3`, a convenient wrapper around `auto_cast/3` that directly returns a schema struct.
* Add `BitcrowdEcto.Assertions.assert_cast_error_on/2`.

### Fixed
Expand Down
58 changes: 0 additions & 58 deletions lib/bitcrowd_ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -539,62 +539,4 @@ defmodule BitcrowdEcto.Changeset do
Ecto.Changeset.cast_embed(cs, embed, required: embed in required)
end)
end

@type action_option :: {:action, :insert | :update}

@doc """
Convenience function calling `auto_cast/3` and `apply_action/2` in one step.
## Options
- `action` the action to apply to the changeset (`:insert` or `:update`, defaults to `:insert`)
- all options from `auto_cast/3`
## Example
iex> auto_cast_and_apply(TestEmbeddedSchema, %{some_field: 4})
{:ok, %TestEmbeddedSchema{some_field: 4}}
"""
@doc since: "0.17.0"
@spec auto_cast_and_apply(module | struct, map) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@spec auto_cast_and_apply(module | struct, map, [auto_cast_option | action_option]) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
def auto_cast_and_apply(schema_or_struct_or_changeset, params, opts \\ []) do
do_auto_cast_and_apply(
schema_or_struct_or_changeset,
params,
&Ecto.Changeset.apply_action/2,
opts
)
end

@doc """
Explosive variant of `auto_cast_and_apply/3`.
## Example
iex> auto_cast_and_apply!(TestEmbeddedSchema, %{some_field: 4})
%TestEmbeddedSchema{some_field: 4}
"""
@doc since: "0.17.0"
@spec auto_cast_and_apply!(module | struct, map) :: Ecto.Schema.t() | no_return
@spec auto_cast_and_apply!(module | struct, map, [auto_cast_option | action_option]) ::
Ecto.Schema.t() | no_return
def auto_cast_and_apply!(schema_or_struct_or_changeset, params, opts \\ []) do
do_auto_cast_and_apply(
schema_or_struct_or_changeset,
params,
&Ecto.Changeset.apply_action!/2,
opts
)
end

defp do_auto_cast_and_apply(schema_or_struct_or_changeset, params, apply_action, opts) do
{action, opts} = Keyword.pop(opts, :action, :insert)

schema_or_struct_or_changeset
|> auto_cast(params, opts)
|> apply_action.(action)
end
end

0 comments on commit 103af66

Please sign in to comment.