ExUtils.Ecto.Changesets (ExUtils v1.0.1)

Copy Markdown View Source

Utility functions for working with Ecto changesets.

To use this module, you need to first add :ecto as a dependency in your mix project.

Summary

Functions

Returns the previous and new values for fields changed in an Ecto changeset.

Checks if the given changeset is for an Ecto embedded schema.

Checks if the changeset data has an Ecto source.

Prevents changing a field once set.

Puts a change only if the field is nil or has not already been changed.

Functions

changes_diff(changeset)

@spec changes_diff(Ecto.Changeset.t()) :: {map(), map()}

Returns the previous and new values for fields changed in an Ecto changeset.

Examples

iex> changeset = User.changeset(%User{}, %{name: "Ada"})
iex> changes_diff(changeset)
{%{name: nil}, %{name: "Ada"}}

iex> user = %User{name: "Ada", settings: %User.Settings{theme: "light"}}
iex> changeset = User.changeset(user, %{name: "Nas", settings: %{theme: "dark"}})
iex> changes_diff(changeset)
{
  %{name: "Ada", settings: %{theme: "light"}},
  %{name: "Nas", settings: %{theme: "dark"}}
}

embedded_schema?(changeset)

@spec embedded_schema?(Ecto.Changeset.t()) :: boolean()

Checks if the given changeset is for an Ecto embedded schema.

Returns false for source-backed schemas and schemaless changesets. Also see has_source?/1.

Examples

# When `Profile` is defined using `embedded_schema`
iex> changeset = Ecto.Changeset.change(%Profile{})
iex> embedded_schema?(changeset)
true

# When `User` schema maps to the "users" table
iex> changeset = Ecto.Changeset.change(%User{})
iex> embedded_schema?(changeset)
false

has_source?(changeset)

@spec has_source?(Ecto.Changeset.t()) :: boolean()

Checks if the changeset data has an Ecto source.

This returns true for changesets of source-backed schemas (that map to a real data source such as a database table), and returns false for Embedded Schemas and schemaless changesets.

Also see embedded_schema?/1.

Examples

# When `User` schema maps to the "users" table
iex> changeset = Ecto.Changeset.change(%User{})
iex> has_source?(changeset)
true

# When `Profile` is defined using `embedded_schema`
iex> changeset = Ecto.Changeset.change(%Profile{})
iex> has_source?(changeset)
false

prevent_change(changeset, field, opts \\ [])

@spec prevent_change(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()

Prevents changing a field once set.

Adds an error when the changeset changes a field whose original value is not ignored.

Options

  • :ignore - Initial values that may be changed. Defaults to [nil].
  • :message - Error message used when a change is prevented. Defaults to "cannot modify".

Examples

# Specify which fields cannot be changed
def changeset(user, attrs) do
  user
  |> cast(attrs, [:name, :email])
  |> prevent_change(:email)
end

iex> changeset = User.changeset(%User{email: "old@example.com"}, %{email: "new@example.com"})
iex> changeset.errors
[email: {"cannot modify", []}]

iex> changeset = User.changeset(%User{}, %{email: "new@example.com"})
iex> changeset.errors
[]

put_new_change(changeset, field, value)

@spec put_new_change(Ecto.Changeset.t(), atom(), any()) :: Ecto.Changeset.t()

Puts a change only if the field is nil or has not already been changed.

Value can optionally be a zero-arity function for lazy execution.

Examples

iex> changeset = Ecto.Changeset.change(%User{name: nil})
iex> changeset = put_new_change(changeset, :name, "Ali")
iex> changeset.changes
%{name: "Ali"}

iex> changeset = Ecto.Changeset.change(%User{name: nil})
iex> changeset = put_new_change(changeset, :name, fn -> "Shey" end)
iex> changeset.changes
%{name: "Shey"}

iex> changeset = Ecto.Changeset.change(%User{name: "Ali"})
iex> changeset = put_new_change(changeset, :name, "Shey")
iex> changeset.changes
%{}