lib/validatable_class_methods.rb in validatable-1.2.2 vs lib/validatable_class_methods.rb in validatable-1.3.0
- old
+ new
@@ -18,13 +18,11 @@
# * level - The level at which the validation should occur
# * if - A block that when executed must return true of the validation will not occur
# * with - The regular expression used to validate the format
# * group - The group that this validation belongs to. A validation can belong to multiple groups
def validates_format_of(*args)
- add_validations(args, ValidatesFormatOf) do |validation, options|
- validation.with = options[:with]
- end
+ add_validations(args, ValidatesFormatOf)
end
# call-seq: validates_length_of(*args)
#
# Validates that the specified attribute matches the length restrictions supplied.
@@ -41,17 +39,15 @@
# * times - The number of times the validation applies
# * level - The level at which the validation should occur
# * if - A block that when executed must return true of the validation will not occur
# * minimum - The minimum size of the attribute
# * maximum - The maximum size of the attribute
+ # * is - The size the attribute must be
+ # * within - A range that the size of the attribute must fall within
# * group - The group that this validation belongs to. A validation can belong to multiple groups
def validates_length_of(*args)
- add_validations(args, ValidatesLengthOf) do |validation, options|
- validation.minimum = options[:minimum]
- validation.maximum = options[:maximum]
- validation.is = options[:is]
- end
+ add_validations(args, ValidatesLengthOf)
end
# call-seq: validates_numericality_of(*args)
#
# Validates that the specified attribute is numeric.
@@ -66,10 +62,11 @@
# * message - The message to add to the errors collection when the validation fails
# * times - The number of times the validation applies
# * level - The level at which the validation should occur
# * if - A block that when executed must return true of the validation will not occur
# * group - The group that this validation belongs to. A validation can belong to multiple groups
+ # * only_integer - Whether the attribute must be an integer (default is false)
def validates_numericality_of(*args)
add_validations(args, ValidatesNumericalityOf)
end
# call-seq: validates_acceptance_of(*args)
@@ -115,17 +112,11 @@
# * times - The number of times the validation applies
# * level - The level at which the validation should occur
# * if - A block that when executed must return true of the validation will not occur
# * group - The group that this validation belongs to. A validation can belong to multiple groups
def validates_confirmation_of(*args)
- add_validations(args, ValidatesConfirmationOf) do |validation, options|
- validation.case_sensitive = if options.has_key? :case_sensitive
- options[:case_sensitive]
- else
- true
- end
- end
+ add_validations(args, ValidatesConfirmationOf)
end
# call-seq: validates_presence_of(*args)
#
# Validates that the specified attributes are not nil or an empty string
@@ -166,13 +157,11 @@
# * level - The level at which the validation should occur
# * if - A block that when executed must return true of the validation will not occur
# * group - The group that this validation belongs to. A validation can belong to multiple groups
# * logic - A block that executes to perform the validation
def validates_true_for(*args)
- add_validations(args, ValidatesTrueFor) do |validation, options|
- validation.logic = options[:logic]
- end
+ add_validations(args, ValidatesTrueFor)
end
# call-seq: include_validations_for(attribute_to_validate, options = {})
#
# Validates the specified attributes.
@@ -204,59 +193,51 @@
# * if - A block that when executed must return true of the validation will not occur.
def include_validations_for(attribute_to_validate, options = {})
children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
end
- def validate(instance) #:nodoc:
- levels = self.validations.collect { |validation| validation.level }.uniq
- levels.sort.each do |level|
- self.validations.select { |validation| validation.level == level }.each do |validation|
- if validation.should_validate?(instance)
- instance.errors.add(validation.attribute, validation.message) unless validation.valid?(instance)
- end
- end
- return if instance.errors.any?
- end
- end
-
def validate_children(instance, groups) #:nodoc:
self.children_to_validate.each do |child_validation|
next unless child_validation.should_validate?(instance)
child = instance.send child_validation.attribute
child.valid?(*groups)
child.errors.each do |attribute, message|
- instance.errors.add(child_validation.map[attribute.to_sym] || attribute, message)
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
end
end
end
def validations #:nodoc:
@validations ||= []
end
-
+
+ def add_error(instance, attribute, message) #:nodoc:
+ instance.errors.add(attribute, message)
+ end
+
protected
+
def add_validations(args, klass) #:nodoc:
options = args.last.is_a?(Hash) ? args.pop : {}
args.each do |attribute|
- klass.must_understand options
new_validation = klass.new attribute, options
- yield new_validation, options if block_given?
self.validations << new_validation
- self.create_valid_method_for_groups new_validation.groups
+ self.create_valid_method_for_groups new_validation.groups
end
end
- def create_valid_method_for_groups(groups)
+ def create_valid_method_for_groups(groups) #:nodoc:
groups.each do |group|
self.class_eval do
- define_method :"valid_for_#{group}?" do
+ define_method "valid_for_#{group}?".to_sym do
valid_for_group?(group)
end
end
end
end
def children_to_validate #:nodoc:
@children_to_validate ||= []
end
+
end
end
\ No newline at end of file