Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap discard methods in a transaction #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/discard/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down