Sha256: 8f2a42f73720d02e248ea5eb222a34037efda17122167c9c5263c30d52eb90ab

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

module Shoulda
  module Matchers
    module ActiveRecord
      module AssociationMatchers
        # @private
        class RequiredMatcher
          attr_reader :missing_option

          def initialize(attribute_name, required)
            @attribute_name = attribute_name
            @required = required
            @submatcher = ActiveModel::DisallowValueMatcher.new(nil).
              for(attribute_name).
              with_message(validation_message_key)
            @missing_option = ''
          end

          def description
            "required: #{required}"
          end

          def matches?(subject)
            if submatcher_passes?(subject)
              true
            else
              @missing_option = 'and for the record '

              missing_option <<
                if required
                  'to '
                else
                  'not to '
                end

              missing_option << (
                'fail validation if ' +
                ":#{attribute_name} is unset; i.e., either the association " +
                'should have been defined with `required: ' +
                "#{required.inspect}`, or there "
              )

              missing_option <<
                if required
                  'should '
                else
                  'should not '
                end

              missing_option << "be a presence validation on :#{attribute_name}"

              false
            end
          end

          private

          attr_reader :attribute_name, :required, :submatcher

          def submatcher_passes?(subject)
            if required
              submatcher.matches?(subject)
            else
              submatcher.does_not_match?(subject)
            end
          end

          def validation_message_key
            RailsShim.validation_message_key_for_association_required_option
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shoulda-matchers-4.4.1 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.4.0 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.3.0 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.2.0 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.1.2 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.1.1 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.1.0 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
shoulda-matchers-4.0.1 lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb