Skip to content
This repository
Browse code

Add validates_confirmation_of matcher

  • Loading branch information...
commit 1b5fc27cf8df2ab9e74259c1f853ae6fc8711e08 1 parent 25c2623
Blake Thomson authored October 23, 2011
1  lib/shoulda/matchers/active_model.rb
@@ -8,6 +8,7 @@
8 8
 require 'shoulda/matchers/active_model/validate_format_of_matcher'
9 9
 require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'
10 10
 require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
  11
+require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
11 12
 require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
12 13
 require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
13 14
 
63  lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
... ...
@@ -0,0 +1,63 @@
  1
+module Shoulda # :nodoc:
  2
+  module Matchers
  3
+    module ActiveModel # :nodoc:
  4
+      # Ensures that the model's attribute matches confirmation
  5
+      #
  6
+      # Example:
  7
+      #   it { should validate_confirmation_of(:password) }
  8
+      #
  9
+      def validate_confirmation_of(attr)
  10
+        ValidateConfirmationOfMatcher.new(attr)
  11
+      end
  12
+
  13
+      class ValidateConfirmationOfMatcher < ValidationMatcher # :nodoc:
  14
+        include Helpers
  15
+
  16
+        def initialize(attribute)
  17
+          @attribute = attribute
  18
+          @confirmation = "#{attribute}_confirmation"
  19
+        end
  20
+
  21
+        def with_message(message)
  22
+          @message = message if message
  23
+          self
  24
+        end
  25
+
  26
+        def description
  27
+          "require #{@base_attribute} to match #{@attribute}"
  28
+        end
  29
+
  30
+        def matches?(subject)
  31
+          super(subject)
  32
+          @message ||= :confirmation
  33
+
  34
+          disallows_different_value &&
  35
+            allows_same_value &&
  36
+            allows_missing_confirmation
  37
+        end
  38
+
  39
+        private
  40
+
  41
+        def disallows_different_value
  42
+          set_confirmation("some value")
  43
+          matches = disallows_value_of("different value", @message)
  44
+        end
  45
+
  46
+        def allows_same_value
  47
+          set_confirmation("same value")
  48
+          allows_value_of("same value", @message)
  49
+        end
  50
+
  51
+        def allows_missing_confirmation
  52
+          set_confirmation(nil)
  53
+          allows_value_of("any value", @message)
  54
+        end
  55
+
  56
+        def set_confirmation(val)
  57
+          setter = :"#{@confirmation}="
  58
+          @subject.send(setter, val) if @subject.respond_to?(setter)
  59
+        end
  60
+      end
  61
+    end
  62
+  end
  63
+end
48  spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb
... ...
@@ -0,0 +1,48 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
  4
+
  5
+  context "an attribute which needs confirmation" do
  6
+    before do
  7
+      define_model(:example, :attr => :string) do
  8
+        validates_confirmation_of :attr
  9
+      end
  10
+      @model = Example.new
  11
+    end
  12
+
  13
+    it "should require confirmation of that attribute" do
  14
+      @model.should validate_confirmation_of(:attr)
  15
+    end
  16
+
  17
+    it "should not override the default message with a blank" do
  18
+      @model.should validate_confirmation_of(:attr).with_message(nil)
  19
+    end
  20
+  end
  21
+
  22
+  context "an attribute which must be confirmed with a custom message" do
  23
+    before do
  24
+      define_model :example, :attr => :string do
  25
+        validates_confirmation_of :attr, :message => 'custom'
  26
+      end
  27
+      @model = Example.new
  28
+    end
  29
+
  30
+    it "should require confirmation of that attribute with that message" do
  31
+      @model.should validate_confirmation_of(:attr).with_message(/custom/)
  32
+    end
  33
+
  34
+    it "should not require confirmation of that attribute with another message" do
  35
+      @model.should_not validate_confirmation_of(:attr)
  36
+    end
  37
+  end
  38
+
  39
+  context "an attribute which doesn't need confirmation" do
  40
+    before do
  41
+      @model = define_model(:example, :attr => :string).new
  42
+    end
  43
+
  44
+    it "should not require confirmation of that attribute" do
  45
+      @model.should_not validate_confirmation_of(:attr)
  46
+    end
  47
+  end
  48
+end

0 notes on commit 1b5fc27

Please sign in to comment.
Something went wrong with that request. Please try again.