Sha256: 2a62e9d9ef0b04d442515b3ca6aeb393e25e41a9f5f539b28b991b6cb8e90ad5

Contents?: true

Size: 1.59 KB

Versions: 11

Compression:

Stored size: 1.59 KB

Contents

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 #{@confirmation} 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')
          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}="
          if @subject.respond_to?(setter)
            @subject.send(setter, val)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 10 versions & 2 rubygems

Version Path
shoulda-matchers-2.3.0 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-1.0.0 vendor/bundle/gems/shoulda-matchers-2.2.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-2.2.0 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-2.1.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-2.1.0 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-1.0.0.beta2 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-2.0.0 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-1.5.6 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-1.5.5 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb