2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-09-05 22:23:34 +03:00
|
|
|
class Venue < ApplicationRecord
|
2014-11-20 14:57:03 +01:00
|
|
|
belongs_to :conference
|
2016-04-03 19:25:41 +05:30
|
|
|
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
|
2014-11-20 14:57:03 +01:00
|
|
|
|
2016-05-24 23:54:46 +05:30
|
|
|
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
|
2014-11-20 14:57:03 +01:00
|
|
|
validates :name, :street, :city, :country, presence: true
|
|
|
|
|
|
2016-04-27 15:59:34 +02:00
|
|
|
mount_uploader :picture, PictureUploader, mount_on: :photo_file_name
|
2014-11-20 14:57:03 +01:00
|
|
|
|
2016-04-21 16:03:09 +02:00
|
|
|
before_save :send_mail_notification
|
2014-11-12 12:12:57 +01:00
|
|
|
|
2014-11-20 14:57:03 +01:00
|
|
|
def address
|
2014-12-05 16:04:34 +01:00
|
|
|
"#{street}, #{city}, #{country_name}"
|
2014-11-20 14:57:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def country_name
|
|
|
|
|
name = ISO3166::Country[country]
|
2018-05-08 14:17:30 -07:00
|
|
|
name&.name
|
2014-11-20 14:57:03 +01:00
|
|
|
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
|
2016-04-21 16:03:09 +02:00
|
|
|
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
|
2017-03-28 13:27:52 +05:30
|
|
|
return false unless name_changed? || street_changed? || city_changed? || country_changed?
|
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
|
2016-08-06 00:57:07 +02:00
|
|
|
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
|