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

View file

@ -71,13 +71,13 @@ class CampaignGiftOption < ApplicationRecord
end end
init_builder(*expand) do |json| init_builder(*expand) do |json|
json.(self, :id, :name, :description, json.(self, :name, :description,
:hide_contributions, :order, :to_ship) :hide_contributions, :order, :to_ship)
if quantity if quantity
json.quantity quantity json.quantity quantity
end end
json.object 'campaign_gift_option'
json.deleted !persisted? json.deleted !persisted?
json.gift_option_amount gift_option_amount do |desc| json.gift_option_amount gift_option_amount do |desc|

View file

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

View file

@ -82,6 +82,8 @@ module Model::Jbuilder
def init_builder(*expand) def init_builder(*expand)
builder_expansions = self.class.builder_expansions builder_expansions = self.class.builder_expansions
Jbuilder.new do | json| Jbuilder.new do | json|
json.(self, :id)
json.object self.class.name.underscore
builder_expansions.keys.each do |k| builder_expansions.keys.each do |k|
if expand.include? k if expand.include? k
json.set! builder_expansions.get_by_key(k).json_attrib, builder_expansions.get_by_key(k).to_expand.(self) 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) def to_builder(*expand)
init_builder(*expand) do |json| init_builder(*expand) do |json|
json.(self, :id, :name, :deleted) json.(self, :name, :deleted)
json.object 'custom_field_definition' json.object 'custom_field_definition'
end end
end end

View file

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

View file

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

View file

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

View file

@ -13,7 +13,7 @@ class ModernDonation < ApplicationRecord
def to_builder(*expand) def to_builder(*expand)
init_builder(*expand) do |json| init_builder(*expand) do |json|
json.(self, :id, :designation) json.(self, :designation)
json.object 'donation' json.object 'donation'
json.dedication do 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 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 Image::AttachmentExtensions
include Model::Jbuilder
# :name, # str # :name, # str
# :stripe_account_id, # str # :stripe_account_id, # str
# :summary, # text: paragraph-sized organization summary # :summary, # text: paragraph-sized organization summary
@ -247,9 +248,8 @@ class Nonprofit < ApplicationRecord
end end
def to_builder(*expand) def to_builder(*expand)
Jbuilder.new do |json| init_builder(*expand) do |json|
json.(self, :id, :name) json.(self, :name)
json.object 'nonprofit'
end end
end end

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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