lib/volt/models/validations/validations.rb in volt-0.9.5.pre4 vs lib/volt/models/validations/validations.rb in volt-0.9.5.pre5
- old
+ new
@@ -40,26 +40,39 @@
validate do
action = new? ? :create : :update
if run_in_actions.size == 0 || run_in_actions.include?(action)
@instance_validations = {}
+ @instance_custom_validations = []
instance_exec(action, &block)
result = run_validations(@instance_validations)
+ result.merge!(run_custom_validations(@instance_custom_validations))
@instance_validations = nil
+ @instance_custom_validations = nil
result
end
end
end
end
- def validate(field_name = nil, options = nil)
- @instance_validations[field_name] ||= {}
- @instance_validations[field_name].merge!(options)
+ # Called on the model inside of a validations block. Allows the user to
+ # control if validations should be run.
+ def validate(field_name = nil, options = nil, &block)
+ if block
+ # Setup a custom validation inside of the current validations block.
+ if field_name || options
+ fail 'validate should be passed a field name and options or a block, not both.'
+ end
+ @instance_custom_validations << block
+ else
+ @instance_validations[field_name] ||= {}
+ @instance_validations[field_name].merge!(options)
+ end
end
def self.included(base)
base.send :extend, ClassMethods
base.class_attribute(:custom_validations, :validations_to_run)
@@ -198,13 +211,15 @@
end
promise
end
- def run_custom_validations
+ def run_custom_validations(custom_validations = nil)
+ # Default to running the class level custom validations
+ custom_validations ||= self.class.custom_validations
+
promise = Promise.new.resolve(nil)
- # Call all of the custom validations
- custom_validations = self.class.custom_validations
+
if custom_validations
custom_validations.each do |custom_validation|
# Add to the promise chain
promise = promise.then do
# Run the validator in the context of the model