public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
Search Repo:
Move it around a bit

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9174 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
david (author)
Mon Mar 31 17:13:39 -0700 2008
commit  d80f9971b2331e04616864c43a2e9ee49beb5cb6
tree    d0bc1c319ea8763ee61c3384e33d36a825733840
parent  de56a4a4de94416309ce05fc19ef9c4c020a042c
...
1
2
3
4
5
6
7
8
9
 
10
11
12
13
14
...
25
26
27
28
29
30
31
32
33
34
35
36
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
40
41
...
67
68
69
70
 
71
72
73
...
75
76
77
78
 
79
80
81
...
1
2
 
 
3
4
5
6
 
7
8
9
10
11
12
...
23
24
25
 
 
 
26
 
 
 
 
 
 
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
...
117
118
119
 
120
121
122
123
...
125
126
127
 
128
129
130
131
0
@@ -1,12 +1,10 @@
0
 module ActiveModel
0
   module Validations
0
- VALIDATIONS = %w( validate validate_on_create validate_on_update )
0
-
0
     def self.included(base) # :nodoc:
0
       base.extend(ClassMethods)
0
       base.send!(:include, ActiveSupport::Callbacks)
0
 
0
- VALIDATIONS.each do |validation_method|
0
+ %w( validate validate_on_create validate_on_update ).each do |validation_method|
0
         base.class_eval <<-"end_eval"
0
           def self.#{validation_method}(*methods, &block)
0
             methods = CallbackChain.build(:#{validation_method}, *methods, &block)
0
0
0
@@ -25,17 +23,69 @@
0
       end
0
     end
0
 
0
- # All of the following validations are defined in the class scope of the model that you're interested in validating.
0
- # They offer a more declarative way of specifying when the model is valid and when it is not. It is recommended to use
0
- # these over the low-level calls to validate and validate_on_create when possible.
0
     module ClassMethods
0
- DEFAULT_VALIDATION_OPTIONS = {
0
- :on => :save,
0
- :allow_nil => false,
0
- :allow_blank => false,
0
- :message => nil
0
- }.freeze
0
+ DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, :allow_blank => false, :message => nil }.freeze
0
 
0
+ # Adds a validation method or block to the class. This is useful when
0
+ # overriding the #validate instance method becomes too unwieldly and
0
+ # you're looking for more descriptive declaration of your validations.
0
+ #
0
+ # This can be done with a symbol pointing to a method:
0
+ #
0
+ # class Comment < ActiveRecord::Base
0
+ # validate :must_be_friends
0
+ #
0
+ # def must_be_friends
0
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
+ # end
0
+ # end
0
+ #
0
+ # Or with a block which is passed the current record to be validated:
0
+ #
0
+ # class Comment < ActiveRecord::Base
0
+ # validate do |comment|
0
+ # comment.must_be_friends
0
+ # end
0
+ #
0
+ # def must_be_friends
0
+ # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
+ # end
0
+ # end
0
+ #
0
+ # This usage applies to #validate_on_create and #validate_on_update as well.
0
+
0
+ # Validates each attribute against a block.
0
+ #
0
+ # class Person < ActiveRecord::Base
0
+ # validates_each :first_name, :last_name do |record, attr, value|
0
+ # record.errors.add attr, 'starts with z.' if value[0] == ?z
0
+ # end
0
+ # end
0
+ #
0
+ # Options:
0
+ # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
0
+ # * <tt>allow_nil</tt> - Skip validation if attribute is nil.
0
+ # * <tt>allow_blank</tt> - Skip validation if attribute is blank.
0
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
0
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
0
+ # method, proc or string should return or evaluate to a true or false value.
0
+ # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
+ # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
+ # method, proc or string should return or evaluate to a true or false value.
0
+ def validates_each(*attrs)
0
+ options = attrs.extract_options!.symbolize_keys
0
+ attrs = attrs.flatten
0
+
0
+ # Declare the validation.
0
+ send(validation_method(options[:on] || :save), options) do |record|
0
+ attrs.each do |attr|
0
+ value = record.send(attr)
0
+ next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
0
+ yield record, attr, value
0
+ end
0
+ end
0
+ end
0
+
0
       private
0
         def validation_method(on)
0
           case on
0
@@ -67,7 +117,7 @@
0
 
0
         if responds_to?(:validate_on_create)
0
           ActiveSupport::Deprecations.warn(
0
- "Base#validate_on_create has been deprecated, please use Base.validate :method, :on => :create instead")
0
+ "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead")
0
           validate_on_create
0
         end
0
       else
0
@@ -75,7 +125,7 @@
0
 
0
         if responds_to?(:validate_on_update)
0
           ActiveSupport::Deprecations.warn(
0
- "Base#validate_on_update has been deprecated, please use Base.validate :method, :on => :update instead")
0
+ "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead")
0
           validate_on_update
0
         end
0
       end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,66 +1 @@
0
-module ActiveModel
0
- module Validations
0
- module ClassMethods
0
- # Adds a validation method or block to the class. This is useful when
0
- # overriding the #validate instance method becomes too unwieldly and
0
- # you're looking for more descriptive declaration of your validations.
0
- #
0
- # This can be done with a symbol pointing to a method:
0
- #
0
- # class Comment < ActiveRecord::Base
0
- # validate :must_be_friends
0
- #
0
- # def must_be_friends
0
- # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
- # end
0
- # end
0
- #
0
- # Or with a block which is passed the current record to be validated:
0
- #
0
- # class Comment < ActiveRecord::Base
0
- # validate do |comment|
0
- # comment.must_be_friends
0
- # end
0
- #
0
- # def must_be_friends
0
- # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
0
- # end
0
- # end
0
- #
0
- # This usage applies to #validate_on_create and #validate_on_update as well.
0
-
0
- # Validates each attribute against a block.
0
- #
0
- # class Person < ActiveRecord::Base
0
- # validates_each :first_name, :last_name do |record, attr, value|
0
- # record.errors.add attr, 'starts with z.' if value[0] == ?z
0
- # end
0
- # end
0
- #
0
- # Options:
0
- # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
0
- # * <tt>allow_nil</tt> - Skip validation if attribute is nil.
0
- # * <tt>allow_blank</tt> - Skip validation if attribute is blank.
0
- # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
0
- # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
- # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
- # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
- def validates_each(*attrs)
0
- options = attrs.extract_options!.symbolize_keys
0
- attrs = attrs.flatten
0
-
0
- # Declare the validation.
0
- send(validation_method(options[:on] || :save), options) do |record|
0
- attrs.each do |attr|
0
- value = record.send(attr)
0
- next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
0
- yield record, attr, value
0
- end
0
- end
0
- end
0
- end
0
- end
0
-end

Comments

    No one has commented yet.