Class: Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb

Constant Summary collapse

BLANK_VALUES =
['', ' ', "\n", "\r", "\t", "\f"].freeze
ARBITRARY_OUTSIDE_STRING =
Shoulda::Matchers::ExampleClass.name
ARBITRARY_OUTSIDE_INTEGER =
123456789
ARBITRARY_OUTSIDE_DECIMAL =
BigDecimal('0.123456789')
ARBITRARY_OUTSIDE_DATE =
Date.jd(9999999)
ARBITRARY_OUTSIDE_DATETIME =
DateTime.jd(9999999)
ARBITRARY_OUTSIDE_TIME =
Time.at(9999999999)
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE =
<<EOT.freeze
You are using `validate_inclusion_of` to assert that a boolean column allows
boolean values and disallows non-boolean ones. Be aware that it is not possible
to fully test this, as boolean columns will automatically convert non-boolean
values to boolean ones. Hence, you should consider removing this test.
EOT
BOOLEAN_ALLOWS_NIL_MESSAGE =
<<EOT.freeze
You are using `validate_inclusion_of` to assert that a boolean column allows nil.
Be aware that it is not possible to fully test this, as anything other than
true, false or nil will be converted to false. Hence, you should consider
removing this test.
EOT

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateInclusionOfMatcher

Returns a new instance of ValidateInclusionOfMatcher.



294
295
296
297
298
299
300
301
302
303
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 294

def initialize(attribute)
  super(attribute)
  @options = {}
  @array = nil
  @range = nil
  @minimum = nil
  @maximum = nil
  @low_message = :inclusion
  @high_message = :inclusion
end

Instance Method Details

#allow_nilObject



317
318
319
320
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 317

def allow_nil
  @options[:allow_nil] = true
  self
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 386

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_nil?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 322

def expects_to_allow_nil?
  @options[:allow_nil]
end

#in_array(array) ⇒ Object



305
306
307
308
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 305

def in_array(array)
  @array = array
  self
end

#in_range(range) ⇒ Object



310
311
312
313
314
315
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 310

def in_range(range)
  @range = range
  @minimum = minimum_range_value
  @maximum = maximum_range_value
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 371

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_descriptionObject



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 353

def simple_description
  if @range
    "validate that :#{@attribute} lies inside the range " +
      Shoulda::Matchers::Util.inspect_range(@range)
  else
    description = "validate that :#{@attribute}"

    description <<
      if @array.count > 1
        " is either #{inspected_array}"
      else
        " is #{inspected_array}"
      end

    description
  end
end

#with_high_message(message) ⇒ Object



345
346
347
348
349
350
351
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 345

def with_high_message(message)
  if message
    @high_message = message
  end

  self
end

#with_low_message(message) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 336

def with_low_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
  end

  self
end

#with_message(message) ⇒ Object



326
327
328
329
330
331
332
333
334
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 326

def with_message(message)
  if message
    @expects_custom_validation_message = true
    @low_message = message
    @high_message = message
  end

  self
end