Class: Validatable::ValidatesConfirmationOf

Inherits:
ValidationBase show all
Defined in:
lib/validatable/validations/validates_confirmation_of.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from ValidationBase

#after_validate, #allow_blank, #allow_nil, #attribute, #if, #klass

Instance Method Summary collapse

Methods inherited from ValidationBase

#validate

Constructor Details

#initialize(klass, attribute, options = {}) ⇒ ValidatesConfirmationOf

Returns a new instance of ValidatesConfirmationOf.



5
6
7
8
9
10
11
12
13
# File 'lib/validatable/validations/validates_confirmation_of.rb', line 5

def initialize(klass, attribute, options={})
  klass.class_eval do
    attr_accessor "_#{attribute}_confirmation"
    alias_method "#{attribute}_confirmation", "_#{attribute}_confirmation"
    alias_method "#{attribute}_confirmation=", "_#{attribute}_confirmation="
  end
  super
  self.case_sensitive = true if self.case_sensitive == nil
end

Instance Attribute Details

#case_sensitiveObject

Returns the value of attribute case_sensitive.



3
4
5
# File 'lib/validatable/validations/validates_confirmation_of.rb', line 3

def case_sensitive
  @case_sensitive
end

Instance Method Details

#message(instance) ⇒ Object



23
24
25
# File 'lib/validatable/validations/validates_confirmation_of.rb', line 23

def message(instance)
  super || "doesn't match confirmation"
end

#valid?(instance) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/validatable/validations/validates_confirmation_of.rb', line 15

def valid?(instance)
  confirmation_value = instance.send("#{self.attribute}_confirmation")
  return true if allow_nil && confirmation_value.nil?
  return true if allow_blank && (!confirmation_value or (confirmation_value.respond_to?(:empty?) and confirmation_value.empty?))
  return instance.send(self.attribute) == instance.send("#{self.attribute}_confirmation".to_sym) if case_sensitive
  instance.send(self.attribute).to_s.casecmp(instance.send("#{self.attribute}_confirmation".to_sym).to_s) == 0
end