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 ModernCampaignGift < ApplicationRecord
|
|
|
|
include Model::Houidable
|
|
|
|
include Model::Jbuilder
|
|
|
|
include Model::Eventable
|
|
|
|
setup_houid :cgift
|
2021-02-09 22:25:55 +00:00
|
|
|
|
|
|
|
add_builder_expansion :nonprofit, :supporter, :campaign, :campaign_gift_option, :campaign_gift_purchase
|
2021-02-08 22:49:59 +00:00
|
|
|
add_builder_expansion :trx,
|
|
|
|
json_attrib: :transaction
|
|
|
|
|
2021-02-08 22:33:19 +00:00
|
|
|
belongs_to :campaign_gift_purchase
|
|
|
|
belongs_to :legacy_campaign_gift, class_name: 'CampaignGift', foreign_key: :campaign_gift_id, inverse_of: :modern_campaign_gift
|
|
|
|
|
2021-02-08 22:49:59 +00:00
|
|
|
has_one :campaign_gift_option, through: :legacy_campaign_gift
|
|
|
|
has_one :trx, through: :campaign_gift_purchase
|
|
|
|
has_one :supporter, through: :campaign_gift_purchase
|
|
|
|
has_one :nonprofit, through: :campaign_gift_purchase
|
|
|
|
has_one :campaign, through: :campaign_gift_purchase
|
|
|
|
|
|
|
|
# TODO replace with Discard gem
|
|
|
|
define_model_callbacks :discard
|
|
|
|
|
2021-02-09 22:25:55 +00:00
|
|
|
# after_discard :publish_deleted
|
2021-02-08 22:49:59 +00:00
|
|
|
|
2021-02-08 22:33:19 +00:00
|
|
|
validates :amount, presence: true
|
|
|
|
validates :legacy_campaign_gift, presence: true
|
|
|
|
validates :campaign_gift_purchase, presence: true
|
|
|
|
|
2021-02-08 22:49:59 +00:00
|
|
|
# TODO replace with discard gem
|
|
|
|
def discard!
|
|
|
|
run_callbacks(:discard) do
|
|
|
|
self.deleted = true
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_builder(*expand)
|
2021-02-09 22:25:55 +00:00
|
|
|
init_builder(*expand) do |json|
|
2021-02-12 21:29:55 +00:00
|
|
|
json.(self, :deleted)
|
2021-02-08 22:49:59 +00:00
|
|
|
json.object 'campaign_gift'
|
|
|
|
json.amount do
|
2021-02-24 23:34:59 +00:00
|
|
|
json.amount amount
|
2021-02-08 22:49:59 +00:00
|
|
|
json.currency nonprofit.currency
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_created
|
2021-02-09 22:25:55 +00:00
|
|
|
Houdini.event_publisher.announce(:campaign_gift_created, to_event('campaign_gift.created', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option, :campaign_gift_purchase).attributes!)
|
2021-02-08 22:49:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_updated
|
2021-02-09 22:25:55 +00:00
|
|
|
Houdini.event_publisher.announce(:campaign_gift_updated, to_event('campaign_gift.updated', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option, :campaign_gift_purchase).attributes!)
|
2021-02-08 22:49:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_deleted
|
2021-02-09 22:25:55 +00:00
|
|
|
Houdini.event_publisher.announce(:campaign_gift_deleted, to_event('campaign_gift.deleted', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option, :campaign_gift_purchase).attributes!)
|
2021-02-08 22:49:59 +00:00
|
|
|
end
|
2021-02-08 22:33:19 +00:00
|
|
|
end
|