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 test string'.freeze
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.



291
292
293
294
295
296
297
298
299
300
# 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

Instance Method Details

#allow_nilObject



314
315
316
317
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 314

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

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

def expects_to_allow_nil?
  @options[:allow_nil]
end

#in_array(array) ⇒ Object



302
303
304
305
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 302

def in_array(array)
  @array = array
  self
end

#in_range(range) ⇒ Object



307
308
309
310
311
312
# 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) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



342
343
344
345
346
347
348
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 342

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

  self
end

#with_low_message(message) ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 333

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

  self
end

#with_message(message) ⇒ Object



323
324
325
326
327
328
329
330
331
# File 'lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb', line 323

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

  self
end