This project is archived and is in readonly mode.
validates_inclusion/exclusion_of should also accept a symbol or proc as the :in/:within option
Reported by shuber | August 20th, 2008 @ 08:26 PM | in 3.x
validates_inclusion_of and validates_exclusion_of should also be able to accept a symbol representing an instance method or a proc that returns an enumerable object as the :in or :within option.
Example
class Page < ActiveRecord::Base
acts_as_tree
validates_exclusion_of :parent, :in => :self_and_all_children
# or
validates_exclusion_of :parent_id, :in => Proc.new { |page| page.self_and_all_children.collect(&:id) }
def self_and_all_children
self.children.inject([self]) { |array, child| array += child.self_and_all_children }
end
end
Attached is a patch that adds this behavior to the validation methods in ActiveRecord and ActiveModel. I didn't see any tests for validations in ActiveModel, so I just added the tests for ActiveRecord.
Comments and changes to this ticket
-
Chris Cherry August 20th, 2008 @ 10:32 PM
+1
This seems like a very useful addition, in fact I'm kinda surprised its not there already. The example he gives above is a pretty good demonstration of where it could be applied, checking for the existence of the value of the parent_id attribute in a dynamic set.
Tests relating to this change pass.
-
Pratik August 21st, 2008 @ 11:25 AM
- State changed from new to incomplete
I like this as well. A few changes before it can be pushed :
- Missing ActiveModel tests
- Missing tests for validates_exclusion_of
- I don't like 'case enum.class.to_s'. It should just use 'case enum'
-
shuber August 21st, 2008 @ 06:13 PM
Updated patch - uses 'case enum' instead of 'case enum.class.to_s'
-
Dan Pickett May 9th, 2010 @ 05:44 PM
- Tag changed from activemodel, activerecord, edge, patch, tested, validations to activemodel, activerecord, bugmash, edge, patch, tested, validations
-
Dan Pickett May 9th, 2010 @ 05:44 PM
- no changes were found...
-
Santiago Pastorino February 2nd, 2011 @ 05:01 PM
- State changed from incomplete to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 05:01 PM
- State changed from open to stale
-
pmontrasio April 25th, 2011 @ 05:24 PM
- Tag cleared.
It seems that shuber's patch didn't make it into Rails 3. Too bad because I was looking for a way to validate against some dynamic value. I'll fall back to writing a custom validator but it takes more code to do that.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>