Module: ActiveRecord::Errors::Unique

Included in:
ActiveRecord::Errors
Defined in:
lib/vex/active_record/validation_error_ext.rb

Overview

better error reporting: this is useful mainly for development cycles, as it adds an error message only once

Instance Method Summary collapse

Instance Method Details

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



10
11
12
# File 'lib/vex/active_record/validation_error_ext.rb', line 10

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

#add_with_unique_messages(error_or_attr, message = nil, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vex/active_record/validation_error_ext.rb', line 14

def add_with_unique_messages(error_or_attr, message = nil, options = {})
  if error_or_attr.is_a?(ActiveRecord::Error)
    error, attribute = error_or_attr, error_or_attr.attribute
  else
    attribute = error_or_attr
    error = ActiveRecord::Error.new(@base, attribute, message, options)
  end
  
  options[:message] = options.delete(:default) if options.has_key?(:default)

  @errors[attribute.to_s] ||= []

  existing = @errors[attribute.to_s].detect do |err|
    err.message == message
  end
  
  existing || (@errors[attribute.to_s] << error)
end

#delete(entry) ⇒ Object



6
7
8
# File 'lib/vex/active_record/validation_error_ext.rb', line 6

def delete(entry)
  @errors.delete entry.to_s
end