Sha256: d46fe44e8c6c06c60ed2002d991a5a65a3e9dc0db398ffb216e0028fff639829

Contents?: true

Size: 1.57 KB

Versions: 28

Compression:

Stored size: 1.57 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 #{@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

Version data entries

28 entries across 20 versions & 4 rubygems

Version Path
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.9.1.beta.3 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
devise_sociable-0.1.0 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.9.1.beta vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.9.0 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-1.4.2 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.3.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.3.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.3.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-1.4.1 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
shoulda-matchers-1.4.0 lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-0.8.1 vendor/bundle/gems/shoulda-matchers-1.3.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.1.0 vendor/bundle/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
challah-rolls-0.1.0 vendor/bundle/gems/shoulda-matchers-1.3.0/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb