Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

minor changes to instance level validations implementation based on f…

…eedback from José Valim
  • Loading branch information...
commit 92160219a8bf17bd435252304319c094e56e5849 1 parent 9131a88
Josh Kalderimis joshk authored Carl Lerche committed
3  activemodel/lib/active_model/validations.rb
View
@@ -47,7 +47,8 @@ module Validations
included do
extend ActiveModel::Translation
- extend HelperMethods; include HelperMethods
+ extend HelperMethods
+ include HelperMethods
define_callbacks :validate, :scope => :name
12 activemodel/lib/active_model/validations/helper_methods.rb
View
@@ -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
37 activemodel/lib/active_model/validations/with.rb
View
@@ -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 @@ def validates_with(*args, &block)
# 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 @@ def validates_with(*args, &block)
# 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
3  activemodel/lib/active_model/validator.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.