class Shoulda::Matchers::ActiveModel::Validator

@private

Attributes

attribute[R]
context[R]
record[R]

Public Class Methods

new(record, attribute, options = {}) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 8
def initialize(record, attribute, options = {})
  @record = record
  @attribute = attribute
  @context = options[:context]
  @expects_strict = options[:expects_strict]
  @expected_message = options[:expected_message]

  @_validation_result = nil
  @captured_validation_exception = false
  @captured_range_error = false
end

Public Instance Methods

all_formatted_validation_error_messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 36
def all_formatted_validation_error_messages
  format_validation_errors(all_validation_errors)
end
call() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 20
def call
  !messages_match? && !captured_range_error?
end
captured_validation_exception?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 28
def captured_validation_exception?
  @captured_validation_exception
end
has_messages?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 24
def has_messages?
  messages.any?
end
type_of_message_matched?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 32
def type_of_message_matched?
  expects_strict? == captured_validation_exception?
end
validation_exception_message() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 40
def validation_exception_message
  validation_result[:validation_exception_message]
end

Private Instance Methods

all_validation_errors() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 80
def all_validation_errors
  validation_result[:all_validation_errors]
end
captured_range_error?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 76
def captured_range_error?
  !!@captured_range_error
end
expects_strict?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 50
def expects_strict?
  @expects_strict
end
matched_messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 68
def matched_messages
  if @expected_message
    messages.grep(@expected_message)
  else
    messages
  end
end
messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 60
def messages
  if expects_strict?
    [validation_exception_message]
  else
    validation_error_messages
  end
end
messages_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 54
def messages_match?
  has_messages? &&
    type_of_message_matched? &&
    matched_messages.compact.any?
end
perform_validation() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 92
def perform_validation
  if context
    record.valid?(context)
  else
    record.valid?
  end

  all_validation_errors = record.errors.dup

  validation_error_messages =
    if record.errors.respond_to?(:[])
      record.errors[attribute]
    else
      record.errors.on(attribute)
    end

  {
    all_validation_errors: all_validation_errors,
    validation_error_messages: validation_error_messages,
    validation_exception_message: nil,
  }
rescue ::ActiveModel::StrictValidationFailed => e
  @captured_validation_exception = true
  {
    all_validation_errors: nil,
    validation_error_messages: [],
    validation_exception_message: e.message,
  }
end
validation_error_messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 84
def validation_error_messages
  validation_result[:validation_error_messages]
end
validation_result() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 88
def validation_result
  @_validation_result ||= perform_validation
end