Class: NotNaughty::ConfirmationValidation

Inherits:
Validation
  • Object
show all
Defined in:
lib/not_naughty/validations/confirmation_validation.rb

Overview

Validates confirmaton of obj’s attribute via :eql? call against the appropiate confirmation attribute.

Unless the validation succeeds an error hash (:attribute => :message) is added to the obj’s instance of Errors.

Options:

:message

see NotNaughty::Errors for details

:if

see NotNaughty::Validation::Condition for details

:unless

see NotNaughty::Validation::Condition for details

Example:

obj = 'abc'
def obj.errors() @errors ||= NotNauthy::Errors.new end
def obj.to_s_confirmation() '123 end

ConfirmationValidation.new({}, :to_s).call obj, :to_s, 'abc'
obj.errors.on(:to_s).any? # => true

Constant Summary

Constants inherited from Validation

Validation::BASEDIR, Validation::PATTERN

Instance Attribute Summary

Attributes inherited from Validation

#attributes

Instance Method Summary collapse

Methods inherited from Validation

#call_with_conditions, #call_without_conditions, inherited, load, new

Constructor Details

#initialize(opts, attributes) ⇒ ConfirmationValidation

:nodoc:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/not_naughty/validations/confirmation_validation.rb', line 23

def initialize(opts, attributes) #:nodoc:
  __message = opts[:message] || '#{"%s".humanize} could not be confirmed.'  
  
  if opts[:allow_blank] or opts[:allow_nil]
    __allow = if opts[:allow_blank] then :blank? else :nil? end
    super opts, attributes do |o, a, v|
      o.errors.add a, __message unless
      v.send! __allow or o.send!(:"#{a}_confirmation").eql? v
    end
  else
    super opts, attributes do |o, a, v|
      o.errors.add a, __message unless
      o.send!(:"#{a}_confirmation").eql? v
    end
  end
end