Sha256: f07e5d7a9252c0283b448921dc77216efb44c8b741ff3efa98e1adac8ca2378d

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

module Shoulda
  module Matchers
    module ActiveModel
      class ValidationMatcher
        # @private
        class BuildDescription
          def self.call(matcher, main_description)
            new(matcher, main_description).call
          end

          def initialize(matcher, main_description)
            @matcher = matcher
            @main_description = main_description
          end

          def call
            if description_clauses_for_qualifiers.any?
              main_description +
                ', ' +
                description_clauses_for_qualifiers.to_sentence
            else
              main_description
            end
          end

          protected

          attr_reader :matcher, :main_description

          private

          def description_clauses_for_qualifiers
            description_clauses = []

            if matcher.try(:expects_to_allow_blank?)
              description_clauses << 'but only if it is not blank'
            elsif matcher.try(:expects_to_allow_nil?)
              description_clauses << 'but only if it is not nil'
            end

            if matcher.try(:expects_strict?)
              description_clauses << 'raising a validation exception'

              if matcher.try(:expects_custom_validation_message?)
                description_clauses.last << ' with a custom message'
              end

              description_clauses.last << ' on failure'
            elsif matcher.try(:expects_custom_validation_message?)
              description_clauses <<
                'producing a custom validation error on failure'
            end

            description_clauses
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shoulda-matchers-3.1.3 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-3.1.2 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-3.1.1 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-3.1.0 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb