class Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher

@private

Constants

ARBITRARY_OUTSIDE_DATE
ARBITRARY_OUTSIDE_DATETIME
ARBITRARY_OUTSIDE_DECIMAL
ARBITRARY_OUTSIDE_INTEGER
ARBITRARY_OUTSIDE_STRING
ARBITRARY_OUTSIDE_TIME
BLANK_VALUES
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
BOOLEAN_ALLOWS_NIL_MESSAGE

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 291
def initialize(attribute)
  super(attribute)
  @options = {}
  @array = nil
  @range = nil
  @minimum = nil
  @maximum = nil
  @low_message = :inclusion
  @high_message = :inclusion
end

Public Instance Methods

allow_blank() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 314
def allow_blank
  @options[:allow_blank] = true
  self
end
allow_nil() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 323
def allow_nil
  @options[:allow_nil] = true
  self
end
does_not_match?(subject) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 391
def does_not_match?(subject)
  super(subject)

  if @range
    does_not_match_for_range?
  elsif @array
    if does_not_match_for_array?
      true
    else
      @failure_message = "#{@array} matches array in validation"
      false
    end
  end
end
expects_to_allow_blank?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 319
def expects_to_allow_blank?
  @options[:allow_blank]
end
expects_to_allow_nil?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 328
def expects_to_allow_nil?
  @options[:allow_nil]
end
in_array(array) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 302
def in_array(array)
  @array = array
  self
end
in_range(range) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 307
def in_range(range)
  @range = range
  @minimum = range.first
  @maximum = range.max
  self
end
matches?(subject) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 376
def matches?(subject)
  super(subject)

  if @range
    matches_for_range?
  elsif @array
    if matches_for_array?
      true
    else
      @failure_message = "#{@array} doesn't match array in validation"
      false
    end
  end
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 359
def simple_description
  if @range
    "validate that :#{@attribute} lies inside the range " +
      Shoulda::Matchers::Util.inspect_range(@range)
  else
    description = "validate that :#{@attribute}"

    if @array.many?
      description << " is either #{inspected_array}"
    else
      description << " is #{inspected_array}"
    end

    description
  end
end
with_high_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 351
def with_high_message(message)
  if message
    @high_message = message
  end

  self
end
with_low_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 342
def with_low_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
  end

  self
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 332
def with_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
    @high_message = message
  end

  self
end

Private Instance Methods

allows_all_values_in_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 472
def allows_all_values_in_array?
  @array.all? do |value|
    allows_value_of(value, @low_message)
  end
end
allows_any_value_outside_of_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 484
def allows_any_value_outside_of_array?
  if attribute_type == :boolean
    case @array
    when [false, true], [true, false]
      Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
      return true
    when [nil]
      if attribute_column.null
        Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE
        return true
      else
        raise NonNullableBooleanError.create(@attribute)
      end
    end
  end

  values_outside_of_array.any? do |value|
    allows_value_of(value, @low_message)
  end
end
allows_blank_value?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 619
def allows_blank_value?
  @options[:allow_blank] != true ||
    BLANK_VALUES.all? { |value| allows_value_of(value) }
end
allows_higher_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 464
def allows_higher_value
  allows_value_of(@maximum + 1, @high_message)
end
allows_lower_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 436
def allows_lower_value
  @minimum &&
    @minimum != 0 &&
    allows_value_of(@minimum - 1, @low_message)
end
allows_maximum_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 456
def allows_maximum_value
  allows_value_of(@maximum, @high_message)
end
allows_minimum_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 448
def allows_minimum_value
  allows_value_of(@minimum, @low_message)
end
allows_nil_value?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 611
def allows_nil_value?
  @options[:allow_nil] != true || allows_value_of(nil)
end
attribute_allows_nil?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 577
def attribute_allows_nil?
  if attribute_column
    attribute_column.null
  else
    true
  end
end
attribute_column() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 585
def attribute_column
  if @subject.class.respond_to?(:columns_hash)
    @subject.class.columns_hash[@attribute.to_s]
  end
end
attribute_type() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 569
def attribute_type
  if attribute_column
    column_type_to_attribute_type(attribute_column.type)
  else
    value_to_attribute_type(@subject.__send__(@attribute))
  end
end
boolean_outside_values() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 553
def boolean_outside_values
  values = []

  values << case @array
    when [true]  then false
    when [false] then true
    else              raise CouldNotDetermineValueOutsideOfArray
  end

  if attribute_allows_nil?
    values << nil
  end

  values
end
column_type_to_attribute_type(type) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 591
def column_type_to_attribute_type(type)
  case type
    when :float then :integer
    when :timestamp then :datetime
    else type
  end
end
disallows_all_values_outside_of_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 505
def disallows_all_values_outside_of_array?
  if attribute_type == :boolean
    case @array
    when [false, true], [true, false]
      Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
      return true
    when [nil]
      if attribute_column.null
        Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE
        return true
      else
        raise NonNullableBooleanError.create(@attribute)
      end
    end
  end

  values_outside_of_array.all? do |value|
    disallows_value_of(value, @low_message)
  end
end
disallows_any_values_in_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 478
def disallows_any_values_in_array?
  @array.any? do |value|
    disallows_value_of(value, @low_message)
  end
end
disallows_blank_value?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 624
def disallows_blank_value?
  @options[:allow_blank] &&
    BLANK_VALUES.any? { |value| disallows_value_of(value) }
end
disallows_higher_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 468
def disallows_higher_value
  disallows_value_of(@maximum + 1, @high_message)
end
disallows_lower_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 442
def disallows_lower_value
  @minimum.nil? ||
    @minimum == 0 ||
    disallows_value_of(@minimum - 1, @low_message)
end
disallows_maximum_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 460
def disallows_maximum_value
  disallows_value_of(@maximum, @high_message)
end
disallows_minimum_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 452
def disallows_minimum_value
  disallows_value_of(@minimum, @low_message)
end
disallows_nil_value?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 615
def disallows_nil_value?
  @options[:allow_nil] && disallows_value_of(nil)
end
does_not_match_for_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 429
def does_not_match_for_array?
  disallows_any_values_in_array? ||
    allows_any_value_outside_of_array? ||
    disallows_nil_value? ||
    disallows_blank_value?
end
does_not_match_for_range?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 415
def does_not_match_for_range?
  allows_lower_value ||
    disallows_minimum_value ||
    disallows_maximum_value ||
    allows_higher_value
end
inspected_array() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 629
def inspected_array
  Shoulda::Matchers::Util.inspect_values(@array).to_sentence(
    two_words_connector: " or ",
    last_word_connector: ", or "
  )
end
matches_for_array?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 422
def matches_for_array?
  allows_all_values_in_array? &&
    disallows_all_values_outside_of_array? &&
    allows_nil_value? &&
    allows_blank_value?
end
matches_for_range?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 408
def matches_for_range?
  disallows_lower_value &&
    allows_minimum_value &&
    allows_maximum_value &&
    disallows_higher_value
end
outside_values() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 534
def outside_values
  case attribute_type
  when :boolean
    boolean_outside_values
  when :integer
    [ARBITRARY_OUTSIDE_INTEGER]
  when :decimal
    [ARBITRARY_OUTSIDE_DECIMAL]
  when :date
    [ARBITRARY_OUTSIDE_DATE]
  when :datetime
    [ARBITRARY_OUTSIDE_DATETIME]
  when :time
    [ARBITRARY_OUTSIDE_TIME]
  else
    [ARBITRARY_OUTSIDE_STRING]
  end
end
value_to_attribute_type(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 599
def value_to_attribute_type(value)
  case value
    when true, false then :boolean
    when BigDecimal then :decimal
    when Integer then :integer
    when Date then :date
    when DateTime then :datetime
    when Time then :time
    else :unknown
  end
end
values_outside_of_array() click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 526
def values_outside_of_array
  if !(@array & outside_values).empty?
    raise CouldNotDetermineValueOutsideOfArray
  else
    outside_values
  end
end