Make init_builder do more

This commit is contained in:
Eric Schultz 2021-02-12 15:29:55 -06:00 committed by Eric Schultz
parent a538f25ecd
commit 7bab0ec5c5
20 changed files with 67 additions and 90 deletions

View file

@ -4,6 +4,7 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class Campaign < ApplicationRecord
include Image::AttachmentExtensions
include Model::Jbuilder
# :name,
# :tagline,
# :slug, # str: url name
@ -39,6 +40,8 @@ class Campaign < ApplicationRecord
# :reason_for_supporting,
# :default_reason_for_supporting
add_builder_expansion :nonprofit
validate :end_datetime_cannot_be_in_past, on: :create
validates :profile, presence: true
validates :nonprofit, presence: true
@ -197,10 +200,8 @@ class Campaign < ApplicationRecord
end
def to_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :name)
json.object "campaign"
json.nonprofit nonprofit.id
init_builder(*expand) do |json|
json.(self, :name)
end
end

View file

@ -71,13 +71,13 @@ class CampaignGiftOption < ApplicationRecord
end
init_builder(*expand) do |json|
json.(self, :id, :name, :description,
json.(self, :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|

View file

@ -30,8 +30,7 @@ class CampaignGiftPurchase < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :deleted)
json.object 'campaign_gift_purchase'
json.(self, :deleted)
json.amount do
json.value_in_cents amount

View file

@ -82,6 +82,8 @@ module Model::Jbuilder
def init_builder(*expand)
builder_expansions = self.class.builder_expansions
Jbuilder.new do | json|
json.(self, :id)
json.object self.class.name.underscore
builder_expansions.keys.each do |k|
if expand.include? k
json.set! builder_expansions.get_by_key(k).json_attrib, builder_expansions.get_by_key(k).to_expand.(self)

View file

@ -40,7 +40,7 @@ class CustomFieldMaster < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :name, :deleted)
json.(self, :name, :deleted)
json.object 'custom_field_definition'
end
end

View file

@ -100,12 +100,8 @@ class Event < ApplicationRecord
end
def to_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :name)
json.object "event"
json.nonprofit expand.include?(:nonprofit) && nonprofit ?
nonprofit.to_builder :
nonprofit && nonprofit.id
init_builder(*expand) do |json|
json.(self, :name)
end
end

View file

@ -4,6 +4,9 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class EventDiscount < ApplicationRecord
include Model::Eventable
include Model::Jbuilder
add_builder_expansion :nonprofit, :event
# :code,
# :event_id,
# :name,
@ -22,39 +25,23 @@ class EventDiscount < ApplicationRecord
belongs_to :event
has_many :tickets
has_one :nonprofit, through: :event
has_many :ticket_levels, through: :event
def to_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :name, :code)
init_builder(*expand) do |json|
json.(self, :name, :code)
json.deleted !persisted?
json.object 'event_discount'
json.discount do
json.percent percent
end
if event
if expand.include? :event
json.event event.to_builder
else
json.event event.id
end
if event.nonprofit
if expand.include? :nonprofit
json.nonprofit event.nonprofit.to_builder
else
json.nonprofit event.nonprofit.id
end
else
json.nonprofit nil
end
if expand.include? :ticket_levels
json.ticket_levels event.ticket_levels do |tl|
json.ticket_levels ticket_levels do |tl|
json.merge! tl.to_builder.attributes!
end
else
json.ticket_levels event.ticket_levels.pluck(:id)
end
json.ticket_levels ticket_levels.pluck(:id)
end
end
end

View file

@ -40,7 +40,7 @@ class ModernCampaignGift < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :deleted)
json.(self, :deleted)
json.object 'campaign_gift'
json.amount do
json.value_in_cents amount

View file

@ -13,7 +13,7 @@ class ModernDonation < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :designation)
json.(self, :designation)
json.object 'donation'
json.dedication do

View file

@ -7,6 +7,7 @@ class Nonprofit < ApplicationRecord
Categories = ['Public Benefit', 'Human Services', 'Education', 'Civic Duty', 'Human Rights', 'Animals', 'Environment', 'Health', 'Arts, Culture, Humanities', 'International', 'Children', 'Religion', 'LGBTQ', "Women's Rights", 'Disaster Relief', 'Veterans'].freeze
include Image::AttachmentExtensions
include Model::Jbuilder
# :name, # str
# :stripe_account_id, # str
# :summary, # text: paragraph-sized organization summary
@ -247,9 +248,8 @@ class Nonprofit < ApplicationRecord
end
def to_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :name)
json.object 'nonprofit'
init_builder(*expand) do |json|
json.(self, :name)
end
end

View file

@ -89,8 +89,7 @@ class Supporter < ApplicationRecord
def to_builder(*expand)
supporter_addresses = [self]
init_builder(*expand) do |json|
json.object "supporter"
json.(self, :id, :name, :organization, :phone, :anonymous, :deleted)
json.(self, :name, :organization, :phone, :anonymous, :deleted)
if expand.include? :supporter_address
json.supporter_addresses supporter_addresses do |i|
json.merge! i.to_supporter_address_builder.attributes!
@ -112,14 +111,20 @@ class Supporter < ApplicationRecord
end
def to_supporter_address_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :address, :state_code, :city, :country, :zip_code, :deleted)
init_builder(*expand) do |json|
json.(self, :address, :state_code, :city, :country, :zip_code, :deleted)
json.object 'supporter_address'
if expand.include? :supporter
json.supporter to_builder
else
json.supporter id
end
if expand.include? :nonprofit
json.nonprofit nonprofit.to_builder
else
json.nonprofit nonprofit.id
end
end
end

