Class: ActiveModel::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/e9_crm/rails_extensions.rb

Instance Method Summary collapse

Instance Method Details

#add(attribute, message = nil, options = {}) ⇒ Object

Monkeypatch add to replace the error message with a LabeledString, which simply changes method missing to respond to methods ending with ‘?’ by checking to see if their label matches the method.

This makes it easy to check an error’s message for what error symbol it was generated for, e.g. :invalid, by calling #invalid? on it.

Errors#add accepts a Proc or a Symbol for message (or defaults to :invalid) so this is hack isn’t always useful, but for my purposes I want to know when a :taken error was added to a User#email, and this is good enough.



106
107
108
109
110
# File 'lib/e9_crm/rails_extensions.rb', line 106

def add(attribute, message = nil, options = {})
  default_add(attribute, message, options)

  self[attribute] << E9Crm::LabeledString.new(message, self[attribute].pop)
end

#default_addObject



93
# File 'lib/e9_crm/rails_extensions.rb', line 93

alias :default_add :add