Class: ActiveModel::Errors
- Inherits:
-
Object
- Object
- ActiveModel::Errors
- Defined in:
- lib/e9_crm/rails_extensions.rb
Instance Method Summary collapse
-
#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.
- #default_add ⇒ Object
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, = nil, = {}) default_add(attribute, , ) self[attribute] << E9Crm::LabeledString.new(, self[attribute].pop) end |
#default_add ⇒ Object
93 |
# File 'lib/e9_crm/rails_extensions.rb', line 93 alias :default_add :add |