mongomatic 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/lib/mongomatic/base.rb +1 -1
  2. data/lib/mongomatic/validatable/child_validation.rb +17 -0
  3. data/lib/mongomatic/validatable/errors.rb +108 -0
  4. data/lib/mongomatic/validatable/included_validation.rb +11 -0
  5. data/lib/mongomatic/validatable/macros.rb +316 -0
  6. data/lib/{validatable → mongomatic/validatable}/object_extension.rb +0 -0
  7. data/lib/mongomatic/validatable/requireable.rb +28 -0
  8. data/lib/mongomatic/validatable/understandable.rb +33 -0
  9. data/lib/mongomatic/validatable/validatable_class_methods.rb +89 -0
  10. data/lib/mongomatic/validatable/validatable_instance_methods.rb +108 -0
  11. data/lib/mongomatic/validatable/validations/validates_acceptance_of.rb +16 -0
  12. data/lib/mongomatic/validatable/validations/validates_associated.rb +15 -0
  13. data/lib/mongomatic/validatable/validations/validates_confirmation_of.rb +25 -0
  14. data/lib/mongomatic/validatable/validations/validates_each.rb +16 -0
  15. data/lib/mongomatic/validatable/validations/validates_exclusion_of.rb +19 -0
  16. data/lib/mongomatic/validatable/validations/validates_format_of.rb +18 -0
  17. data/lib/mongomatic/validatable/validations/validates_inclusion_of.rb +19 -0
  18. data/lib/mongomatic/validatable/validations/validates_length_of.rb +32 -0
  19. data/lib/mongomatic/validatable/validations/validates_numericality_of.rb +28 -0
  20. data/lib/mongomatic/validatable/validations/validates_presence_of.rb +18 -0
  21. data/lib/mongomatic/validatable/validations/validates_true_for.rb +15 -0
  22. data/lib/mongomatic/validatable/validations/validation_base.rb +93 -0
  23. data/lib/{validatable.rb → mongomatic/validatable.rb} +4 -2
  24. data/lib/mongomatic.rb +1 -1
  25. metadata +26 -35
  26. data/lib/validatable/child_validation.rb +0 -15
  27. data/lib/validatable/errors.rb +0 -106
  28. data/lib/validatable/included_validation.rb +0 -9
  29. data/lib/validatable/macros.rb +0 -314
  30. data/lib/validatable/requireable.rb +0 -26
  31. data/lib/validatable/understandable.rb +0 -31
  32. data/lib/validatable/validatable_class_methods.rb +0 -87
  33. data/lib/validatable/validatable_instance_methods.rb +0 -106
  34. data/lib/validatable/validations/validates_acceptance_of.rb +0 -14
  35. data/lib/validatable/validations/validates_associated.rb +0 -13
  36. data/lib/validatable/validations/validates_confirmation_of.rb +0 -23
  37. data/lib/validatable/validations/validates_each.rb +0 -14
  38. data/lib/validatable/validations/validates_exclusion_of.rb +0 -17
  39. data/lib/validatable/validations/validates_format_of.rb +0 -16
  40. data/lib/validatable/validations/validates_inclusion_of.rb +0 -17
  41. data/lib/validatable/validations/validates_length_of.rb +0 -30
  42. data/lib/validatable/validations/validates_numericality_of.rb +0 -27
  43. data/lib/validatable/validations/validates_presence_of.rb +0 -17
  44. data/lib/validatable/validations/validates_true_for.rb +0 -13
  45. data/lib/validatable/validations/validation_base.rb +0 -91
