Class: Shoulda::Matchers::ActiveModel::AllowValueMatcher
- Inherits:
-
Object
- Object
- Shoulda::Matchers::ActiveModel::AllowValueMatcher
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
#ignore_interference_by_writer
Instance Method Summary
collapse
#ignoring_interference_by_writer
Methods included from Helpers
#default_error_message, #format_validation_errors, #pretty_error_messages
Constructor Details
Returns a new instance of AllowValueMatcher.
355
356
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 355
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_callback ⇒ Object
Returns the value of attribute after_setting_value_callback.
341
342
343
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 341
def after_setting_value_callback
@after_setting_value_callback
end
|
#attribute_changed_value_message=(value) ⇒ Object
Sets the attribute attribute_changed_value_message
349
350
351
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 349
def attribute_changed_value_message=(value)
@attribute_changed_value_message = value
end
|
#attribute_to_check_message_against ⇒ Object
Returns the value of attribute attribute_to_check_message_against.
341
342
343
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 341
def attribute_to_check_message_against
@attribute_to_check_message_against
end
|
#attribute_to_set ⇒ Object
Returns the value of attribute attribute_to_set.
341
342
343
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 341
def attribute_to_set
@attribute_to_set
end
|
#context ⇒ Object
Returns the value of attribute context.
341
342
343
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 341
def context
@context
end
|
#failure_message_preface=(value) ⇒ Object
Sets the attribute failure_message_preface
349
350
351
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 349
def failure_message_preface=(value)
@failure_message_preface = value
end
|
Instance Method Details
#_after_setting_value(&callback) ⇒ Object
425
426
427
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 425
def _after_setting_value(&callback)
@after_setting_value_callback = callback
end
|
#against(attribute) ⇒ Object
382
383
384
385
386
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 382
def against(attribute)
@attribute_to_check_message_against = attribute if attribute.present?
self
end
|
#does_not_match?(instance) ⇒ Boolean
435
436
437
438
439
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 435
def does_not_match?(instance)
@instance = instance
@result = run(:first_passing)
@result.nil?
end
|
#expected_message ⇒ Object
402
403
404
405
406
407
408
409
410
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 402
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
412
413
414
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 412
def expects_custom_validation_message?
@expects_custom_validation_message
end
|
#expects_strict? ⇒ Boolean
421
422
423
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 421
def expects_strict?
@expects_strict
end
|
#failure_message ⇒ Object
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
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 445
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_negated ⇒ Object
rubocop:disable Metrics/MethodLength
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 472
def failure_message_when_negated 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? message << ' raise a validation exception with message'
else
message << ' produce'
message <<
if expected_message.is_a?(Regexp) ' a'
else
' the'
end
message << ' validation error'
end
if expected_message.is_a?(Regexp) message << ' matching '
message << Shoulda::Matchers::Util.inspect_value(
expected_message,
)
else
message << " #{expected_message.inspect}"
end
unless validator.captured_validation_exception? message << " on :#{attribute_to_check_message_against}"
end
message << '. The record was indeed invalid, but'
if validator.captured_validation_exception? 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
elsif validator.has_any_errors?
message << ", placing a validation error on :#{attribute_setter.attribute_name}"
message << '. The Example was invalid,'
message << " but it had errors involving other attributes:\n\n"
message << validator.all_formatted_validation_error_messages
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
368
369
370
371
372
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 368
def for(attribute_name)
@attribute_to_set = attribute_name
@attribute_to_check_message_against = attribute_name
self
end
|
#has_any_errors? ⇒ Boolean
441
442
443
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 441
def has_any_errors?
validator.record.errors.any?
end
|
#last_attribute_setter_used ⇒ Object
559
560
561
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 559
def last_attribute_setter_used
result.attribute_setter
end
|
#last_value_set ⇒ Object
563
564
565
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 563
def last_value_set
last_attribute_setter_used.value_written
end
|
#matches?(instance) ⇒ Boolean
429
430
431
432
433
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 429
def matches?(instance)
@instance = instance
@result = run(:first_failing)
@result.nil?
end
|
#model ⇒ Object
555
556
557
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 555
def model
instance.class
end
|
#on(context) ⇒ Object
374
375
376
377
378
379
380
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 374
def on(context)
if context.present?
@context = context
end
self
end
|
#simple_description ⇒ Object
551
552
553
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 551
def simple_description
"allow :#{attribute_to_set} to be #{inspected_values_to_set}"
end
|
#strict(expects_strict = true) ⇒ Object
416
417
418
419
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 416
def strict(expects_strict = true)
@expects_strict = expects_strict
self
end
|
#with_message(message, given_options = {}) ⇒ Object
388
389
390
391
392
393
394
395
396
397
398
399
400
|
# File 'lib/shoulda/matchers/active_model/allow_value_matcher.rb', line 388
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
|