class Shoulda::Matchers::ActiveRecord::AssociationMatchers::RequiredMatcher

@private

Attributes

attribute_name[R]
missing_option[R]
required[R]
submatcher[R]

Public Class Methods

new(attribute_name, required) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb, line 9
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

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb, line 18
def description
  "required: #{required}"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb, line 22
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 Instance Methods

submatcher_passes?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb, line 59
def submatcher_passes?(subject)
  if required
    submatcher.matches?(subject)
  else
    submatcher.does_not_match?(subject)
  end
end
validation_message_key() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb, line 67
def validation_message_key
  :required
end