View Source EctoDiscriminator.DiscriminatorChangeset protocol (ecto_discriminator v0.4.3)

Protocol for defining behaviour of changesets on discriminator-enabled schemas.

In most cases there is no need to alter this logic, however if there is some extra logic needed to be executed everytime some diverged schema changeset is being created it can be injected by overriding this protocol.

Could be useful especially for some side effects that doesn't look "good" when put directly inside base changeset function

Link to this section Summary

Functions

Outputs changeset for base schema from a diverged one.

Calls changeset/2 function from base type.

Calls changeset/2 function from diverged type.

Link to this section Types

Link to this section Functions

Link to this function

base_changeset(struct, params \\ %{})

View Source

Outputs changeset for base schema from a diverged one.

This makes it possible to reduce diverged struct to base version in places where we have polymorphic relationship, to avoid loading the same data in multiple ways.

Type is inferred from the discriminator field in passed struct.

Every module that uses EctoDiscriminator.Schema derives the basic implementation. There is also a helper method to avoid aliasing EctoDiscriminator.DiscriminatorChangeset everywhere.

examples

Examples

changeset = DiscriminatorChangeset.base_changeset(%SomeTable.Foo{not_in_base: 1}, %{not_in_base_param: 1})
changeset.data #=> %SomeTable{...} - won't contain "not_in" values
changeset.changes #=> %{}

You can call the same thing from any module that uses EctoDiscriminator.schema:

changeset = SomeTable.base_changeset(%SomeTable.Foo{}, %{})
changeset.data #=> %SomeTable{...}
Link to this function

cast_base(struct, params)

View Source

Calls changeset/2 function from base type.

Use this inside changeset function of a diverged schema to call base changeset.

By default it calls changeset/2 from base schema to apply logic for common fields and then returns changeset for further modifications.

It can be placed anywhere in the changeset, but the safest place should be the beginning. cast_base/2 won't contain changes for fields that are overriden by diverged schema.

Every module that uses EctoDiscriminator.Schema derives the basic implementation. There is also a helper method to avoid aliasing EctoDiscriminator.DiscriminatorChangeset everywhere.

examples

Examples

def changeset(struct, params) do
  struct
  |> ...
  |> cast_base(params)
  |> ...
end
Link to this function

diverged_changeset(struct, params \\ %{})

View Source

Calls changeset/2 function from diverged type.

In general, this function works the same like calling changeset/2 on diverged schema with cast_base/2 inside. It becomes useful when you want to create changeset for diverged struct, but the data is coming from external source (like user input). This makes it possible to use generic naming like SomeTable in places where there could be multiple diverged types inserted by the same function, to keep code clean.

Type is inferred from the discriminator field in params or from passed struct.

Every module that uses EctoDiscriminator.Schema derives the basic implementation. There is also a helper method to avoid aliasing EctoDiscriminator.DiscriminatorChangeset everywhere.

examples

Examples

changeset = DiscriminatorChangeset.diverged_changeset(%SomeTable{}, %{type: SomeTable.Foo})
changeset.data #=> %SomeTable.Foo{...}

You can call the same thing from any module that uses EctoDiscriminator.schema:

changeset = SomeTable.diverged_changeset(%SomeTable{}, %{type: SomeTable.Foo})
changeset.data #=> %SomeTable.Foo{...}

If type is missing from params it will be taken from the passed struct:

changeset = SomeTable.diverged_changeset(%SomeTable.Foo{}, %{title: "abc"})
changeset.data #=> %SomeTable.Foo{...}