Class: ActiveSpec::Specifications::ConfirmationSpecification

Inherits:
AttributesSpecification show all
Defined in:
lib/active_spec/specifications/confirmation_specification.rb

Overview

Looks for a confirmation attribute for each given attribute. It looks for a confirmation attribtue named attributename_confirmation and checks that the value of the attribute and its confirmation attribute match. This could be used to validate password confirmations.

Example usage:

include ActiveSpec::Specifications

class User
  attr_accessor :password, :password_confirmation
end

spec = ConfirmationSpecification.new(:password)

# passing spec
user_one = User.new
user_one.password = 'foo'
user_one.password_confirmation = 'foo'
spec.satisfied_by?(user_one) # returns true

# failing spec
user_two = User.new
user_two.password = 'foo'
user_two.password_confirmation = 'bar'
spec.satisfied_by?(user_two) # returns false

This works in the same way as the Rails validation macro validates_confirmation_of.

Instance Method Summary collapse

Methods inherited from AttributesSpecification

#initialize

Constructor Details

This class inherits a constructor from ActiveSpec::Specifications::AttributesSpecification

Instance Method Details

#satisfied_by?(object) ⇒ Boolean

Returns false if the given object fails to respond to any of the attributes, or the attributes’ confirmation methods. If both attribute and attribute confirmation methods respond, then it checks that the values of both match.

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/active_spec/specifications/confirmation_specification.rb', line 36

def satisfied_by?(object)
  attributes_satisfy? do |a|
    return false unless object.respond_to?(a)
    return false unless object.respond_to?(confirmation_attribute(a))
    return false unless object.send(a) == object.send(confirmation_attribute(a))
  end
end