ExTenant.Changeset (ExTenant v0.2.1) View Source

The function cast_tenanted(changset, attrs, allowed)

  • injects the tenant_id key/atom into the allowed fields
  • retrieves the tenant_id value from the process dictionary
  • injects the %{"tenant_id" => tenant_id} into the attrs map
  • and then calls the standard Ecto.Changeset.cast function

An exception will be raised in the following two situations

  • if the Repo was not set in the Config.exs file
  • if the tenant_id in the process dictionary is nil

Here is an example

defmodule Post do
  use ExTenant.Schema
  use ExTenant.Changeset

  ...

  defp changeset(attrs) do
    %__MODULE__{}
    |> cast_tenanted(params, [:name, :body])
  end
end

Link to this section Summary

Functions

Basically call this like the standard cast function and the module macros will handle all the tenant_id injecting

Link to this section Functions

Link to this macro

cast_tenanted(changeset, params, allowed)

View Source (macro)

Basically call this like the standard cast function and the module macros will handle all the tenant_id injecting

Example

def changeset(attrs) do
  %__MODULE__{}
  |> cast_tenanted(attrs, [:name, :body])
end