diff --git a/app/models/campaign_gift.rb b/app/models/campaign_gift.rb index 5bd4a0d6..9d3a3f17 100644 --- a/app/models/campaign_gift.rb +++ b/app/models/campaign_gift.rb @@ -10,6 +10,7 @@ class CampaignGift < ApplicationRecord belongs_to :donation belongs_to :campaign_gift_option + has_one :modern_campaign_gift validates :donation, presence: true validates :campaign_gift_option, presence: true diff --git a/app/models/campaign_gift_purchase.rb b/app/models/campaign_gift_purchase.rb index 63ce969c..390201ee 100644 --- a/app/models/campaign_gift_purchase.rb +++ b/app/models/campaign_gift_purchase.rb @@ -4,9 +4,17 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class CampaignGiftPurchase < ApplicationRecord belongs_to :campaign - has_many :campaign_gifts, class_name: 'ModernCampaignGift' + add_builder_expansion :nonprofit, :supporter, :campaign + add_builder_expansion :trx, + json_attrib: :transaction + + has_one :transaction_assignment, as: :assignable + has_one :trx, through: :transaction_assignment + has_one :supporter, through: :trx + has_one :nonprofit, through: :supporter + validates :amount, presence: true validates :campaign, presence: true validates :campaign_gifts, length: { minimum: 1 } diff --git a/app/models/modern_campaign_gift.rb b/app/models/modern_campaign_gift.rb index 4bd9934c..e0825432 100644 --- a/app/models/modern_campaign_gift.rb +++ b/app/models/modern_campaign_gift.rb @@ -7,12 +7,56 @@ class ModernCampaignGift < ApplicationRecord include Model::Jbuilder include Model::Eventable setup_houid :cgift - + add_builder_expansion :nonprofit, :supporter, :campaign, :campaign_gift_option + add_builder_expansion :trx, + json_attrib: :transaction + belongs_to :campaign_gift_purchase belongs_to :legacy_campaign_gift, class_name: 'CampaignGift', foreign_key: :campaign_gift_id, inverse_of: :modern_campaign_gift + 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 + + after_discard :publish_deleted + validates :amount, presence: true validates :legacy_campaign_gift, presence: true validates :campaign_gift_purchase, presence: true + # TODO replace with discard gem + def discard! + run_callbacks(:discard) do + self.deleted = true + save! + end + end + + def to_builder(*expand) + init_builder(*expand) do + json.(self, :id, :name, :deleted) + json.object 'campaign_gift' + json.amount do + json.value_in_cents amount + json.currency nonprofit.currency + end + end + end + + def publish_created + Houdini.event_publisher.announce(:campaign_gift_created, to_event('campaign_gift.created', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option).attributes!) + end + + def publish_updated + Houdini.event_publisher.announce(:campaign_gift_updated, to_event('campaign_gift.updated', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option).attributes!) + end + + def publish_deleted + Houdini.event_publisher.announce(:campaign_gift_deleted, to_event('campaign_gift.deleted', :nonprofit, :supporter, :trx, :campaign, :campaign_gift_option).attributes!) + end end diff --git a/spec/factories/campaign_gift_purchases.rb b/spec/factories/campaign_gift_purchases.rb new file mode 100644 index 00000000..f6668190 --- /dev/null +++ b/spec/factories/campaign_gift_purchases.rb @@ -0,0 +1,11 @@ +# 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 +FactoryBot.define do + factory :campaign_gift_purchase do + deleted { false } + amount { 1 } + campaign { nil } + end +end diff --git a/spec/factories/modern_campaign_gifts.rb b/spec/factories/modern_campaign_gifts.rb new file mode 100644 index 00000000..ace2c655 --- /dev/null +++ b/spec/factories/modern_campaign_gifts.rb @@ -0,0 +1,11 @@ +# 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 +FactoryBot.define do + factory :modern_campaign_gift do + deleted { false } + campaign_gift { "" } + amount { 1 } + end +end diff --git a/spec/models/campaign_gift_purchase_spec.rb b/spec/models/campaign_gift_purchase_spec.rb new file mode 100644 index 00000000..09076fb4 --- /dev/null +++ b/spec/models/campaign_gift_purchase_spec.rb @@ -0,0 +1,9 @@ +# 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 +require 'rails_helper' + +RSpec.describe CampaignGiftPurchase, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/modern_campaign_gift_spec.rb b/spec/models/modern_campaign_gift_spec.rb new file mode 100644 index 00000000..9766fdbe --- /dev/null +++ b/spec/models/modern_campaign_gift_spec.rb @@ -0,0 +1,9 @@ +# 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 +require 'rails_helper' + +RSpec.describe ModernCampaignGift, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end