houdini/app/models/event.rb

120 lines
3.8 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 Event < ApplicationRecord
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)
# :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
# :organizer_email, # string
# :receipt_message # text
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
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
has_many :event_discounts, dependent: :destroy
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
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
has_one_attached_with_sizes :main_image, {normal: 400, thumb: 100}
has_one_attached_with_sizes :background_image, {normal: [1000, 600]}
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-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
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) }
validates :slug, uniqueness: { scope: :nonprofit_id, message: 'You already have a campaign with that name.' }
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
self
end
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
self
end
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
def url
"#{nonprofit.url}/events/#{slug}"
end
def full_address
Format::Address.full_address(address, city, state_code, zip_code)
end
end