osem-fcy/app/models/venue.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Venue < ApplicationRecord
belongs_to :conference
has_one :commercial, as: :commercialable, dependent: :destroy
2015-11-07 11:43:36 +02:00
has_many :rooms, dependent: :destroy
2013-01-07 09:04:09 +01:00
before_create :generate_guid
has_paper_trail ignore: [:updated_at, :guid], meta: { conference_id: :conference_id }
2016-04-03 22:08:46 +05:30
accepts_nested_attributes_for :commercial, allow_destroy: true
validates :name, :street, :city, :country, presence: true
2016-04-27 15:59:34 +02:00
mount_uploader :picture, PictureUploader, mount_on: :photo_file_name
before_save :send_mail_notification
2014-11-12 12:12:57 +01:00
def address
"#{street}, #{city}, #{country_name}"
end
def country_name
I18nData.countries[country]
end
2016-02-08 12:25:42 +01:00
def location?
!(latitude.blank? || longitude.blank?)
end
2014-11-12 12:12:57 +01:00
private
def send_mail_notification
ConferenceVenueUpdateMailJob.perform_later(conference) if notify_on_venue_changed?
2014-11-12 12:12:57 +01:00
end
2016-03-31 16:51:16 +02:00
def notify_on_venue_changed?
2016-04-30 17:56:23 +02:00
return false unless conference.try(:email_settings).try(:send_on_venue_updated)
2016-03-31 16:51:16 +02:00
# do not notify unless the address changed
return false unless saved_change_to_name? || saved_change_to_street? || saved_change_to_city? || saved_change_to_country?
2018-11-13 18:01:49 -08:00
2016-03-31 16:51:16 +02:00
# do not notify unless the mail content is set up
(!conference.email_settings.venue_updated_subject.blank? && !conference.email_settings.venue_updated_body.blank?)
2014-08-12 16:04:24 +05:30
end
2014-07-21 14:27:32 +02:00
# TODO: create a module to be mixed into model to perform same operation
# event.rb has same functionality which can be shared
# TODO: rename guid to UUID as guid is specifically Microsoft term
2013-01-07 09:04:09 +01:00
def generate_guid
2014-07-21 14:27:32 +02:00
loop do
@guid = SecureRandom.urlsafe_base64
break unless Venue.where(guid: guid).any?
2014-07-21 14:27:32 +02:00
end
self.guid = @guid
2013-01-07 09:04:09 +01:00
end
end