Adding callbacks and object_events

This commit is contained in:
Eric Schultz 2021-02-08 16:49:59 -06:00 committed by Eric Schultz
parent d27b0b66f4
commit 84ae414355
7 changed files with 95 additions and 2 deletions

View file

@ -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

View file

@ -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 }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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