Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Created December 29, 2015 22:47
Show Gist options
  • Select an option

  • Save mecampbellsoup/fb80080b40e8f5990440 to your computer and use it in GitHub Desktop.

Select an option

Save mecampbellsoup/fb80080b40e8f5990440 to your computer and use it in GitHub Desktop.
module Zipmark
module Actions
module Deposits
class UnmarkReturned
include Virtus.model
include ActiveModel::Validations
attribute :id, String
attribute :dry_run, Boolean
validates :id, presence: true
validate :deposit_must_be_marked_returned
def perform!
if valid?
ActiveRecord::Base.transaction do
# Isolate the associated return and ledger item
_return = deposit.deposit_return
item = deposit.ledger_item
# Destroy the associated return and ledger item
_return.destroy
item.destroy
# restore the status of the deposit to `settled`
deposit.update_column(:status, "settled")
# if dry_run, revert
raise ActiveRecord::Rollback if dry_run?
end
else
raise errors.full_messages.to_sentence
end
end
private
def deposit
@deposit ||= (Deposit.find_by_id(id) || Disbursement.find_by_id(id))
end
def deposit_must_be_marked_returned
unless deposit.returned?
errors.add(:base, "Cannot un-return a deposit that has not yet been marked as returned")
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment