houdini/app/models/event_discount.rb

64 lines
2 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 EventDiscount < ApplicationRecord
2021-02-02 21:04:53 +00:00
include Model::Eventable
2021-02-12 21:29:55 +00:00
include Model::Jbuilder
# :code,
# :event_id,
# :name,
# :percent
validates :name, presence: true
validates :code, presence: true
validates :event, presence: true
validates :percent, presence: true, numericality: {only_integer: true, greater_than: 0, less_than_or_equal_to: 100}
# we use after_create_commit because the db could be in an inconsistent state and the messages will be slightly wrong
# we use after commit on the rest for consistency
after_create_commit :publish_create
after_destroy_commit :publish_delete
after_update_commit :publish_updated
belongs_to :event
has_many :tickets
2021-02-12 21:29:55 +00:00
has_one :nonprofit, through: :event
has_many :ticket_levels, through: :event
2021-01-11 22:34:48 +00:00
def to_builder(*expand)
2021-02-12 21:29:55 +00:00
init_builder(*expand) do |json|
json.(self, :name, :code)
json.deleted !persisted?
json.discount do
json.percent percent
end
json.add_builder_expansion :nonprofit, :event
json.add_builder_expansion :ticket_levels, enum_type: :expandable
# if expand.include? :ticket_levels
# json.ticket_levels ticket_levels do |tl|
# json.merge! tl.to_builder.attributes!
# end
# else
# json.ticket_levels ticket_levels.pluck(:id)
# end
end
end
private
def publish_create
Houdini.event_publisher.announce(:event_discount_created, to_event('event_discount.created', :event, :nonprofit, :ticket_levels).attributes!)
end
def publish_updated
Houdini.event_publisher.announce(:event_discount_updated, to_event('event_discount.updated', :event, :nonprofit, :ticket_levels).attributes!)
end
def publish_delete
Houdini.event_publisher.announce(:event_discount_deleted, to_event('event_discount.deleted', :event, :nonprofit, :ticket_levels).attributes!)
end
end