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') 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 From 454d820bf0a18fe1db4c55b0145197d70fef1f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 2 Oct 2012 23:24:42 -0300 Subject: Don't use tap in this case. The use of tap in this case is very confusing since we are mutating the return value inside the block --- activemodel/lib/active_model/validations.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 5a3225a7e6..be780d570b 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -195,12 +195,14 @@ module ActiveModel def validators_on(*attributes) options = attributes.extract_options! - attributes.map do |attribute| + validators = attributes.map do |attribute| _validators[attribute.to_sym] - end.flatten.tap do |validators| - if options[:kind] - validators.select! { |validator| validator.kind == options[:kind] } - end + end.flatten + + if options[:kind] + validators.select! { |validator| validator.kind == options[:kind] } + else + validators end end -- cgit v1.2.3 From 86062005a73589dc4919cfac401ce061f061c6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 2 Oct 2012 23:56:12 -0300 Subject: Revert "Merge pull request #7826 from sikachu/master-validators-kind" This reverts commit 4e9f53f9736544f070e75e516c71137b7eb49a7a, reversing changes made to 6b802cdb4f5b84e1bf49aaeb0e994b3be6028af9. Revert "Don't use tap in this case." This reverts commit 454d820bf0a18fe1db4c55b0145197d70fef1f82. Reason: Is not a good idea to add options to this method since we can do the same thing using method composition. Person.validators_on(:name).select { |v| v.kind == :presence } Also it avoids to change the method again to add more options. --- activemodel/lib/active_model/validations.rb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index be780d570b..4762f39044 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -187,23 +187,10 @@ 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! - - validators = attributes.map do |attribute| + attributes.map do |attribute| _validators[attribute.to_sym] end.flatten - - if options[:kind] - validators.select! { |validator| validator.kind == options[:kind] } - else - validators - end end # Returns +true+ if +attribute+ is an attribute method, +false+ otherwise. -- cgit v1.2.3 From d56b5dacb1fbaeef9d48d57594b2f1f9c32a21bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 3 Oct 2012 00:17:10 -0300 Subject: Use the `flat_map` method. Thanks to @jeremy to teach me this one. --- activemodel/lib/active_model/validations.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 4762f39044..243d911f71 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -188,9 +188,9 @@ module ActiveModel # # #0..99}> # # ] def validators_on(*attributes) - attributes.map do |attribute| + attributes.flat_map do |attribute| _validators[attribute.to_sym] - end.flatten + end end # Returns +true+ if +attribute+ is an attribute method, +false+ otherwise. -- cgit v1.2.3