osem-fcy/app/models/venue.rb

52 lines
1.6 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class Venue < ActiveRecord::Base
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
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
name = ISO3166::Country[country]
name.name if name
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 self.name_changed? || self.street_changed? || self.city_changed? || self.country_changed?
# 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
2014-07-21 14:49:05 +02:00
break if !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