From 4f9b59dba0fe9d90f53417f0d4f1bc63679d556a Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Tue, 2 Oct 2012 21:45:09 -0400 Subject: Make `.validators_on` accept `:kind` option This will filter out the validators on a particular attribute based on its kind. --- activemodel/lib/active_model/validations.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 4762f39044..5a3225a7e6 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -187,10 +187,21 @@ module ActiveModel # # #, # # #0..99}> # # ] + # + # You can also pass a +:kind+ option to filter the validators based on their kind. + # + # Person.validators_on(:name, kind: :presence) + # # => [#] def validators_on(*attributes) + options = attributes.extract_options! + attributes.map do |attribute| _validators[attribute.to_sym] - end.flatten + end.flatten.tap do |validators| + if options[:kind] + validators.select! { |validator| validator.kind == options[:kind] } + end + end end # Returns +true+ if +attribute+ is an attribute method, +false+ otherwise. -- cgit v1.2.3