diff --git a/lib/discard/model.rb b/lib/discard/model.rb index 5ba0740..cc2a7d5 100644 --- a/lib/discard/model.rb +++ b/lib/discard/model.rb @@ -117,8 +117,11 @@ def undiscarded? # @return [Boolean] true if successful, otherwise false def discard return false if discarded? - run_callbacks(:discard) do - update_attribute(self.class.discard_column, Time.current) + + _in_discard_transaction do + run_callbacks(:discard) do + update_attribute(self.class.discard_column, Time.current) + end end end @@ -139,8 +142,11 @@ def discard! # @return [Boolean] true if successful, otherwise false def undiscard return unless discarded? - run_callbacks(:undiscard) do - update_attribute(self.class.discard_column, nil) + + _in_discard_transaction do + run_callbacks(:undiscard) do + update_attribute(self.class.discard_column, nil) + end end end @@ -158,6 +164,18 @@ def undiscard! private + def _in_discard_transaction + previous_discard_value = public_send(self.class.discard_column) + + with_transaction_returning_status do + yield + end + rescue + assign_attributes(self.class.discard_column => previous_discard_value) + + raise + end + def _raise_record_not_discarded raise ::Discard::RecordNotDiscarded.new("Failed to discard the record", self) end