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, #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.



183
184
185
186
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 183

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

Instance Method Details

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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_reasonObject



233
234
235
236
237
238
239
240
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 233

def failure_reason
  reason = super
  if should_add_footnote_about_belongs_to?
    "#{reason}\n\n#{belongs_to_footnote}"
  else
    reason
  end
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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



229
230
231
# File 'lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb', line 229

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