class ActiveRecordUniquenessViaDb::Validator

Attributes

adapter[RW]
attrs_to_rescue[RW]
exception[RW]
record[RW]

Public Class Methods

new(adapter, record, exception) click to toggle source
# File lib/active_record_uniqueness_via_db/validator.rb, line 4
def initialize(adapter, record, exception)
  @adapter         = adapter
  @record          = record
  @exception       = exception
  @attrs_to_rescue = record.
    instance_variable_get("@#{ActiveRecordUniquenessViaDb::ATTRS_IVAR}") || {}
end
validate(adapter, record, exception) click to toggle source
# File lib/active_record_uniqueness_via_db/validator.rb, line 20
def self.validate(adapter, record, exception)
  new(adapter,record, exception).call
end

Public Instance Methods

call() click to toggle source
# File lib/active_record_uniqueness_via_db/validator.rb, line 12
def call
  if attribute_with_error
    record.errors.add(attribute_with_error.first, :taken, attribute_with_error.last)
  else
    raise exception
  end
end

Private Instance Methods

attribute_with_error() click to toggle source
# File lib/active_record_uniqueness_via_db/validator.rb, line 26
def attribute_with_error
  @attr_with_error ||=
    attrs_to_rescue.
    find { |attr, _| adapter.error_matches_attr(attr, exception.message) }
end