If I want to control some behaviour downside the child associations tree during cast process what is better?
Process.put(:allow_edit, true)
# Somewhere in nested changeset
def changeset(struct, attrs)
cs = struct
|> cast(attrs, [:foobar])
cs = if Process.get(:allow_edit, false) do
cs
else
validate_not_edited_somefield_because_it_is_restructed(cs)
end
cs
end
vs
attrs = put_in(attrs, ["blah", "allow_edit"], true)
...
def changeset(struct, attrs)
# Mostly the same but check from attrs variable instead of Process data
end
You should avoid doing this at nearly all costs, for the same reason that you should basically always avoid global variables in other languages. It’s particularly jarring in Elixir because it’s one of the few things that doesn’t work functionally.
There are some valid use cases, but never just to avoid passing in a parameter.