Class: Shoulda::Matchers::ActiveModel::AllowValueMatcher

Inherits:
Object
  • Object
show all
Includes:
Helpers, Qualifiers::IgnoringInterferenceByWriter
Defined in:
lib/shoulda/matchers/active_model/allow_value_matcher.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/successful_check.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/successful_setting.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_does_not_exist_error.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator.rb,
lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb

Defined Under Namespace

Classes: AttributeChangedValueError, AttributeDoesNotExistError, AttributeSetter, AttributeSetterAndValidator, AttributeSetters, AttributeSettersAndValidators, SuccessfulCheck, SuccessfulSetting

Instance Attribute Summary collapse

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Methods included from Helpers

#default_error_message, #format_validation_errors, #pretty_error_messages

Constructor Details

#initialize(*values) ⇒ AllowValueMatcher

Returns a new instance of AllowValueMatcher.



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

def initialize(*values)
  super
  @values_to_set = values
  @options = {}
  @after_setting_value_callback = -> {}
  @expects_strict = false
  @expects_custom_validation_message = false
  @context = nil
  @values_to_preset = {}
  @failure_message_preface = nil
  @attribute_changed_value_message = nil
end

Instance Attribute Details

#after_setting_value_callbackObject (readonly)

Returns the value of attribute after_setting_value_callback.



311
312
313
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 311

def after_setting_value_callback
  @after_setting_value_callback
end

#attribute_changed_value_message=(value) ⇒ Object

Sets the attribute attribute_changed_value_message

Parameters:

  • value

    the value to set the attribute attribute_changed_value_message to.



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

def attribute_changed_value_message=(value)
  @attribute_changed_value_message = value
end

#attribute_to_check_message_againstObject (readonly)

Returns the value of attribute attribute_to_check_message_against.



311
312
313
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 311

def attribute_to_check_message_against
  @attribute_to_check_message_against
end

#attribute_to_setObject (readonly)

Returns the value of attribute attribute_to_set.



311
312
313
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 311

def attribute_to_set
  @attribute_to_set
end

#contextObject (readonly)

Returns the value of attribute context.



311
312
313
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 311

def context
  @context
end

#failure_message_preface=(value) ⇒ Object

Sets the attribute failure_message_preface

Parameters:

  • value

    the value to set the attribute failure_message_preface to.



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

def failure_message_preface=(value)
  @failure_message_preface = value
end

Instance Method Details

#_after_setting_value(&callback) ⇒ Object



389
390
391
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 389

def _after_setting_value(&callback)
  @after_setting_value_callback = callback
end

#descriptionObject



502
503
504
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 502

def description
  ValidationMatcher::BuildDescription.call(self, simple_description)
end

#does_not_match?(instance) ⇒ Boolean

Returns:

  • (Boolean)


399
400
401
402
403
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 399

def does_not_match?(instance)
  @instance = instance
  @result = run(:first_passing)
  @result.nil?
end

#expected_messageObject



366
367
368
369
370
371
372
373
374
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 366

def expected_message
  if options.key?(:expected_message)
    if Symbol === options[:expected_message]
      default_expected_message
    else
      options[:expected_message]
    end
  end
end

#expects_custom_validation_message?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 376

def expects_custom_validation_message?
  @expects_custom_validation_message
end

#expects_strict?Boolean

Returns:

  • (Boolean)


385
386
387
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 385

def expects_strict?
  @expects_strict
end

#failure_messageObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 405

def failure_message
  attribute_setter = result.attribute_setter

  if result.attribute_setter.unsuccessfully_checked?
    message = attribute_setter.failure_message
  else
    validator = result.validator
    message = failure_message_preface.call
    message << ' valid, but it was invalid instead,'

    if validator.captured_validation_exception?
      message << ' raising a validation exception with the message '
      message << validator.validation_exception_message.inspect
      message << '.'
    else
      message << " producing these validation errors:\n\n"
      message << validator.all_formatted_validation_error_messages
    end
  end

  if include_attribute_changed_value_message?
    message << "\n\n#{attribute_changed_value_message.call}"
  end

  Shoulda::Matchers.word_wrap(message)
end

#failure_message_when_negatedObject

rubocop:disable Metrics/MethodLength



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 432

def failure_message_when_negated # rubocop:disable Metrics/MethodLength
  attribute_setter = result.attribute_setter

  if attribute_setter.unsuccessfully_checked?
    message = attribute_setter.failure_message
  else
    validator = result.validator
    message = "#{failure_message_preface.call} invalid"

    if validator.type_of_message_matched?
      if validator.has_messages?
        message << ' and to'

        if validator.captured_validation_exception? # rubocop:disable Metrics/BlockNesting
          message << ' raise a validation exception with message'
        else
          message << ' produce'

          message <<
            if expected_message.is_a?(Regexp) # rubocop:disable Metrics/BlockNesting
              ' a'
            else
              ' the'
            end

          message << ' validation error'
        end

        if expected_message.is_a?(Regexp) # rubocop:disable Metrics/BlockNesting
          message << ' matching '
          message << Shoulda::Matchers::Util.inspect_value(
            expected_message,
          )
        else
          message << " #{expected_message.inspect}"
        end

        unless validator.captured_validation_exception? # rubocop:disable Metrics/BlockNesting
          message << " on :#{attribute_to_check_message_against}"
        end

        message << '. The record was indeed invalid, but'

        if validator.captured_validation_exception? # rubocop:disable Metrics/BlockNesting
          message << ' the exception message was '
          message << validator.validation_exception_message.inspect
          message << ' instead.'
        else
          message << " it produced these validation errors instead:\n\n"
          message << validator.all_formatted_validation_error_messages
        end
      else
        message << ', but it was valid instead.'
      end
    elsif validator.captured_validation_exception?
      message << ' and to produce validation errors, but the record'
      message << ' raised a validation exception instead.'
    else
      message << ' and to raise a validation exception, but the record'
      message << ' produced validation errors instead.'
    end
  end

  if include_attribute_changed_value_message?
    message << "\n\n#{attribute_changed_value_message.call}"
  end

  Shoulda::Matchers.word_wrap(message)
end

#for(attribute_name) ⇒ Object



338
339
340
341
342
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 338

def for(attribute_name)
  @attribute_to_set = attribute_name
  @attribute_to_check_message_against = attribute_name
  self
end

#last_attribute_setter_usedObject



514
515
516
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 514

def last_attribute_setter_used
  result.attribute_setter
end

#last_value_setObject



518
519
520
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 518

def last_value_set
  last_attribute_setter_used.value_written
end

#matches?(instance) ⇒ Boolean

Returns:

  • (Boolean)


393
394
395
396
397
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 393

def matches?(instance)
  @instance = instance
  @result = run(:first_failing)
  @result.nil?
end

#modelObject



510
511
512
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 510

def model
  instance.class
end

#on(context) ⇒ Object



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

def on(context)
  if context.present?
    @context = context
  end

  self
end

#simple_descriptionObject



506
507
508
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 506

def simple_description
  "allow :#{attribute_to_set} to be #{inspected_values_to_set}"
end

#strict(expects_strict = true) ⇒ Object



380
381
382
383
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 380

def strict(expects_strict = true)
  @expects_strict = expects_strict
  self
end

#with_message(message, given_options = {}) ⇒ Object



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

def with_message(message, given_options = {})
  if message.present?
    @expects_custom_validation_message = true
    options[:expected_message] = message
    options[:expected_message_values] = given_options.fetch(:values, {})

    if given_options.key?(:against)
      @attribute_to_check_message_against = given_options[:against]
    end
  end

  self
end