2021-02-02 21:04:53 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
|
|
|
class Transaction < ApplicationRecord
|
|
|
|
include Model::Houidable
|
2021-02-02 23:48:05 +00:00
|
|
|
include Model::Jbuilder
|
|
|
|
include Model::Eventable
|
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
setup_houid :trx
|
2021-02-02 23:48:05 +00:00
|
|
|
add_builder_expansion :nonprofit, :supporter
|
2021-02-02 21:04:53 +00:00
|
|
|
|
|
|
|
belongs_to :supporter
|
2021-02-02 23:48:05 +00:00
|
|
|
has_one :nonprofit, through: :supporter
|
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
has_many :transaction_assignments
|
|
|
|
|
|
|
|
has_many :ticket_purchases, through: :transaction_assignments, source: :assignable, source_type: 'TicketPurchase'
|
|
|
|
|
|
|
|
validates :supporter, presence: true
|
|
|
|
|
|
|
|
|
|
|
|
def to_builder(*expand)
|
2021-02-02 23:48:05 +00:00
|
|
|
init_builder(*expand) do |json|
|
2021-02-02 21:04:53 +00:00
|
|
|
json.(self, :id)
|
|
|
|
json.object 'transaction'
|
2021-02-02 23:48:05 +00:00
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
json.amount do
|
|
|
|
json.value_in_cents amount || 0
|
2021-02-02 23:48:05 +00:00
|
|
|
json.currency nonprofit.currency
|
2021-02-02 21:04:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-02 23:48:05 +00:00
|
|
|
|
|
|
|
def publish_created
|
|
|
|
Houdini.event_publisher.announce(:transaction_created,
|
|
|
|
to_event('transaction.created', :nonprofit, :supporter).attributes!)
|
|
|
|
end
|
2021-02-02 21:04:53 +00:00
|
|
|
end
|