aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2010-05-11 15:36:09 +0200
committerCarl Lerche <carllerche@mac.com>2010-05-13 13:57:37 -0700
commit92160219a8bf17bd435252304319c094e56e5849 (patch)
treed6768948321481bcea35b8d06cba82b5f528d9ac
parent9131a88bb8e82f139ec49b4057fb6065ba0a2c6a (diff)
downloadrails-92160219a8bf17bd435252304319c094e56e5849.tar.gz
rails-92160219a8bf17bd435252304319c094e56e5849.tar.bz2
rails-92160219a8bf17bd435252304319c094e56e5849.zip
minor changes to instance level validations implementation based on feedback from José Valim
-rw-r--r--activemodel/lib/active_model/validations.rb3
-rw-r--r--activemodel/lib/active_model/validations/helper_methods.rb12
-rw-r--r--activemodel/lib/active_model/validations/with.rb37
-rw-r--r--activemodel/lib/active_model/validator.rb3
4 files changed, 17 insertions, 38 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 1e2901633e..f472f50f9b 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -47,7 +47,8 @@ module ActiveModel
included do
extend ActiveModel::Translation
- extend HelperMethods; include HelperMethods
+ extend HelperMethods
+ include HelperMethods
define_callbacks :validate, :scope => :name
diff --git a/activemodel/lib/active_model/validations/helper_methods.rb b/activemodel/lib/active_model/validations/helper_methods.rb
deleted file mode 100644
index 4c709b1fa9..0000000000
--- a/activemodel/lib/active_model/validations/helper_methods.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-module ActiveModel
- module Validations
- module HelperMethods
- private
-
- def _merge_attributes(attr_names)
- options = attr_names.extract_options!
- options.merge(:attributes => attr_names.flatten)
- end
- end
- end
-end \ No newline at end of file
diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb
index 2a2d0d55b4..6dbde5bfad 100644
--- a/activemodel/lib/active_model/validations/with.rb
+++ b/activemodel/lib/active_model/validations/with.rb
@@ -1,5 +1,13 @@
module ActiveModel
module Validations
+ module HelperMethods
+ private
+ def _merge_attributes(attr_names)
+ options = attr_names.extract_options!
+ options.merge(:attributes => attr_names.flatten)
+ end
+ end
+
module ClassMethods
# Passes the record off to the class or classes specified and allows them
@@ -89,18 +97,8 @@ module ActiveModel
# end
# end
#
- # class MyValidator < ActiveModel::Validator
- # def validate(record)
- # if some_complex_logic
- # record.errors[:base] << "This record is invalid"
- # end
- # end
- #
- # private
- # def some_complex_logic
- # # ...
- # end
- # end
+ # Please consult the class method documentation for more information on
+ # creating your own validator.
#
# You may also pass it multiple classes, like so:
#
@@ -120,24 +118,13 @@ module ActiveModel
# in the callback
#
# If you pass any additional configuration options, they will be passed
- # to the class and available as <tt>options</tt>:
- #
- # class Person
- # include ActiveModel::Validations
- # validates_with MyValidator, :my_custom_key => "my custom value"
- # end
- #
- # class MyValidator < ActiveModel::Validator
- # def validate(record)
- # options[:my_custom_key] # => "my custom value"
- # end
- # end
+ # to the class and available as <tt>options</tt>, please refer to the
+ # class version of this method for more information
#
def validates_with(*args, &block)
options = args.extract_options!
args.each do |klass|
validator = klass.new(options, &block)
- validator.setup(self) if validator.respond_to?(:setup)
validator.validate(self)
end
end
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 906d239bcc..56179c1a6c 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -88,6 +88,9 @@ module ActiveModel #:nodoc:
# klass.send :attr_accessor, :custom_attribute
# end
# end
+ #
+ # This setup method is only called when used with validation macros or the
+ # class level <tt>validates_with</tt> method.
#
class Validator
attr_reader :options