Module: Volt::Validations::ClassMethods
- Defined in:
- lib/volt/models/validations/validations.rb
Instance Method Summary collapse
-
#validate(field_name = nil, options = nil, &block) ⇒ Object
Validate is called directly on the class and sets up the validation to be run each time validate! is called on the class.
-
#validations(*run_in_actions, &block) ⇒ Object
Validations takes a block, and can contain validate calls inside of it which will conditionally be run based on the code in the block.
Instance Method Details
#validate(field_name = nil, options = nil, &block) ⇒ Object
Validate is called directly on the class and sets up the validation to be run each time validate! is called on the class.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/volt/models/validations/validations.rb', line 18 def validate(field_name = nil, = nil, &block) if block if field_name || fail 'validate should be passed a field name and options or a block, not both.' end self.custom_validations ||= [] custom_validations << block else self.validations_to_run ||= {} validations_to_run[field_name] ||= {} validations_to_run[field_name].merge!() end end |
#validations(*run_in_actions, &block) ⇒ Object
Validations takes a block, and can contain validate calls inside of it which will conditionally be run based on the code in the block. The context of the block will be the current model.
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 |
# File 'lib/volt/models/validations/validations.rb', line 35 def validations(*run_in_actions, &block) unless block_given? raise 'validations must take a block, use `validate` to setup a validation on a class directly.' end # Add a validation block to run during each validation 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 |