houdini/app/models/transaction.rb

39 lines
1.2 KiB
Ruby
Raw Normal View History

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
include Model::Jbuilder
include Model::Eventable
2021-02-02 21:04:53 +00:00
setup_houid :trx
add_builder_expansion :nonprofit, :supporter
2021-02-02 21:04:53 +00:00
belongs_to :supporter
has_one :nonprofit, through: :supporter
2021-02-02 21:04:53 +00:00
has_many :transaction_assignments
2021-02-08 20:55:31 +00:00
has_many :donations, through: :transaction_assignments, source: :assignable, source_type: 'ModernDonation'
2021-02-02 21:04:53 +00:00
has_many :ticket_purchases, through: :transaction_assignments, source: :assignable, source_type: 'TicketPurchase'
has_many :campaign_gift_purchases, through: :transaction_assignments, source: :assignable, source_type: 'CampaignGiftPurchase'
2021-02-02 21:04:53 +00:00
validates :supporter, presence: true
def to_builder(*expand)
init_builder(*expand) do |json|
2021-02-02 21:04:53 +00:00
json.amount do
json.value_in_cents amount || 0
json.currency nonprofit.currency
2021-02-02 21:04:53 +00:00
end
end
end
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