@@ -45,27 +45,6 @@ module Validations
45
45
extend HelperMethods
46
46
include HelperMethods
47
47
48
- ##
49
- # :method: validation_context
50
- # Returns the context when running validations.
51
- #
52
- # This is useful when running validations except a certain context (opposite to the +on+ option).
53
- #
54
- # class Person
55
- # include ActiveModel::Validations
56
- #
57
- # attr_accessor :name
58
- # validates :name, presence: true, if: -> { validation_context != :custom }
59
- # end
60
- #
61
- # person = Person.new
62
- # person.valid? #=> false
63
- # person.valid?(:new) #=> false
64
- # person.valid?(:custom) #=> true
65
-
66
- ##
67
- attr_accessor :validation_context
68
- private :validation_context=
69
48
define_callbacks :validate , scope : :name
70
49
71
50
class_attribute :_validators , instance_writer : false , default : Hash . new { |h , k | h [ k ] = [ ] }
@@ -361,15 +340,23 @@ def errors
361
340
# person.valid? # => true
362
341
# person.valid?(:new) # => false
363
342
def valid? ( context = nil )
364
- current_context , self . validation_context = validation_context , context
343
+ current_context = validation_context
344
+ context_for_validation . context = context
365
345
errors . clear
366
346
run_validations!
367
347
ensure
368
- self . validation_context = current_context
348
+ context_for_validation . context = current_context
369
349
end
370
350
371
351
alias_method :validate , :valid?
372
352
353
+ def freeze
354
+ errors
355
+ context_for_validation
356
+
357
+ super
358
+ end
359
+
373
360
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
374
361
# added, +false+ otherwise.
375
362
#
@@ -430,7 +417,34 @@ def validate!(context = nil)
430
417
# end
431
418
alias :read_attribute_for_validation :send
432
419
420
+ # Returns the context when running validations.
421
+ #
422
+ # This is useful when running validations except a certain context (opposite to the +on+ option).
423
+ #
424
+ # class Person
425
+ # include ActiveModel::Validations
426
+ #
427
+ # attr_accessor :name
428
+ # validates :name, presence: true, if: -> { validation_context != :custom }
429
+ # end
430
+ #
431
+ # person = Person.new
432
+ # person.valid? #=> false
433
+ # person.valid?(:new) #=> false
434
+ # person.valid?(:custom) #=> true
435
+ def validation_context
436
+ context_for_validation . context
437
+ end
438
+
433
439
private
440
+ def validation_context = ( context )
441
+ context_for_validation . context = context
442
+ end
443
+
444
+ def context_for_validation
445
+ @context_for_validation ||= ValidationContext . new
446
+ end
447
+
434
448
def init_internals
435
449
super
436
450
@errors = nil
@@ -466,6 +480,10 @@ def initialize(model)
466
480
super ( I18n . t ( :"#{ @model . class . i18n_scope } .errors.messages.model_invalid" , errors : errors , default : :"errors.messages.model_invalid" ) )
467
481
end
468
482
end
483
+
484
+ class ValidationContext # :nodoc:
485
+ attr_accessor :context
486
+ end
469
487
end
470
488
471
489
Dir [ File . expand_path ( "validations/*.rb" , __dir__ ) ] . each { |file | require file }
0 commit comments