houdini/app/models/campaign_gift_option.rb
2020-06-15 10:26:57 -05:00

35 lines
1.1 KiB
Ruby

# 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 CampaignGiftOption < ApplicationRecord
# :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
def total_gifts
campaign_gifts.count
end
def as_json(options = {})
h = super(options)
h[:total_gifts] = total_gifts
h
end
end