View file

@ -40,8 +40,7 @@ class SupporterNote < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :deleted, :content)
json.object 'supporter_note'
json.(self, :deleted, :content)
end
end

View file

@ -44,7 +44,7 @@ class TagMaster < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :name, :deleted)
json.(self, :name, :deleted)
json.object 'tag_definition'
end
end

View file

@ -16,6 +16,8 @@ class TicketLevel < ApplicationRecord
# :limit, #int: for limiting the number of tickets to be sold
# :order #int: order in which to be displayed
add_builder_expansion :nonprofit, :event
# TODO replace with Discard gem
define_model_callbacks :discard
@ -29,7 +31,6 @@ class TicketLevel < ApplicationRecord
has_many :tickets
belongs_to :event
has_one :nonprofit, through: :event
has_many :event_discounts, through: :event
validates :name, presence: true
validates :event_id, presence: true
@ -38,6 +39,12 @@ class TicketLevel < ApplicationRecord
scope :not_deleted, -> { where(deleted: [false, nil]) }
# has_many didn't work here, don't know why offhand.
def event_discounts
event.event_discounts
end
before_validation do
self.amount = Format::Currency.dollars_to_cents(amount_dollars) if amount_dollars.present?
self.amount = 0 if amount.nil?
@ -52,38 +59,20 @@ class TicketLevel < ApplicationRecord
end
def to_builder(*expand)
Jbuilder.new do |json|
json.(self, :id, :name, :deleted, :order, :limit, :description)
json.object 'ticket_level'
init_builder(*expand) do |json|
json.(self, :name, :deleted, :order, :limit, :description)
json.amount do
json.value_in_cents amount || 0
json.currency event.nonprofit.currency
end
json.available_to admin_only ? 'admins' : 'everyone'
if event
if event.nonprofit
if expand.include? :nonprofit
json.nonprofit event.nonprofit.to_builder
else
json.nonprofit event.nonprofit.id
end
else
json.nonprofit nil
end
if expand.include? :event
json.event event.to_builder
else
json.event event.id
end
if expand.include? :event_discounts
json.event_discounts event.event_discounts do |disc|
json.event_discounts event_discounts do |disc|
json.merge! disc.to_builder.attributes!
end
else
json.event_discounts event.event_discounts.pluck(:id)
end
json.event_discounts event_discounts.pluck(:id)
end
end
end

View file

@ -23,8 +23,6 @@ class TicketPurchase < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id)
json.object 'ticket_purchase'
json.original_discount do
json.percent original_discount
end if original_discount

View file

@ -29,7 +29,7 @@ class TicketToLegacyTicket < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id, :checked_in, :deleted, :note)
json.(self, :checked_in, :deleted, :note)
json.object "ticket"
json.amount do

View file

@ -24,9 +24,6 @@ class Transaction < ApplicationRecord
def to_builder(*expand)
init_builder(*expand) do |json|
json.(self, :id)
json.object 'transaction'
json.amount do
json.value_in_cents amount || 0
json.currency nonprofit.currency

View file

@ -219,7 +219,7 @@ RSpec.describe EventDiscount, type: :model do
'available_to' => 'everyone',
'nonprofit' => nonprofit.id,
'event' => event.id,
'event_discounts' => [kind_of(Numeric)]
'event_discounts' => []
}
]
}

View file

@ -76,7 +76,8 @@ RSpec.describe Supporter, type: :model do
'type' => 'supporter_address.created',
'data' => {
'object' => supporter_address_to_builder_base.merge({
'supporter' => supporter_to_builder_base
'supporter' => supporter_to_builder_base,
'nonprofit' => nonprofit_to_builder_base
})
}
}).ordered
@ -95,7 +96,8 @@ RSpec.describe Supporter, type: :model do
supporter_address_result = supporter_address_to_builder_base.merge({
'deleted' => true,
'supporter'=> supporter_to_builder_base.merge({'deleted' => true})
'supporter'=> supporter_to_builder_base.merge({'deleted' => true}),
'nonprofit' => nonprofit_to_builder_base
})
expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_deleted, {
@ -147,7 +149,8 @@ RSpec.describe Supporter, type: :model do
'data' => {
'object' => supporter_address_to_builder_base.merge({
'city' => 'new_city',
'supporter'=> supporter_to_builder_base
'supporter'=> supporter_to_builder_base,
'nonprofit' => nonprofit_to_builder_base
})
}}).ordered

View file

@ -74,7 +74,8 @@ RSpec.shared_context :shared_donation_charge_context do
'zip_code' => nil,
'country' => 'United States',
'object' => 'supporter_address',
'supporter' => kind_of(Numeric)
'supporter' => kind_of(Numeric),
'nonprofit'=> nonprofit.id
}
end