Module: Volt::Models::Helpers::ChangeHelpers
- Included in:
- Volt::Model
- Defined in:
- lib/volt/models/helpers/change_helpers.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#run_changed(attribute_name = nil) ⇒ Promise|nil
Called when something in the model changes.
Class Method Details
.included(base) ⇒ Object
7 8 9 |
# File 'lib/volt/models/helpers/change_helpers.rb', line 7 def self.included(base) base.setup_action_helpers_in_class(:before_save, :before_validate) end |
Instance Method Details
#run_changed(attribute_name = nil) ⇒ Promise|nil
Called when something in the model changes. Saves the model if there is a persistor, and changes the model to not be new.
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 |
# File 'lib/volt/models/helpers/change_helpers.rb', line 17 def run_changed(attribute_name = nil) # no_validate mode should only be used internally. no_validate mode is a # performance optimization that prevents validation from running after each # change when assigning multile attributes. unless Volt.in_mode?(:no_validate) # Run the validations for all fields result = nil return validate!.then do # Buffers are allowed to be in an invalid state unless buffer? # First check that all local validations pass. Any time any # validations fail, the model is in an invalid state and won't # persist. However, we want to be able to move the model # towards a valid state one change at a time. if error_in_changed_attributes? # Some errors are present, revert changes revert_changes! # After we revert, we need to validate again to get the error messages back # TODO: Could probably cache the previous errors. result = validate!.then do # Reject the promise with the errors Promise.new.reject(errs) end else result = persist_changes(attribute_name) end end # Return result inside of the validate! promise result.then { self } end end # Didn't run validations self.then end |