Skip to content

Commit 8be20b0

Browse files
committed
Document ActiveModel#validation_context [skip ci]
Background: To run validations except in a certain context, we need to either 1. explicitly pass all validation contexts to the `on` option 2. access undocumented `validation_context` Option 1 is error-prone. Additionally, adding the opposite behavior of `on` was rejected rails#30710. Solution: Document `ActiveModel#validation_context` to make it clear that the API is public. Close rails#46391.
1 parent 656118b commit 8be20b0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ module Validations
4545
extend HelperMethods
4646
include HelperMethods
4747

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+
##
4867
attr_accessor :validation_context
4968
private :validation_context=
5069
define_callbacks :validate, scope: :name

0 commit comments

Comments
 (0)