class Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher

@private

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 114
def initialize(attribute)
  super
  @expected_message = :blank
end

Public Instance Methods

matches?(subject) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 119
def matches?(subject)
  super(subject)

  if secure_password_being_validated?
    ignore_interference_by_writer.default_to(when: :blank?)
    disallows_and_double_checks_value_of!(blank_value, @expected_message)
  else
    disallows_original_or_typecast_value?(blank_value, @expected_message)
  end
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 130
def simple_description
  "validate that :#{@attribute} cannot be empty/falsy"
end

Private Instance Methods

blank_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 151
def blank_value
  if collection?
    []
  else
    nil
  end
end
collection?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 159
def collection?
  if reflection
    [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
  else
    false
  end
end
disallows_and_double_checks_value_of!(value, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 141
def disallows_and_double_checks_value_of!(value, message)
  disallows_value_of(value, message)
rescue ActiveModel::AllowValueMatcher::AttributeChangedValueError
  raise ActiveModel::CouldNotSetPasswordError.create(@subject.class)
end
disallows_original_or_typecast_value?(value, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 147
def disallows_original_or_typecast_value?(value, message)
  disallows_value_of(blank_value, @expected_message)
end
reflection() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 167
def reflection
  @subject.class.respond_to?(:reflect_on_association) &&
    @subject.class.reflect_on_association(@attribute)
end
secure_password_being_validated?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 136
def secure_password_being_validated?
  Shoulda::Matchers::RailsShim.digestible_attributes_in(@subject).
    include?(@attribute)
end