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 Event < ApplicationRecord
|
2020-05-12 21:11:44 +00:00
|
|
|
include Image::AttachmentExtensions
|
2021-02-02 21:04:53 +00:00
|
|
|
include Model::Jbuilder
|
|
|
|
add_builder_expansion :nonprofit
|
2019-08-06 14:05:43 +00:00
|
|
|
# :deleted, #bool for soft-delete
|
|
|
|
# :name, # str
|
|
|
|
# :tagline, # str
|
|
|
|
# :summary, # text
|
|
|
|
# :body, # text (html)
|
2019-07-30 21:29:24 +00:00
|
|
|
# :end_datetime,
|
|
|
|
# :start_datetime,
|
2019-08-06 14:05:43 +00:00
|
|
|
# :location, # str
|
|
|
|
# :city, # str
|
|
|
|
# :state_code, # str
|
|
|
|
# :address, # str
|
|
|
|
# :zip_code, # str
|
|
|
|
# :main_image, # str
|
|
|
|
# :remove_main_image, # for carrierwave
|
|
|
|
# :background_image, # str
|
|
|
|
# :remove_background_image, # bool carrierwave
|
|
|
|
# :published, # bool
|
|
|
|
# :slug, # str
|
|
|
|
# :directions, # text
|
|
|
|
# :venue_name, # str
|
|
|
|
# :profile_id, # creator
|
|
|
|
# :ticket_levels_attributes,
|
|
|
|
# :show_total_raised, # bool
|
|
|
|
# :show_total_count, # bool
|
|
|
|
# :hide_activity_feed, # bool
|
|
|
|
# :nonprofit_id, # host
|
|
|
|
# :hide_title, # bool
|
2019-07-30 21:29:24 +00:00
|
|
|
# :organizer_email, # string
|
|
|
|
# :receipt_message # text
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :end_datetime, presence: true
|
|
|
|
validates :start_datetime, presence: true
|
|
|
|
validates :address, presence: true
|
|
|
|
validates :city, presence: true
|
|
|
|
validates :state_code, presence: true
|
|
|
|
validates :slug, presence: true, uniqueness: { scope: :nonprofit_id, message: 'You already have an event with that URL' }
|
|
|
|
validates :nonprofit_id, presence: true
|
|
|
|
validates :profile_id, presence: true
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
belongs_to :nonprofit
|
|
|
|
belongs_to :profile
|
|
|
|
has_many :donations
|
|
|
|
has_many :charges, through: :tickets
|
|
|
|
has_many :supporters, through: :donations
|
|
|
|
has_many :recurring_donations
|
|
|
|
has_many :ticket_levels, dependent: :destroy
|
2018-03-25 17:30:42 +00:00
|
|
|
has_many :event_discounts, dependent: :destroy
|
2019-07-30 21:29:24 +00:00
|
|
|
has_many :tickets
|
|
|
|
has_many :payments, through: :tickets
|
|
|
|
has_many :roles, as: :host, dependent: :destroy
|
|
|
|
has_many :comments, as: :host, dependent: :destroy
|
|
|
|
has_many :activities, as: :host, dependent: :destroy
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-02-04 01:18:07 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
accepts_nested_attributes_for :ticket_levels, allow_destroy: true
|
2020-04-29 22:22:13 +00:00
|
|
|
has_one_attached :main_image
|
|
|
|
has_one_attached :background_image
|
2020-05-12 21:11:44 +00:00
|
|
|
|
|
|
|
has_one_attached_with_sizes :main_image, {normal: 400, thumb: 100}
|
|
|
|
has_one_attached_with_sizes :background_image, {normal: [1000, 600]}
|
2020-05-15 17:32:47 +00:00
|
|
|
|
2020-06-10 22:31:47 +00:00
|
|
|
has_one_attached_with_default(:main_image, Houdini.defaults.image.profile,
|
|
|
|
filename: "main_image_#{SecureRandom.uuid}#{Pathname.new(Houdini.defaults.image.profile).extname}")
|
2020-05-15 17:32:47 +00:00
|
|
|
|
2020-06-10 22:31:47 +00:00
|
|
|
has_one_attached_with_default(:background_image, Houdini.defaults.image.campaign,
|
|
|
|
filename: "background_image_#{SecureRandom.uuid}#{Pathname.new(Houdini.defaults.image.campaign).extname}")
|
2020-04-29 22:22:13 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
scope :not_deleted, -> { where(deleted: [nil, false]) }
|
|
|
|
scope :deleted, -> { where(deleted: true) }
|
|
|
|
scope :published, -> { where(published: true) }
|
|
|
|
scope :upcoming, -> { where('start_datetime >= ?', Date.today).published }
|
|
|
|
scope :past, -> { where('end_datetime < ?', Date.today).published }
|
|
|
|
scope :unpublished, -> { where('published != ?', true) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
validates :slug, uniqueness: { scope: :nonprofit_id, message: 'You already have a campaign with that name.' }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before_validation(on: :create) do
|
|
|
|
self.slug = Format::Url.convert_to_slug(name) unless slug
|
|
|
|
self.published = false if published.nil?
|
|
|
|
self.total_raised ||= 0
|
2018-03-25 17:30:42 +00:00
|
|
|
self
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
after_create do
|
|
|
|
user = profile.user
|
|
|
|
Role.create(name: :event_editor, user_id: user.id, host: self)
|
2019-11-07 21:35:22 +00:00
|
|
|
EventCreateJob.perform_later self
|
2019-07-30 21:29:24 +00:00
|
|
|
self
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-01-11 22:34:48 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def url
|
|
|
|
"#{nonprofit.url}/events/#{slug}"
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def full_address
|
2019-07-30 21:29:24 +00:00
|
|
|
Format::Address.full_address(address, city, state_code, zip_code)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|