class Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher

@private

Public Class Methods

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

Public Instance Methods

does_not_match?(subject) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 91
def does_not_match?(subject)
  super(subject)
  allows_value_of(value, @expected_message)
end
matches?(subject) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 86
def matches?(subject)
  super(subject)
  disallows_value_of(value, @expected_message)
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 96
def simple_description
  "validate that :#{@attribute} is empty/falsy"
end

Private Instance Methods

array_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 145
def array_column?
  @subject.class.respond_to?(:columns_hash) &&
    @subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
    @subject.class.columns_hash[@attribute.to_s].array
end
collection?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 132
def collection?
  if reflection
    [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
  else
    false
  end
end
column_type() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 126
def column_type
  @subject.class.respond_to?(:columns_hash) &&
    @subject.class.columns_hash[@attribute.to_s].respond_to?(:type) &&
    @subject.class.columns_hash[@attribute.to_s].type
end
enum_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 151
def enum_column?
  @subject.class.respond_to?(:defined_enums) &&
    @subject.class.defined_enums.key?(@attribute.to_s)
end
enum_values() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 156
def enum_values
  @subject.class.defined_enums[@attribute.to_s].values
end
reflection() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 140
def reflection
  @subject.class.respond_to?(:reflect_on_association) &&
    @subject.class.reflect_on_association(@attribute)
end
value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb, line 102
def value
  if reflection
    obj = reflection.klass.new
    if collection?
      [obj]
    else
      obj
    end
  elsif array_column?
    ['an arbitary value']
  elsif enum_column?
    enum_values.first
  else
    case column_type
    when :integer, :float then 1
    when :decimal then BigDecimal(1, 0)
    when :datetime, :time, :timestamp then Time.current
    when :date then Date.new
    when :binary then '0'
    else 'an arbitrary value'
    end
  end
end