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

Inherits:
ValidationMatcher show all
Includes:
Qualifiers::AllowBlank, 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::AllowBlank

#allow_blank

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.



159
160
161
162
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 159

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

Instance Method Details

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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)) ||
      (expects_to_allow_blank? && disallows_value_of('')) ||
      disallowed_values.any? do |value|
        allows_original_or_typecast_value?(value)
      end
  end
end

#failure_messageObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 209

def failure_message
  message = super

  if should_add_footnote_about_belongs_to?
    message << "\n\n"
    message << Shoulda::Matchers.word_wrap("You're getting this error because \#{reason_for_existing_presence_validation}.\n*This* presence validation doesn't use \"can't be blank\", the usual validation\nmessage, but \"must exist\" instead.\n\nWith that said, did you know that the `belong_to` matcher can test this\nvalidation for you? Instead of using `validate_presence_of`, try\n\#{suggestions_for_belongs_to}\n    MESSAGE\n  end\n\n  message\nend\n".strip, indent: 2)

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 164

def matches?(subject)
  super(subject)

  possibly_ignore_interference_by_writer

  if secure_password_being_validated? &&
     Shoulda::Matchers::RailsShim.active_model_lt_7?
    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)) &&
      (!expects_to_allow_blank? || allows_value_of('')) &&
      disallowed_values.all? do |value|
        disallows_original_or_typecast_value?(value)
      end
  end
end

#simple_descriptionObject



205
206
207
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 205

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