@@ -1,106 +0,0 @@
1
- module Validatable
2
- class Errors
3
- extend Forwardable
4
- include Enumerable
5
-
6
- def_delegators :errors, :clear, :each, :each_pair, :empty?, :length, :size
7
-
8
- # Returns true if the specified +attribute+ has errors associated with it.
9
- #
10
- # class Company < ActiveRecord::Base
11
- # validates_presence_of :name, :address, :email
12
- # validates_length_of :name, :in => 5..30
13
- # end
14
- #
15
- # company = Company.create(:address => '123 First St.')
16
- # company.errors.invalid?(:name) # => true
17
- # company.errors.invalid?(:address) # => false
18
- def invalid?(attribute)
19
- !@errors[attribute.to_sym].nil?
20
- end
21
-
22
- # Adds an error to the base object instead of any particular attribute. This is used
23
- # to report errors that don't tie to any specific attribute, but rather to the object
24
- # as a whole. These error messages don't get prepended with any field name when iterating
25
- # with +each_full+, so they should be complete sentences.
26
- def add_to_base(msg)
27
- add(:base, msg)
28
- end
29
-
30
- # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
31
- def on_base
32
- on(:base)
33
- end
34
-
35
- # call-seq: on(attribute)
36
- #
37
- # * Returns nil, if no errors are associated with the specified +attribute+.
38
- # * Returns the error message, if one error is associated with the specified +attribute+.
39
- # * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
40
- def on(attribute)
41
- return nil if errors[attribute.to_sym].nil?
42
- errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
43
- end
44
-
45
- # Rails 3 API for errors, always return array.
46
- def [](attribute)
47
- errors[attribute.to_sym] || []
48
- end
49
-
50
- def add(attribute, message) #:nodoc:
51
- errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
52
- errors[attribute.to_sym] << message
53
- end
54
-
55
- def merge!(errors) #:nodoc:
56
- errors.each_pair{|k, v| add(k,v)}
57
- self
58
- end
59
-
60
- # call-seq: replace(attribute)
61
- #
62
- # * Replaces the errors value for the given +attribute+
63
- def replace(attribute, value)
64
- errors[attribute.to_sym] = value
65
- end
66
-
67
- # call-seq: raw(attribute)
68
- #
69
- # * Returns an array of error messages associated with the specified +attribute+.
70
- def raw(attribute)
71
- errors[attribute.to_sym]
72
- end
73
-
74
- def errors #:nodoc:
75
- @errors ||= {}
76
- end
77
-
78
- def count #:nodoc:
79
- errors.values.flatten.size
80
- end
81
-
82
- # call-seq: full_messages -> an_array_of_messages
83
- #
84
- # Returns an array containing the full list of error messages.
85
- def full_messages
86
- full_messages = []
87
-
88
- errors.each_key do |attribute|
89
- errors[attribute].each do |msg|
90
- next if msg.nil?
91
-
92
- if attribute.to_s == "base"
93
- full_messages << msg
94
- else
95
- full_messages << humanize(attribute.to_s) + " " + msg
96
- end
97
- end
98
- end
99
- full_messages
100
- end
101
-
102
- def humanize(lower_case_and_underscored_word) #:nodoc:
103
- lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
104
- end
105
- end
106
- end
@@ -1,9 +0,0 @@
1
- module Validatable
2
- class IncludedValidation #:nodoc:
3
- attr_accessor :attribute
4
-
5
- def initialize(attribute)
6
- @attribute = attribute
7
- end
8
- end
9
- end
@@ -1,314 +0,0 @@
1
- module Validatable
2
- module Macros
3
- # call-seq: validates_each(*args)
4
- #
5
- # Validates that the logic evaluates to true
6
- #
7
- # class Address
8
- # include Validatable
9
- # validates_each :zip_code, :logic => lambda { errors.add(:zip_code, "is not valid") if ZipCodeService.allows(zip_code) }
10
- # end
11
- #
12
- # The logic option is required.
13
- #
14
- # Configuration options:
15
- #
16
- # * after_validate - A block that executes following the run of a validation
17
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
18
- # * if - A block that when executed must return true of the validation will not occur
19
- # * level - The level at which the validation should occur
20
- # * logic - A block that executes to perform the validation
21
- # * message - The message to add to the errors collection when the validation fails
22
- # * times - The number of times the validation applies
23
- def validates_each(*args)
24
- add_validations(args, ValidatesEach)
25
- end
26
-
27
- # call-seq: validates_format_of(*args)
28
- #
29
- # Validates whether the value of the specified attribute is of the
30
- # correct form by matching it against the regular expression provided.
31
- #
32
- # class Person
33
- # include Validatable
34
- # validates_format_of :first_name, :with => /[ A-Za-z]/
35
- # end
36
- #
37
- # A regular expression must be provided or else an exception will be raised.
38
- #
39
- # Configuration options:
40
- #
41
- # * after_validate - A block that executes following the run of a validation
42
- # * message - The message to add to the errors collection when the validation fails
43
- # * times - The number of times the validation applies
44
- # * level - The level at which the validation should occur
45
- # * if - A block that when executed must return true of the validation will not occur
46
- # * with - The regular expression used to validate the format
47
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
48
- def validates_format_of(*args)
49
- add_validations(args, ValidatesFormatOf)
50
- end
51
-
52
- # call-seq: validates_length_of(*args)
53
- #
54
- # Validates that the specified attribute matches the length restrictions supplied.
55
- #
56
- # class Person
57
- # include Validatable
58
- # validates_length_of :first_name, :maximum=>30
59
- # validates_length_of :last_name, :minimum=>30
60
- # end
61
- #
62
- # Configuration options:
63
- #
64
- # * after_validate - A block that executes following the run of a validation
65
- # * message - The message to add to the errors collection when the validation fails
66
- # * times - The number of times the validation applies
67
- # * level - The level at which the validation should occur
68
- # * if - A block that when executed must return true of the validation will not occur
69
- # * minimum - The minimum size of the attribute
70
- # * maximum - The maximum size of the attribute
71
- # * is - The size the attribute must be
72
- # * within - A range that the size of the attribute must fall within
73
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
74
- def validates_length_of(*args)
75
- add_validations(args, ValidatesLengthOf)
76
- end
77
-
78
- # call-seq: validates_numericality_of(*args)
79
- #
80
- # Validates that the specified attribute is numeric.
81
- #
82
- # class Person
83
- # include Validatable
84
- # validates_numericality_of :age
85
- # end
86
- #
87
- # Configuration options:
88
- #
89
- # * after_validate - A block that executes following the run of a validation
90
- # * message - The message to add to the errors collection when the validation fails
91
- # * times - The number of times the validation applies
92
- # * level - The level at which the validation should occur
93
- # * if - A block that when executed must return true of the validation will not occur
94
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
95
- # * only_integer - Whether the attribute must be an integer (default is false)
96
- def validates_numericality_of(*args)
97
- add_validations(args, ValidatesNumericalityOf)
98
- end
99
-
100
- # call-seq: validates_acceptance_of(*args)
101
- #
102
- # Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:
103
- #
104
- # class Person
105
- # include Validatable
106
- # validates_acceptance_of :terms_of_service
107
- # validates_acceptance_of :eula, :message => "must be abided"
108
- # end
109
- #
110
- # Configuration options:
111
- #
112
- # * after_validate - A block that executes following the run of a validation
113
- # * message - The message to add to the errors collection when the validation fails
114
- # * times - The number of times the validation applies
115
- # * level - The level at which the validation should occur
116
- # * if - A block that when executed must return true of the validation will not occur
117
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
118
- def validates_acceptance_of(*args)
119
- add_validations(args, ValidatesAcceptanceOf)
120
- end
121
-
122
- # call-seq: validates_confirmation_of(*args)
123
- #
124
- # Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
125
- #
126
- # Class:
127
- # class PersonPresenter
128
- # include Validatable
129
- # validates_confirmation_of :user_name, :password
130
- # validates_confirmation_of :email_address, :message => "should match confirmation"
131
- # end
132
- #
133
- # View:
134
- # <%= password_field "person", "password" %>
135
- # <%= password_field "person", "password_confirmation" %>
136
- #
137
- # Configuration options:
138
- #
139
- # * after_validate - A block that executes following the run of a validation
140
- # * case_sensitive - Whether or not to apply case-sensitivity on the comparison. Defaults to true.
141
- # * message - The message to add to the errors collection when the validation fails
142
- # * times - The number of times the validation applies
143
- # * level - The level at which the validation should occur
144
- # * if - A block that when executed must return true of the validation will not occur
145
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
146
- def validates_confirmation_of(*args)
147
- add_validations(args, ValidatesConfirmationOf)
148
- end
149
-
150
- # call-seq: validates_presence_of(*args)
151
- #
152
- # Validates that the specified attributes are not nil or an empty string
153
- #
154
- # class Person
155
- # include Validatable
156
- # validates_presence_of :first_name
157
- # end
158
- #
159
- # The first_name attribute must be in the object and it cannot be nil or empty.
160
- #
161
- # Configuration options:
162
- #
163
- # * after_validate - A block that executes following the run of a validation
164
- # * message - The message to add to the errors collection when the validation fails
165
- # * times - The number of times the validation applies
166
- # * level - The level at which the validation should occur
167
- # * if - A block that when executed must return true of the validation will not occur
168
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
169
- def validates_presence_of(*args)
170
- add_validations(args, ValidatesPresenceOf)
171
- end
172
-
173
- # call-seq: validates_true_for(*args)
174
- #
175
- # Validates that the logic evaluates to true
176
- #
177
- # class Person
178
- # include Validatable
179
- # validates_true_for :first_name, :logic => lambda { first_name == 'Jamie' }
180
- # end
181
- #
182
- # The logic option is required.
183
- #
184
- # Configuration options:
185
- #
186
- # * after_validate - A block that executes following the run of a validation
187
- # * message - The message to add to the errors collection when the validation fails
188
- # * times - The number of times the validation applies
189
- # * level - The level at which the validation should occur
190
- # * if - A block that when executed must return true of the validation will not occur
191
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
192
- # * logic - A block that executes to perform the validation
193
- def validates_true_for(*args)
194
- add_validations(args, ValidatesTrueFor)
195
- end
196
-
197
- def validates_exclusion_of(*args)
198
- add_validations(args, ValidatesExclusionOf)
199
- end
200
-
201
- def validates_inclusion_of(*args)
202
- add_validations(args, ValidatesInclusionOf)
203
- end
204
-
205
- # call-seq: validates_associated(*args)
206
- #
207
- # Checks the validity of an associated object or objects and adds a single
208
- # error if validation fails.
209
- #
210
- # class Person
211
- # include Validatable
212
- # attr_accessor :addresses
213
- # validates_associated :addresses
214
- # end
215
- #
216
- # Configuration options:
217
- #
218
- # * after_validate - A block that executes following the run of a validation
219
- # * message - The message to add to the errors collection when the validation fails
220
- # * times - The number of times the validation applies
221
- # * level - The level at which the validation should occur
222
- # * if - A block that when executed must return true of the validation will not occur
223
- # * group - The group that this validation belongs to. A validation can belong to multiple groups
224
- def validates_associated(*args)
225
- add_validations(args, ValidatesAssociated)
226
- end
227
-
228
- # call-seq: include_validations_from(attribute)
229
- #
230
- # Includes all the validations that are defined on the attribute.
231
- # class Person
232
- # include Validatable
233
- # validates_presence_of :name
234
- # end
235
- #
236
- # class PersonPresenter
237
- # include Validatable
238
- # include_validataions_from :person
239
- # attr_accessor :person
240
- # def name
241
- # person.name
242
- # end
243
- #
244
- # def initialize(person)
245
- # @person = person
246
- # end
247
- # end
248
- #
249
- # presenter = PersonPresenter.new(Person.new)
250
- # presenter.valid? #=> false
251
- # presenter.errors.on(:name) #=> "can't be blank"
252
- #
253
- # The name attribute whose validations should be added.
254
- def include_validations_from(attribute_to_validate, options = {})
255
- validations_to_include << IncludedValidation.new(attribute_to_validate)
256
- end
257
-
258
- # call-seq: include_errors_from(attribute_to_validate, options = {})
259
- #
260
- # Validates the specified attributes.
261
- # class Person
262
- # include Validatable
263
- # validates_presence_of :name
264
- # attr_accessor :name
265
- # end
266
- #
267
- # class PersonPresenter
268
- # include Validatable
269
- # include_errors_from :person, :map => { :name => :namen }, :if => lambda { not person.nil? }
270
- # attr_accessor :person
271
- #
272
- # def initialize(person)
273
- # @person = person
274
- # end
275
- # end
276
- #
277
- # presenter = PersonPresenter.new(Person.new)
278
- # presenter.valid? #=> false
279
- # presenter.errors.on(:namen) #=> "can't be blank"
280
- #
281
- # The person attribute will be validated.
282
- # If person is invalid the errors will be added to the PersonPresenter errors collection.
283
- #
284
- # Configuration options:
285
- #
286
- # * map - A hash that maps attributes of the child to attributes of the parent.
287
- # * if - A block that when executed must return true of the validation will not occur.
288
- def include_errors_from(attribute_to_validate, options = {})
289
- children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
290
- end
291
-
292
- def include_validations_for(attribute_to_validate, options = {}) #:nodoc:
293
- puts "include_validations_for is deprecated; use include_errors_from instead"
294
- children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
295
- end
296
-
297
- # call-seq: before_validation(&block)
298
- #
299
- # Is called before valid? or valid_for_*?
300
- #
301
- # class Person
302
- # include Validatable
303
- # before_validation do
304
- # self.name = "default name"
305
- # end
306
- #
307
- # attr_accessor :name
308
- # end
309
- #
310
- def before_validation(&block)
311
- before_validations << block
312
- end
313
- end
314
- end
@@ -1,26 +0,0 @@
1
- module Validatable
2
- module Requireable #:nodoc:
3
- module ClassMethods #:nodoc:
4
- def requires(*args)
5
- required_options.concat args
6
- end
7
-
8
- def required_options
9
- @required_options ||= []
10
- end
11
- end
12
-
13
- def self.included(klass)
14
- klass.extend ClassMethods
15
- end
16
-
17
- def requires(options)
18
- required_options = self.class.required_options.inject([]) do |errors, attribute|
19
- errors << attribute.to_s unless options.has_key?(attribute)
20
- errors
21
- end
22
- raise ArgumentError.new("#{self.class} requires options: #{required_options.join(', ')}") if required_options.any?
23
- true
24
- end
25
- end
26
- end
@@ -1,31 +0,0 @@
1
- module Validatable
2
- module Understandable #:nodoc:
3
- module ClassMethods #:nodoc:
4
- def understands(*args)
5
- understandings.concat args
6
- end
7
-
8
- def understandings
9
- @understandings ||= []
10
- end
11
-
12
- def all_understandings
13
- return understandings + self.superclass.all_understandings if self.superclass.respond_to? :all_understandings
14
- understandings
15
- end
16
- end
17
-
18
- def self.included(klass)
19
- klass.extend ClassMethods
20
- end
21
-
22
- def must_understand(hash)
23
- invalid_options = hash.inject([]) do |errors, (key, value)|
24
- errors << key.to_s unless self.class.all_understandings.include?(key)
25
- errors
26
- end
27
- raise ArgumentError.new("invalid options: #{invalid_options.join(', ')}") if invalid_options.any?
28
- true
29
- end
30
- end
31
- end
@@ -1,87 +0,0 @@
1
- module Validatable
2
- module ClassMethods #:nodoc:
3
-
4
- def validate_children(instance, group)
5
- self.children_to_validate.each do |child_validation|
6
- next unless child_validation.should_validate?(instance)
7
- child_or_children = instance.send child_validation.attribute
8
- [child_or_children].flatten.each do |child|
9
- if (child.respond_to?(:valid_for_group?))
10
- child.valid_for_group?(group)
11
- else
12
- child.valid?
13
- end
14
- child.errors.each do |attribute, messages|
15
- if messages.is_a?(String)
16
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
17
- else
18
- messages.each do |message|
19
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
27
- def all_before_validations
28
- if self.superclass.respond_to? :all_before_validations
29
- return before_validations + self.superclass.all_before_validations
30
- end
31
- before_validations
32
- end
33
-
34
- def before_validations
35
- @before_validations ||= []
36
- end
37
-
38
- def all_validations
39
- if self.respond_to?(:superclass) && self.superclass.respond_to?(:all_validations)
40
- return validations + self.superclass.all_validations
41
- end
42
- validations
43
- end
44
-
45
- def validations
46
- @validations ||= []
47
- end
48
-
49
- def add_error(instance, attribute, msg)
50
- instance.errors.add(attribute, msg)
51
- end
52
-
53
- def validation_keys_include?(key)
54
- validations.map { |validation| validation.key }.include?(key)
55
- end
56
-
57
- def validations_to_include
58
- @validations_to_include ||= []
59
- end
60
-
61
- protected
62
-
63
- def add_validations(args, klass)
64
- options = args.last.is_a?(Hash) ? args.pop : {}
65
- args.each do |attribute|
66
- new_validation = klass.new self, attribute, options
67
- self.validations << new_validation
68
- self.create_valid_method_for_groups new_validation.groups
69
- end
70
- end
71
-
72
- def create_valid_method_for_groups(groups)
73
- groups.each do |group|
74
- self.class_eval do
75
- define_method "valid_for_#{group}?".to_sym do
76
- valid_for_group?(group)
77
- end
78
- end
79
- end
80
- end
81
-
82
- def children_to_validate
83
- @children_to_validate ||= []
84
- end
85
-
86
- end
87
- end
@@ -1,106 +0,0 @@
1
- module Validatable
2
- def self.included(klass) #:nodoc:
3
- klass.extend Validatable::ClassMethods
4
- klass.extend Validatable::Macros
5
- end
6
-
7
- # call-seq: valid?
8
- #
9
- # Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
10
- def valid?
11
- valid_for_group?(nil)
12
- end
13
-
14
- # call-seq: errors
15
- #
16
- # Returns the Errors object that holds all information about attribute error messages.
17
- def errors
18
- @errors ||= Validatable::Errors.new
19
- end
20
-
21
- def valid_for_group?(group) #:nodoc:
22
- errors.clear
23
- run_before_validations
24
- self.class.validate_children(self, group)
25
- self.validate_group(group)
26
- errors.empty?
27
- end
28
-
29
- def times_validated(key) #:nodoc:
30
- times_validated_hash[key] || 0
31
- end
32
-
33
- def increment_times_validated_for(validation) #:nodoc:
34
- if validation.key != nil
35
- if times_validated_hash[validation.key].nil?
36
- times_validated_hash[validation.key] = 1
37
- else
38
- times_validated_hash[validation.key] += 1
39
- end
40
- end
41
- end
42
-
43
- # call-seq: validate_only(key)
44
- #
45
- # Only executes a specified validation. The argument should follow a pattern based on the key of the validation.
46
- # Examples:
47
- # * validates_presence_of :name can be run with obj.validate_only("presence_of/name")
48
- # * validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
49
- def validate_only(key)
50
- validation_name, attribute_name = key.split("/")
51
- validation_name = validation_name.split("_").collect{|word| word.capitalize}.join
52
- validation_key = "#{self.class.name}/Validatable::Validates#{validation_name}/#{attribute_name}"
53
- validation = self.class.all_validations.find { |validation| validation.key == validation_key }
54
- raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil?
55
- errors.clear
56
- run_validation(validation)
57
- end
58
-
59
- protected
60
- def times_validated_hash #:nodoc:
61
- @times_validated_hash ||= {}
62
- end
63
-
64
- def validate_group(group) #:nodoc:
65
- validation_levels.each do |level|
66
- validations_for_level_and_group(level, group).each do |validation|
67
- run_validation(validation) if validation.should_validate?(self)
68
- end
69
- return unless self.errors.empty?
70
- end
71
- end
72
-
73
- def run_validation(validation) #:nodoc:
74
- validation_result = validation.valid?(self)
75
- add_error(validation.attribute, validation.message(self)) unless validation_result
76
- increment_times_validated_for(validation)
77
- validation.run_after_validate(validation_result, self, validation.attribute)
78
- end
79
-
80
- def run_before_validations #:nodoc:
81
- self.class.all_before_validations.each do |block|
82
- instance_eval &block
83
- end
84
- end
85
-
86
- def add_error(attribute, message) #:nodoc:
87
- self.class.add_error(self, attribute, message)
88
- end
89
-
90
- def validations_for_level_and_group(level, group) #:nodoc:
91
- validations_for_level = self.all_validations.select { |validation| validation.level == level }
92
- return validations_for_level.select { |validation| validation.groups.empty? } if group.nil?
93
- validations_for_level.select { |validation| validation.groups.include?(group) }
94
- end
95
-
96
- def all_validations #:nodoc:
97
- res = self.class.validations_to_include.inject(self.class.all_validations) do |result, included_validation_class|
98
- result += self.send(included_validation_class.attribute).all_validations
99
- result
100
- end
101
- end
102
-
103
- def validation_levels #:nodoc:
104
- self.class.all_validations.inject([1]) { |result, validation| result << validation.level }.uniq.sort
105
- end
106
- end
@@ -1,14 +0,0 @@
1
- module Validatable
2
- class ValidatesAcceptanceOf < ValidationBase #:nodoc:
3
- def valid?(instance)
4
- value = instance[self.attribute.to_s]
5
- return true if allow_nil && value.nil?
6
- return true if allow_blank && value.blank?
7
- %w(1 true t).include?(value)
8
- end
9
-
10
- def message(instance)
11
- super || "must be accepted"
12
- end
13
- end
14
- end