Class: Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher

Inherits:
ValidationMatcher show all
Includes:
Qualifiers::AllowNil
Defined in:
lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods included from Qualifiers::AllowNil

#allow_nil

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #failure_message_when_negated, #on, #strict, #with_message

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidatePresenceOfMatcher

Returns a new instance of ValidatePresenceOfMatcher.



137
138
139
140
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 137

def initialize(attribute)
  super
  @expected_message = :blank
end

Instance Method Details

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 161

def does_not_match?(subject)
  super(subject)

  possibly_ignore_interference_by_writer

  if secure_password_being_validated?
    ignore_interference_by_writer.default_to(when: :blank?)

    disallowed_values.any? do |value|
      allows_and_double_checks_value_of!(value)
    end
  else
    (expects_to_allow_nil? && disallows_value_of(nil)) ||
      disallowed_values.any? do |value|
        allows_original_or_typecast_value?(value)
      end
  end
end

#failure_messageObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 184

def failure_message
  message = super

  if should_add_footnote_about_belongs_to?
    message << "\n\n"
    message << Shoulda::Matchers.word_wrap(<<-MESSAGE.strip, indent: 2)
You're getting this error because #{reason_for_existing_presence_validation}.
*This* presence validation doesn't use "can't be blank", the usual validation
message, but "must exist" instead.

With that said, did you know that the `belong_to` matcher can test this
validation for you? Instead of using `validate_presence_of`, try
#{suggestions_for_belongs_to}
    MESSAGE
  end

  message
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 142

def matches?(subject)
  super(subject)

  possibly_ignore_interference_by_writer

  if secure_password_being_validated?
    ignore_interference_by_writer.default_to(when: :blank?)

    disallowed_values.all? do |value|
      disallows_and_double_checks_value_of!(value)
    end
  else
    (!expects_to_allow_nil? || allows_value_of(nil)) &&
      disallowed_values.all? do |value|
        disallows_original_or_typecast_value?(value)
      end
  end
end

#simple_descriptionObject



180
181
182
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 180

def simple_description
  "validate that :#{@attribute} cannot be empty/falsy"
end