This project is archived and is in readonly mode.
Adding a validates_with method
-
Pratik
- Tag changed from active_record, doc, patch, validations to active_record, patch, validations
-
CancelProfileIsBroken
- Tag changed from active_record, patch, validations to active_record, bugmash, patch, validations
-
Derander
+1
This would be useful in some of my applications. We have a huge number of models that all use essentially the same sets of validations, which are currently wrapped into modules. This would allow us to extract the "should I run this validation" logic into the model which is being validation more easily.
I've read the source completely, and it seems reasonable and consistent with the way the other validations are implemented.
The tests pass on 2-3-stable
-
Josh Sharpe
Cool idea. It would make sense for Observers and Validators to function the same way, since they both have the same idea of moving functionality out of the base class... As it stands now, Observers have to be explicitly defined in environment.rb for them to function. Based on the description in this post, this doesn't seem to be the case with Validators.
See Configuration here:
http://api.rubyonrails.org/classes/ActiveRecord/Observer.html
-
Matt Duncan
+1
Pluggable validations and validation plugins would be really nice.
Passes all tests on 2-3-stable.
-
Michael Koziarski
- Assigned user set to Michael Koziarski
- Milestone cleared.
Hey Jeff,
I like this but only for 3.0 (bit too new a feature for a point release).
Can you rebase it for master and re-upload. I'll apply it after that.
Josh, the only reason observers need to be defined in env.rb is that they're 'magic' so CustomerObserver automatically observes 'Customer'. Given we have a declaration here, it's fine.
-
Jeff Dean
Thanks! I've rebased from master and uploaded the new patch.
There are 3 things to note about this patch:
-
It was a non-trivial move, since validations are now in ActiveModel. I moved the code and tests to match the style of ActiveModel.
-
ActiveModel tests seem to cover the ability to send validations based on a certain key (:create, :update), and ActiveRecord tests seem to cover the fact that they send the correct key to the validations. In the original patch I put tests in to test this explicitly, but in this patch I removed the 2 test methods because they seemed redundant.
-
The validation guide is no longer up to date with the new syntax for errors.add_to_base => errors[:base] etc... I can fix the guide and add validates_with documentation, but I'll send that as a separate patch to the guides project. I've adjusted all of the RDoc comments to reflect the new style of validations.
Also, I had some mysterious test breakages on ActiveModel in master. CI was broken for other reasons, so can't say whether it was local or not. After looking into both of the failures, I can't imagine how any side effect of this patch would affect these tests, but since they were red for me to begin with, I can't say for sure. Here's the test output just for the record:
1) Failure: test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records(BelongsToAssociationsTest) [./test/cases/associations/belongs_to_associations_test.rb:417:in `test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records' ./test/cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__' ./test/cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run']: <nil> expected but was <"">. 2) Failure: test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added(SchemaDumperTest) [./test/cases/schema_dumper_test.rb:214:in `test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added' ./test/cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__' ./test/cases/../../lib/../../activemodel/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run']: non-primary key id column not preserved. <" t.string \"id\", :default => \"\", :null => false"> expected to be =~ </t.string[[:space:]]+"id",[[:space:]]+:null => false$/>. -
-
Jeff Dean
@josh - I based this patch off of the existing callback code, which allows you to do:
class Foo < ActiveRecord::Base after_save MyCallbackClass.new endThat code only exists, as far as I know, to support observers. It has the pleasant side effect that you can move complex callback code to a different class, and I use it all the time outside of observers.
Then there's another layer of observers which hooks these external classes up automatically. I've never felt the need to go to that level of abstraction with validations, so I didn't add it to this feature. At the very least, I see them as separate features.
-
Rizwan Reza
verified
+1 This patch works cleanly on master. Safe to commit.
-
Elad Meidar
+1 verified and applied on 2-3-stable and master. +1 on idea too, i have been doing those dependent validations for a while too, trying to find a better way.. good job!
-
Josh Nichols
+1, great idea which would make it a lot easier to build re-usable and testable validations.
Also verified it applies to master cleanly, and the tests pass.
-
Repository
- State changed from new to committed
(from [22f339825329e2d4463a4130e9fa68baf9d27eb6]) Introduce validates_with to encapsulate attribute validations in a class.
[#2630 Adding a validates_with method state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/22f339825329e2d4463a4130e9fa68... -
Jeremy Kemper
- Assigned user changed from Michael Koziarski to josh
- Tag changed from active_record, bugmash, patch, validations to active_record, patch, validations
