Sha256: ae2a6fc8750600b9b7ac07303067edac92342e4253d0b703c125daa740f2f992

Contents?: true

Size: 1.76 KB

Versions: 11

Compression:

Stored size: 1.76 KB

Contents

# better error reporting: this is useful mainly for development cycles, as
# it adds an error message only once
module ActiveRecord::Errors::Unique

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

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

  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
end

class ActiveRecord::Errors
  include Unique
  alias_method_chain :add, :unique_messages
end

module ActiveRecord::Errors::Unique::Etest
  ActiveRecord::Base.connection.create_table :error_models do |t|
    t.integer :value
  end
  
  class ErrorModels < ActiveRecord::Base
  end
  
  def test_single_adds
    obj = ErrorModels.new

    obj.errors.add "value", "xxx"
    assert_kind_of(String, obj.errors["value"])

    obj.errors.add "value", "xxx"
    assert_kind_of(String, obj.errors["value"])

    obj.errors.add "value", "yyyy"
    assert_equal(%w(xxx yyyy), obj.errors["value"])

    obj.errors.add "value", "yyyy"
    assert_equal(%w(xxx yyyy), obj.errors["value"])

    obj.errors.add "value", "xxx"
    assert_equal(%w(xxx yyyy), obj.errors["value"])

    obj.errors.add "value", "zzz"
    assert_equal(%w(xxx yyyy zzz), obj.errors["value"])
  end
end if VEX_TEST == "active_record"

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/active_record/validation_error_ext.rb
vex-0.4.4 lib/vex/active_record/validation_error_ext.rb
vex-0.4.2 lib/vex/active_record/validation_error_ext.rb
vex-0.3.3 lib/vex/active_record/validation_error_ext.rb
vex-0.2.9 lib/vex/active_record/validation_error_ext.rb
vex-0.2.8 lib/vex/active_record/validation_error_ext.rb
vex-0.2.7 lib/vex/active_record/validation_error_ext.rb
vex-0.2.6 lib/vex/active_record/validation_error_ext.rb
vex-0.2.5 lib/vex/active_record/validation_error_ext.rb
vex-0.2.2 lib/vex/active_record/validation_error_ext.rb
vex-0.2.1 lib/vex/active_record/validation_error_ext.rb