Class: UnchangeableValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/protected/user.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/models/protected/user.rb', line 57

def validate_each(object, attribute, value)
  if !object.new_record? && value.present?
    original = object.class.send(:where, "id = #{object.id}").select("id, #{attribute.to_s}").first
    if original.send(attribute) != value
      object.errors[attribute] << (options[:message] || "cannot be changed once assigned")
    end
  end
end