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

Merged
merged 1 commit into from Oct 3, 2012

Conversation

Projects
None yet
2 participants
Member

sikachu commented Oct 3, 2012

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
+ # => [#<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
+ # #<ActiveModel::Validations::InclusionValidator:0x007fe603bb8780 @attributes=[:age], @options={:in=>0..99}>]
+
+ Person.validators_on(:name, :presence)
@rafaelfranca

rafaelfranca Oct 3, 2012

Owner

Where is the :kind option?

@sikachu

sikachu Oct 3, 2012

Member

Busted.

Fixing now.

activemodel/lib/active_model/validations.rb
+ #
+ # You can also pass a +:kind+ option to filter the validators based on their kind.
+ #
+ # Person.validators_on(:name, :presence)
@rafaelfranca

rafaelfranca Oct 3, 2012

Owner

Where is the :kind option?

Make `.validators_on` accept `:kind` option
This will filter out the validators on a particular attribute based on
its kind.
Member

sikachu commented Oct 3, 2012

Pull request updated.

Owner

rafaelfranca commented Oct 3, 2012

This will be useful ❤️

rafaelfranca added a commit that referenced this pull request Oct 3, 2012

Merge pull request #7826 from sikachu/master-validators-kind
Make `.validators_on` accept `:kind` option

@rafaelfranca rafaelfranca merged commit 4e9f53f into rails:master Oct 3, 2012

rafaelfranca added a commit that referenced this pull request Oct 3, 2012

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment