makandra/assignable_values

Restrict values that can *not* be assigned

elsapet opened this issue · 3 comments

Is there a way to restrict the values that can not be assigned to attributes or associations?
For example, if I wanted user.name to be any value except admin or administrator, I could do something like this:

class User < ActiveRecord::Base
  non_assignable_values_for :name do
    ['admin', 'administrator']
  end
end

And if there isn't a way, would this be worth a pull request?
Thanks for a great gem, by the way :)

Sounds interesting, definitely. I think @henning-koch should decide on this.

Hi @elsapet . I think getting this into the gem would involve a lot of pain because of questions like:

  • What would happen if we call user.assignable_values to receive the list of all possible values?
  • What would happen if the same model has both assignable_values and non_assignable_values? What if they contradict each other?

I would implement your use case above with Rails' built-in validates_exclusion_of macro.

Thank you @codener and @henning-koch for the responses. I hadn't thought of those cases!
Thanks for the validates_exclusion_of suggestion -- it will work great for my situation.