Module: Skr::Concerns::HasGlTransaction::InstanceMethods
- Defined in:
- lib/skr/concerns/has_gl_transaction.rb
Instance Method Summary collapse
-
#save ⇒ Object
N.B.
Instance Method Details
#save ⇒ Object
N.B. We are overriding ActiveRecord::Base#save This is so we can wrap the save call inside a GlTransaction.record block which will collect any GlPostings and then collapse them down at the end of the block in order to prevent multiple postings to the same account.
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 |
# File 'lib/skr/concerns/has_gl_transaction.rb', line 21 def save(*) opts = # do nothing if the options specified an if clause and the method returns false if opts.present? and opts.has_key?(:if) and self.send( opts[:if] ) == false super else save_status = false # First we wrap it up in our transaction since we don't have the benefit of # save's rollback_active_record_state! block # I'm tempted to appropriate it's usage, but am not since it's not documented (that I've found) # and is thus likely to change GlTransaction.transaction( requires_new: true ) do # Enclose in GlTransaction#record block gl_transaction = GlTransaction.record do save_status = super # ActiveRecord::Base#save # If save was successfull, pass attributes back to GlTransaction#record. # Otherwise pass false back so it knows to not save the GlTransaction save_status ? { attributes: self.send( opts[:attributes] ) } : false end # if we saved successfully, but the GlTransaction did not; # Copy the errors to the model and abort the transaction if save_status && gl_transaction.errors.any? self.errors[:gl_transaction] += gl_transaction.errors. save_status = false raise InvalidGlTransaction.new(gl_transaction) end end return save_status end end |