Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add validates_confirmation_of matcher

  • Loading branch information...
commit 1b5fc27cf8df2ab9e74259c1f853ae6fc8711e08 1 parent 25c2623
@thomsbg thomsbg authored
View
1  lib/shoulda/matchers/active_model.rb
@@ -8,6 +8,7 @@
require 'shoulda/matchers/active_model/validate_format_of_matcher'
require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'
require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
+require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
View
63 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
@@ -0,0 +1,63 @@
+module Shoulda # :nodoc:
+ module Matchers
+ module ActiveModel # :nodoc:
+ # Ensures that the model's attribute matches confirmation
+ #
+ # Example:
+ # it { should validate_confirmation_of(:password) }
+ #
+ def validate_confirmation_of(attr)
+ ValidateConfirmationOfMatcher.new(attr)
+ end
+
+ class ValidateConfirmationOfMatcher < ValidationMatcher # :nodoc:
+ include Helpers
+
+ def initialize(attribute)
+ @attribute = attribute
+ @confirmation = "#{attribute}_confirmation"
+ end
+
+ def with_message(message)
+ @message = message if message
+ self
+ end
+
+ def description
+ "require #{@base_attribute} to match #{@attribute}"
+ end
+
+ def matches?(subject)
+ super(subject)
+ @message ||= :confirmation
+
+ disallows_different_value &&
+ allows_same_value &&
+ allows_missing_confirmation
+ end
+
+ private
+
+ def disallows_different_value
+ set_confirmation("some value")
+ matches = disallows_value_of("different value", @message)
+ end
+
+ def allows_same_value
+ set_confirmation("same value")
+ allows_value_of("same value", @message)
+ end
+
+ def allows_missing_confirmation
+ set_confirmation(nil)
+ allows_value_of("any value", @message)
+ end
+
+ def set_confirmation(val)
+ setter = :"#{@confirmation}="
+ @subject.send(setter, val) if @subject.respond_to?(setter)
+ end
+ end
+ end
+ end
+end
View
48 spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
+
+ context "an attribute which needs confirmation" do
+ before do
+ define_model(:example, :attr => :string) do
+ validates_confirmation_of :attr
+ end
+ @model = Example.new
+ end
+
+ it "should require confirmation of that attribute" do
+ @model.should validate_confirmation_of(:attr)
+ end
+
+ it "should not override the default message with a blank" do
+ @model.should validate_confirmation_of(:attr).with_message(nil)
+ end
+ end
+
+ context "an attribute which must be confirmed with a custom message" do
+ before do
+ define_model :example, :attr => :string do
+ validates_confirmation_of :attr, :message => 'custom'
+ end
+ @model = Example.new
+ end
+
+ it "should require confirmation of that attribute with that message" do
+ @model.should validate_confirmation_of(:attr).with_message(/custom/)
+ end
+
+ it "should not require confirmation of that attribute with another message" do
+ @model.should_not validate_confirmation_of(:attr)
+ end
+ end
+
+ context "an attribute which doesn't need confirmation" do
+ before do
+ @model = define_model(:example, :attr => :string).new
+ end
+
+ it "should not require confirmation of that attribute" do
+ @model.should_not validate_confirmation_of(:attr)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.