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 EventDiscount < ApplicationRecord
|
2021-02-02 21:04:53 +00:00
|
|
|
include Model::Eventable
|
2021-02-12 21:29:55 +00:00
|
|
|
include Model::Jbuilder
|
|
|
|
|
2019-08-06 14:05:24 +00:00
|
|
|
# :code,
|
|
|
|
# :event_id,
|
|
|
|
# :name,
|
|
|
|
# :percent
|
2021-01-13 22:59:04 +00:00
|
|
|
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
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
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)
|
2021-01-13 22:59:04 +00:00
|
|
|
json.deleted !persisted?
|
|
|
|
json.discount do
|
|
|
|
json.percent percent
|
|
|
|
end
|
|
|
|
|
2021-04-07 21:43:58 +00:00
|
|
|
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
|
2021-01-13 22:59:04 +00:00
|
|
|
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
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|