houdini/app/models/campaign_gift_option.rb

109 lines
3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-06-12 20:03:43 +00:00
# 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 CampaignGiftOption < ApplicationRecord
2021-02-02 21:04:53 +00:00
include Model::Eventable
include Model::Jbuilder
# :amount_one_time, #int (cents)
# :amount_recurring, #int (cents)
# :amount_dollars, #str, gets converted to amount
# :description, # text
# :name, # str
# :campaign, #assocation
# :quantity, #int (optional)
# :to_ship, #boolean
# :order, #int (optional)
# :hide_contributions #boolean (optional)
belongs_to :campaign
has_many :campaign_gifts
has_many :donations, through: :campaign_gifts
validates :name, presence: true
validates :campaign, presence: true
validates :amount_one_time, presence: true, numericality: { only_integer: true }, unless: :amount_recurring
validates :amount_recurring, presence: true, numericality: { only_integer: true }, unless: :amount_one_time
after_create_commit :publish_created
after_update_commit :publish_updated
after_destroy_commit :publish_deleted
add_builder_expansion :campaign, :nonprofit
2021-02-02 21:04:53 +00:00
has_one :nonprofit, through: :campaign
2021-02-02 21:04:53 +00:00
def total_gifts
campaign_gifts.count
end
def as_json(options = {})
h = super(options)
h[:total_gifts] = total_gifts
h
end
def to_builder(*expand)
gift_option_amount = []
if amount_one_time
gift_option_amount.push({
amount:{
value_in_cents: amount_one_time,
currency: campaign.nonprofit.currency
}
})
end
if amount_recurring
gift_option_amount.push({
amount:{
value_in_cents: amount_recurring,
currency: campaign.nonprofit.currency
},
recurrence: {
type: 'monthly',
interval: 1
}
})
end
2021-02-02 21:04:53 +00:00
init_builder(*expand) do |json|
json.(self, :id, :name, :description,
:hide_contributions, :order, :to_ship)
if quantity
json.quantity quantity
end
json.object 'campaign_gift_option'
json.deleted !persisted?
json.gift_option_amount gift_option_amount do |desc|
json.amount do
json.currency desc[:amount][:currency]
json.value_in_cents desc[:amount][:value_in_cents]
end
json.recurrence do
json.interval desc[:recurrence][:interval]
json.type desc[:recurrence][:type]
end if desc[:recurrence]
end
end
end
private
def publish_created
Houdini.event_publisher.announce(:campaign_gift_option_created, to_event('campaign_gift_option.created', :nonprofit, :campaign).attributes!)
end
def publish_updated
Houdini.event_publisher.announce(:campaign_gift_option_updated, to_event('campaign_gift_option.updated', :nonprofit, :campaign).attributes!)
end
def publish_deleted
Houdini.event_publisher.announce(:campaign_gift_option_deleted, to_event('campaign_gift_option.deleted', :nonprofit, :campaign).attributes!)
end
end