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 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-04-07 21:43:58 +00:00
|
|
|
|
|
|
|
def to_id
|
|
|
|
::Jbuilder.new do |json|
|
|
|
|
json.id id
|
|
|
|
json.object 'campaign_gift_purchase'
|
|
|
|
json.type 'trx_assignment'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-09 22:25:55 +00:00
|
|
|
def to_builder(*expand)
|
|
|
|
init_builder(*expand) do |json|
|
2021-02-12 21:29:55 +00:00
|
|
|
json.(self, :deleted)
|
2021-04-07 21:43:58 +00:00
|
|
|
json.type 'trx_assignment'
|
2021-02-09 22:25:55 +00:00
|
|
|
|
|
|
|
json.amount do
|
2021-02-26 20:23:29 +00:00
|
|
|
json.cents amount
|
2021-02-09 22:25:55 +00:00
|
|
|
json.currency nonprofit.currency
|
|
|
|
end
|
|
|
|
|
2021-04-07 21:43:58 +00:00
|
|
|
json.add_builder_expansion :campaign, :nonprofit, :supporter
|
|
|
|
json.add_builder_expansion :trx, json_attribute: "transaction"
|
|
|
|
json.add_builder_expansion :campaign_gifts, enum_type: :expandable
|
2021-02-09 22:25:55 +00:00
|
|
|
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
|