Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::RequiredMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, required) ⇒ RequiredMatcher

Returns a new instance of RequiredMatcher.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#missing_optionObject (readonly)

Returns the value of attribute missing_option.



7
8
9
# File 'lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb', line 7

def missing_option
  @missing_option
end

Instance Method Details

#descriptionObject



18
19
20
# File 'lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb', line 18

def description
  "required: #{required}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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