Is Process.put safe comparing to passing attrs

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

I’m not sure what you want to achieve, but the Process dictionary should most likely be avoided…

1 Like

Reasons?

Because it generates values from thin air, and it’s hard to debug when you do not get the values out of it that you’d expect

2 Likes

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.

4 Likes