2021-02-08 22:33:19 +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 CampaignGiftPurchase < ApplicationRecord
|
2021-02-10 23:48:18 +00:00
|
|
|
include Model::TrxAssignable
|
2021-02-09 22:25:55 +00:00
|
|
|
|
|
|
|
setup_houid :cgpur
|
|
|
|
|
2021-02-08 22:33:19 +00:00
|
|
|
belongs_to :campaign
|
|
|
|
has_many :campaign_gifts, class_name: 'ModernCampaignGift'
|
|
|
|
|
2021-02-10 23:48:18 +00:00
|
|
|
add_builder_expansion :campaign
|
2021-02-08 22:49:59 +00:00
|
|
|
|
2021-02-10 20:38:41 +00:00
|
|
|
|
|
|
|
# TODO replace with Discard gem
|
|
|
|
define_model_callbacks :discard
|
|
|
|
|
2021-02-08 22:33:19 +00:00
|
|
|
validates :amount, presence: true
|
|
|
|
validates :campaign, presence: true
|
|
|
|
validates :campaign_gifts, length: { minimum: 1 }
|
|
|
|
|
2021-02-10 20:38:41 +00:00
|
|
|
# TODO replace with discard gem
|
|
|
|
def discard!
|
|
|
|
run_callbacks(:discard) do
|
|
|
|
self.deleted = true
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-09 22:25:55 +00:00
|
|
|
def to_builder(*expand)
|
|
|
|
init_builder(*expand) do |json|
|
|
|
|
json.(self, :id, :deleted)
|
|
|
|
json.object 'campaign_gift_purchase'
|
|
|
|
|
|
|
|
json.amount do
|
|
|
|
json.value_in_cents amount
|
|
|
|
json.currency nonprofit.currency
|
|
|
|
end
|
|
|
|
|
|
|
|
if expand.include? :campaign_gifts
|
|
|
|
json.campaign_gifts campaign_gifts do |gift|
|
2021-02-10 20:38:41 +00:00
|
|
|
json.merge! gift.to_builder.attributes!
|
2021-02-09 22:25:55 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
json.campaign_gifts campaign_gifts.pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-10 20:38:41 +00:00
|
|
|
|
|
|
|
def publish_created
|
|
|
|
Houdini.event_publisher.announce(:campaign_gift_purchase_created, to_event('campaign_gift_purchase.created', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_updated
|
|
|
|
Houdini.event_publisher.announce(:campaign_gift_purchase_updated, to_event('campaign_gift_purchase.updated', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_deleted
|
|
|
|
Houdini.event_publisher.announce(:campaign_gift_purchase_deleted, to_event('campaign_gift_purchase.deleted', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!)
|
|
|
|
end
|
2021-02-08 22:33:19 +00:00
|
|
|
end
|