View Source EctoShorts.CommonChanges (ecto_shorts v2.2.1)

This module is responsible for determining put/cast assoc as well as creating and updating model relations

Link to this section Summary

Functions

Checks if field on changeset is empty list in data or changes

Checks if field on changeset is nil in data or changes

This function is the primary use function Preloads changeset assoc if change is made and then and put_or_cast's it

Preloads a changesets association

Determines put or cast on association with some special magic

Run's changeset function if when function returns true

Link to this section Functions

Link to this function

changeset_field_empty?(changeset, key)

View Source

Specs

changeset_field_empty?(Ecto.Changeset.t(), atom()) :: boolean()

Checks if field on changeset is empty list in data or changes

Link to this function

changeset_field_nil?(changeset, key)

View Source

Specs

changeset_field_nil?(Ecto.Changeset.t(), atom()) :: boolean()

Checks if field on changeset is nil in data or changes

Link to this function

preload_change_assoc(changeset, key)

View Source

Specs

preload_change_assoc(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()
Link to this function

preload_change_assoc(changeset, key, opts)

View Source

Specs

preload_change_assoc(Ecto.Changeset.t(), atom(), Keyword.t()) ::
  Ecto.Changeset.t()

This function is the primary use function Preloads changeset assoc if change is made and then and put_or_cast's it

example

Example

iex> CommonChanges.preload_change_assoc(changeset, :my_relation) iex> CommonChanges.preload_change_assoc(changeset, :my_relation, repo: MyApp.OtherRepo) iex> CommonChanges.preload_change_assoc(changeset, :my_relation, required: true)

Link to this function

preload_changeset_assoc(changeset, key, opts \\ [])

View Source

Specs

preload_changeset_assoc(Ecto.Changeset.t(), atom(), [integer()]) ::
  Ecto.Changeset.t()

Preloads a changesets association

Link to this function

put_or_cast_assoc(changeset, key, opts \\ [])

View Source

Specs

put_or_cast_assoc(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()

Determines put or cast on association with some special magic

If you pass a many to many relation only a list of id's it will count that as a member_update and remove or add members to the relations list

E.G. User many_to_many Fruit

This would update the user to have only fruits with id 1 and 3

CommonChanges.put_or_cast_assoc(change(user, fruits: [%{id: 1}, %{id: 3}]), :fruits)
Link to this function

put_when(changeset, when_func, change_func)

View Source

Specs

Run's changeset function if when function returns true