Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make `.validators_on` accept `:kind` option #7826

Merged
merged 1 commit into from

2 participants

@sikachu
Collaborator

Making ActiveModel::Validators#validators_on to accept a :kind option which will filter out the validators on a particular attribute based on its kind.

Person.validators_on(:name)
# => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
# #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>]

Person.validators_on(:name, :presence)
# => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>]
activemodel/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* `ActiveModel::Validators#validators_on` now accepts a `:kind` option which will filter out the
+ validators on a particular attribute based on its kind.
+
+ Person.validators_on(:name)
+ # => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
+ # #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>]
+
+ Person.validators_on(:name, :presence)
@rafaelfranca Owner

Where is the :kind option?

@sikachu Collaborator
sikachu added a note

Busted.

Fixing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/lib/active_model/validations.rb
@@ -187,10 +187,21 @@ def validators
# # #<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
# # #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>
# # ]
+ #
+ # You can also pass a +:kind+ option to filter the validators based on their kind.
+ #
+ # Person.validators_on(:name, :presence)
@rafaelfranca Owner

Where is the :kind option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sikachu sikachu Make `.validators_on` accept `:kind` option
This will filter out the validators on a particular attribute based on
its kind.
4f9b59d
@sikachu
Collaborator

Pull request updated.

@rafaelfranca
Owner

This will be useful :heart:

@rafaelfranca rafaelfranca merged commit 4e9f53f into rails:master
@rafaelfranca rafaelfranca referenced this pull request from a commit
@rafaelfranca rafaelfranca Revert "Merge pull request #7826 from sikachu/master-validators-kind"
This reverts commit 4e9f53f, reversing
changes made to 6b802cd.

Revert "Don't use tap in this case."

This reverts commit 454d820.

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.
8606200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 3, 2012
  1. @sikachu

    Make `.validators_on` accept `:kind` option

    sikachu authored
    This will filter out the validators on a particular attribute based on
    its kind.
This page is out of date. Refresh to see the latest.
View
10 activemodel/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##
+* `ActiveModel::Validators#validators_on` now accepts a `:kind` option which will filter out the
+ validators on a particular attribute based on its kind.
+
+ Person.validators_on(:name)
+ # => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
+ # #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>]
+
+ Person.validators_on(:name, kind: :presence)
+ # => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>]
+
* Add `ActiveModel::ForbiddenAttributesProtection`, a simple module to
protect attributes from mass assignment when non-permitted attributes are passed.
View
13 activemodel/lib/active_model/validations.rb
@@ -187,10 +187,21 @@ def validators
# # #<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
# # #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>
# # ]
+ #
+ # You can also pass a +:kind+ option to filter the validators based on their kind.
+ #
+ # Person.validators_on(:name, kind: :presence)
+ # # => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>]
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.
View
8 activemodel/test/cases/validations_test.rb
@@ -287,6 +287,14 @@ def test_list_of_validators_will_be_empty_when_empty
assert_equal [], Topic.validators_on(:author_name)
end
+ def test_list_of_validators_on_an_attribute_based_on_kind
+ Topic.validates_presence_of :title, :content
+ Topic.validates_length_of :title, :minimum => 2
+
+ assert_equal Topic.validators_on(:title).select { |v| v.kind == :presence },
+ Topic.validators_on(:title, kind: :presence)
+ end
+
def test_validations_on_the_instance_level
auto = Automobile.new
Something went wrong with that request. Please try again.