View Source EctoShorts.CommonChanges (ecto_shorts v2.3.0)
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
Specs
changeset_field_empty?(Ecto.Changeset.t(), atom()) :: boolean()
Checks if field on changeset is empty list in data or changes
Specs
changeset_field_nil?(Ecto.Changeset.t(), atom()) :: boolean()
Checks if field on changeset is nil in data or changes
Specs
preload_change_assoc(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()
Specs
preload_change_assoc(Ecto.Changeset.t(), atom(), keyword()) :: 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) iex> CommonChanges.preload_change_assoc(changeset, :my_relation, required_when_missing: :my_relation_id)
Specs
preload_changeset_assoc(Ecto.Changeset.t(), atom(), keyword()) :: Ecto.Changeset.t()
Preloads a changesets association
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)
Specs
put_when( Ecto.Changeset.t(), (Ecto.Changeset.t() -> boolean()), (Ecto.Changeset.t() -> Ecto.Changeset.t()) ) :: Ecto.Changeset.t()
Run's changeset function if when function returns true