Module: MassiveRecord::ORM::InTheMiddleOfSavingTracker
- Included in:
- Embedded
- Defined in:
- lib/massive_record/orm/in_the_middle_of_saving_tracker.rb
Overview
Tracks if we are in the middle of being saved or not, and if we are asked valid? in the middle of a save, we’ll return the last answer we gave to that question, instead of running validations again.
Main reason for doing so is when used in conjunction with Embedded records, in the following situations:
-
Calling save on an embedded record when the owner is not persisted will call save on the same record again when owner is saved and informs embeds many proxy that it is being about to be saved. Resulting in all callbacks fire again on the record which issued the save in the first place.
We can query each embedded record inside of embeds_many#parent_will_be_saved! if we are to call save on it (to make each embedded record update it’s persisted state), or if we should not do it, if the embedded record was the one triggered the save in the first place.
-
Calling save on an embedded record when the owner is not persisted will first validate the embedded record, then the owner will validate all associated records, including the one record which issued the save and therefor is validated.
We can controll if we are to run validations on a record or not, based if we are in the middle of a save on current record, thus callbacks (like before_validations etc) will not run twice.
Instance Attribute Summary collapse
-
#in_the_middle_of_saving ⇒ Object
(also: #in_the_middle_of_saving?)
readonly
Returns the value of attribute in_the_middle_of_saving.
Instance Method Summary collapse
Instance Attribute Details
#in_the_middle_of_saving ⇒ Object (readonly) Also known as: in_the_middle_of_saving?
Returns the value of attribute in_the_middle_of_saving.
40 41 42 |
# File 'lib/massive_record/orm/in_the_middle_of_saving_tracker.rb', line 40 def in_the_middle_of_saving @in_the_middle_of_saving end |
Instance Method Details
#save(options = {}) ⇒ Object
43 44 45 |
# File 'lib/massive_record/orm/in_the_middle_of_saving_tracker.rb', line 43 def save( = {}) with_in_the_middle_of_saving_tracker { super } end |
#save!(options = {}) ⇒ Object
47 48 49 |
# File 'lib/massive_record/orm/in_the_middle_of_saving_tracker.rb', line 47 def save!( = {}) with_in_the_middle_of_saving_tracker { super } end |
#valid? ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/massive_record/orm/in_the_middle_of_saving_tracker.rb', line 51 def valid?(*) if in_the_middle_of_saving? if @last_valid_value_inside_of_current_save.nil? @last_valid_value_inside_of_current_save = super else @last_valid_value_inside_of_current_save end else super end end |