class Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher

@private

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 130
def description
  "require #{@attribute} to be set"
end
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)
  @expected_message ||= :blank

  if secure_password_being_validated?
    disallows_and_double_checks_value_of!(blank_value, @expected_message)
  else
    disallows_value_of(blank_value, @expected_message)
  end
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 114
def with_message(message)
  @expected_message = message if message
  self
end

Private Instance Methods

blank_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 156
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 164
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 142
def disallows_and_double_checks_value_of!(value, message)
  error_class = Shoulda::Matchers::ActiveModel::CouldNotSetPasswordError

  disallows_value_of(value, message) do |matcher|
    matcher._after_setting_value do
      actual_value = @subject.__send__(@attribute)

      if !actual_value.nil?
        raise error_class.create(@subject.class)
      end
    end
  end
end
reflection() click to toggle source
# File lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb, line 172
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?
  defined?(::ActiveModel::SecurePassword) &&
    @subject.class.ancestors.include?(::ActiveModel::SecurePassword::InstanceMethodsOnActivation) &&
    @attribute == :password
end