2019-07-30 21:29:24 +00:00
|
|
|
# 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
|
2019-02-01 19:40:24 +00:00
|
|
|
class CampaignGiftOption < ApplicationRecord
|
2021-02-02 21:04:53 +00:00
|
|
|
include Model::Eventable
|
|
|
|
include Model::Jbuilder
|
|
|
|
|
2019-08-06 12:36:31 +00:00
|
|
|
# :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)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
belongs_to :campaign
|
|
|
|
has_many :campaign_gifts
|
|
|
|
has_many :donations, through: :campaign_gifts
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
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
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-01-15 23:10:37 +00:00
|
|
|
after_create_commit :publish_created
|
|
|
|
after_update_commit :publish_updated
|
|
|
|
after_destroy_commit :publish_deleted
|
|
|
|
|
2021-02-09 22:25:55 +00:00
|
|
|
add_builder_expansion :campaign, :nonprofit
|
2021-02-02 21:04:53 +00:00
|
|
|
|
2021-02-09 22:25:55 +00:00
|
|
|
has_one :nonprofit, through: :campaign
|
2021-02-02 21:04:53 +00:00
|
|
|
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def total_gifts
|
|
|
|
campaign_gifts.count
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def as_json(options = {})
|
|
|
|
h = super(options)
|
|
|
|
h[:total_gifts] = total_gifts
|
|
|
|
h
|
|
|
|
end
|
2021-01-15 23:10:37 +00:00
|
|
|
|
|
|
|
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|
|
2021-01-15 23:10:37 +00:00
|
|
|
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|
|
2021-02-09 22:25:55 +00:00
|
|
|
json.amount do
|
|
|
|
json.currency desc[:amount][:currency]
|
|
|
|
json.value_in_cents desc[:amount][:value_in_cents]
|
|
|
|
end
|
2021-02-10 20:08:47 +00:00
|
|
|
json.recurrence do
|
|
|
|
json.interval desc[:recurrence][:interval]
|
|
|
|
json.type desc[:recurrence][:type]
|
|
|
|
end if desc[:recurrence]
|
2021-01-15 23:10:37 +00:00
|
|
|
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
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|