Skip to content
This repository
Browse code

minor changes to instance level validations implementation based on f…

…eedback from José Valim
  • Loading branch information...
commit 92160219a8bf17bd435252304319c094e56e5849 1 parent 9131a88
Josh Kalderimis authored May 11, 2010 Carl Lerche committed May 13, 2010
3  activemodel/lib/active_model/validations.rb
@@ -47,7 +47,8 @@ module Validations
47 47
     included do
48 48
       extend ActiveModel::Translation
49 49
 
50  
-      extend HelperMethods; include HelperMethods
  50
+      extend  HelperMethods
  51
+      include HelperMethods
51 52
 
52 53
       define_callbacks :validate, :scope => :name
53 54
 
12  activemodel/lib/active_model/validations/helper_methods.rb
... ...
@@ -1,12 +0,0 @@
1  
-module ActiveModel
2  
-  module Validations
3  
-    module HelperMethods
4  
-      private
5  
-
6  
-        def _merge_attributes(attr_names)
7  
-          options = attr_names.extract_options!
8  
-          options.merge(:attributes => attr_names.flatten)
9  
-        end
10  
-    end
11  
-  end
12  
-end
37  activemodel/lib/active_model/validations/with.rb
... ...
@@ -1,5 +1,13 @@
1 1
 module ActiveModel
2 2
   module Validations
  3
+    module HelperMethods
  4
+      private
  5
+        def _merge_attributes(attr_names)
  6
+          options = attr_names.extract_options!
  7
+          options.merge(:attributes => attr_names.flatten)
  8
+        end
  9
+    end
  10
+
3 11
     module ClassMethods
4 12
 
5 13
       # Passes the record off to the class or classes specified and allows them
@@ -89,18 +97,8 @@ def validates_with(*args, &block)
89 97
     #     end
90 98
     #   end
91 99
     #
92  
-    #   class MyValidator < ActiveModel::Validator
93  
-    #     def validate(record)
94  
-    #       if some_complex_logic
95  
-    #         record.errors[:base] << "This record is invalid"
96  
-    #       end
97  
-    #     end
98  
-    #
99  
-    #     private
100  
-    #       def some_complex_logic
101  
-    #         # ...
102  
-    #       end
103  
-    #   end
  100
+    # Please consult the class method documentation for more information on
  101
+    # creating your own validator.
104 102
     #
105 103
     # You may also pass it multiple classes, like so:
106 104
     #
@@ -120,24 +118,13 @@ def validates_with(*args, &block)
120 118
     # in the callback
121 119
     #
122 120
     # If you pass any additional configuration options, they will be passed
123  
-    # to the class and available as <tt>options</tt>:
124  
-    #
125  
-    #   class Person
126  
-    #     include ActiveModel::Validations
127  
-    #     validates_with MyValidator, :my_custom_key => "my custom value"
128  
-    #   end
129  
-    #
130  
-    #   class MyValidator < ActiveModel::Validator
131  
-    #     def validate(record)
132  
-    #       options[:my_custom_key] # => "my custom value"
133  
-    #     end
134  
-    #   end
  121
+    # to the class and available as <tt>options</tt>, please refer to the
  122
+    # class version of this method for more information
135 123
     #
136 124
     def validates_with(*args, &block)
137 125
       options = args.extract_options!
138 126
       args.each do |klass|
139 127
         validator = klass.new(options, &block)
140  
-        validator.setup(self) if validator.respond_to?(:setup)
141 128
         validator.validate(self)
142 129
       end
143 130
     end
3  activemodel/lib/active_model/validator.rb
@@ -88,6 +88,9 @@ module ActiveModel #:nodoc:
88 88
   #       klass.send :attr_accessor, :custom_attribute
89 89
   #     end
90 90
   #   end
  91
+  #
  92
+  # This setup method is only called when used with validation macros or the
  93
+  # class level <tt>validates_with</tt> method.
91 94
   # 
92 95
   class Validator
93 96
     attr_reader :options

0 notes on commit 9216021

Please sign in to comment.
Something went wrong with that request. Please try again.