2014-09-28 00:19:13 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
# This is the central ADL class, where are managed all events
|
2013-12-28 23:45:13 +01:00
|
|
|
class Event < ActiveRecord::Base
|
2014-06-27 00:52:47 +02:00
|
|
|
extend SimpleCalendar
|
2014-12-14 13:29:52 +01:00
|
|
|
strip_attributes
|
2016-02-27 17:42:07 +01:00
|
|
|
has_paper_trail ignore: [:last_updated, :secret, :submitter, :decision_time,
|
|
|
|
:lock_version, :latitude, :longitude]
|
2014-06-27 00:52:47 +02:00
|
|
|
|
2015-02-15 15:50:47 +01:00
|
|
|
belongs_to :region
|
2014-06-29 13:37:08 +02:00
|
|
|
has_many :notes, dependent: :destroy
|
2013-12-28 23:45:13 +01:00
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
validates :title, presence: true
|
2014-09-26 01:26:33 +02:00
|
|
|
validate :end_after_start
|
2014-08-06 14:47:47 +02:00
|
|
|
validates :description, presence: true
|
|
|
|
validates :city, presence: true
|
2015-02-15 15:50:47 +01:00
|
|
|
validates :region, presence: true
|
2014-09-26 01:26:33 +02:00
|
|
|
validates :url, presence: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2014-05-26 01:06:43 +02:00
|
|
|
validates :contact, email: true
|
|
|
|
validates :submitter, email: true
|
2014-12-14 21:48:52 +01:00
|
|
|
validates :tags, presence: true, format: /\A[\p{Alnum}\s-]*\z/
|
2014-01-01 21:26:35 +01:00
|
|
|
|
2014-10-16 23:31:02 +02:00
|
|
|
geocoded_by :full_address
|
2014-09-21 14:36:36 +02:00
|
|
|
# after_validation :geocode, if: -> (obj) { obj.address_changed? }
|
|
|
|
after_validation :geocode
|
2014-08-23 16:59:42 +02:00
|
|
|
|
2015-11-10 23:29:05 +01:00
|
|
|
# Mechanism to store some reason which can be used when sending notifications
|
2015-10-22 21:14:36 +02:00
|
|
|
attr_accessor :reason_for_deletion
|
|
|
|
|
|
|
|
after_create EventCallbacks
|
|
|
|
after_update EventCallbacks
|
|
|
|
after_destroy EventCallbacks
|
|
|
|
|
2015-12-08 00:43:26 +01:00
|
|
|
scope :moderated, ->(*) { where moderated: true }
|
|
|
|
scope :unmoderated, ->(*) { where moderated: false }
|
2014-09-05 22:15:38 +02:00
|
|
|
scope :last_year, -> { where '? <= end_time', 1.year.ago }
|
2015-04-18 17:24:15 +02:00
|
|
|
scope :past, -> { where 'start_time <= ?', Time.zone.now }
|
|
|
|
scope :future, -> { where '? <= end_time', Time.zone.now }
|
2015-11-10 23:40:56 +01:00
|
|
|
scope :daylimit, ->(d) { where 'end_time <= ?', d.to_i.days.from_now }
|
2014-08-06 14:47:47 +02:00
|
|
|
scope :year, (lambda do |year|
|
2014-06-27 00:52:47 +02:00
|
|
|
where '? <= end_time and start_time <= ?',
|
2015-11-10 23:29:05 +01:00
|
|
|
Date.new(year.to_i, 1, 1).beginning_of_week,
|
|
|
|
Date.new(year.to_i, 12, 31).end_of_week.end_of_day
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
|
|
|
scope :month, (lambda do |start_date|
|
2014-06-27 00:52:47 +02:00
|
|
|
where '? <= end_time and start_time <= ?',
|
2015-11-10 23:29:05 +01:00
|
|
|
start_date.to_date.beginning_of_month.beginning_of_week,
|
|
|
|
start_date.to_date.end_of_month.end_of_week.end_of_day
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
2015-11-10 23:29:05 +01:00
|
|
|
scope :period, (lambda do |year, week|
|
|
|
|
start_date = DateTime.commercial(year.to_i, week.to_i)
|
|
|
|
where '? <= end_time and start_time <= ?',
|
|
|
|
start_date, start_date.end_of_week.end_of_day
|
|
|
|
end)
|
2015-11-10 23:40:56 +01:00
|
|
|
scope :region, ->(region) { where region: region unless region == 'all' }
|
|
|
|
scope :locality, ->(locality) { where locality: locality }
|
|
|
|
scope :tag, ->(tag) { where 'tags like ?', "%#{tag}%" }
|
2014-08-23 17:21:11 +02:00
|
|
|
scope :geo, -> { where 'latitude is not null and longitude is not null' }
|
2013-12-29 23:25:38 +01:00
|
|
|
|
2014-09-26 01:26:33 +02:00
|
|
|
before_validation do
|
|
|
|
# Tags are always downcased
|
|
|
|
self.tags = tags.mb_chars.downcase if tags
|
|
|
|
end
|
|
|
|
|
2014-07-01 21:31:17 +02:00
|
|
|
before_validation on: :create do
|
2015-04-18 17:24:15 +02:00
|
|
|
self.submission_time = Time.zone.now
|
|
|
|
self.decision_time = Time.zone.now
|
2014-05-31 23:13:43 +02:00
|
|
|
|
2014-12-14 13:29:52 +01:00
|
|
|
# Populate submitter using contact info if absent
|
2015-10-22 22:28:25 +02:00
|
|
|
self.submitter = contact if submitter.blank?
|
2014-01-01 21:26:35 +01:00
|
|
|
end
|
|
|
|
|
2014-08-25 11:12:22 +02:00
|
|
|
before_validation on: :update do
|
|
|
|
if address_changed?
|
|
|
|
self.latitude = nil
|
|
|
|
self.longitude = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-25 23:59:03 +02:00
|
|
|
before_create do
|
|
|
|
self.secret = SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
self.moderator_mail_id = SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
self.submitter_mail_id = SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
end
|
|
|
|
|
2014-07-28 16:16:27 +02:00
|
|
|
before_update do
|
2015-10-22 21:14:36 +02:00
|
|
|
self.decision_time = Time.zone.now if moderated? && moderated_changed?
|
2014-07-28 16:16:27 +02:00
|
|
|
end
|
|
|
|
|
2014-09-03 02:14:21 +02:00
|
|
|
def as_json(_options = {})
|
2014-08-06 14:47:47 +02:00
|
|
|
{ type: 'Feature', properties: {
|
2015-11-10 23:37:23 +01:00
|
|
|
id: id, name: title, start_time: start_time, end_time: end_time,
|
|
|
|
place_name: place_name, address: address, city: city, locality: locality,
|
|
|
|
tags: tags,
|
|
|
|
popupContent: "<a href=\"/events/#{id}\">#{self}</a>"
|
2015-11-10 23:29:05 +01:00
|
|
|
}, geometry: { type: 'Point', coordinates: [longitude, latitude] } }
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
2014-08-23 16:59:42 +02:00
|
|
|
|
|
|
|
def full_address
|
2016-04-09 16:06:22 +02:00
|
|
|
[address, city, region.try(:name)].compact.join ', '
|
2014-08-23 16:59:42 +02:00
|
|
|
end
|
2014-09-23 17:50:35 +02:00
|
|
|
|
2014-10-04 18:12:03 +02:00
|
|
|
def hashtags
|
2015-08-20 11:10:35 +02:00
|
|
|
tags.split.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
|
2014-10-04 18:12:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"#{start_time.to_date} #{city}: #{title} #{hashtags.join(' ')}"
|
|
|
|
end
|
|
|
|
|
2015-10-23 16:21:42 +02:00
|
|
|
def to_tweet
|
|
|
|
url = Rails.application.routes.url_helpers.event_url(
|
|
|
|
self,
|
|
|
|
host: ActionMailer::Base.default_url_options[:host])
|
|
|
|
|
|
|
|
tweet = "#{self} #{url}"
|
2016-02-13 17:24:23 +01:00
|
|
|
if tweet.size >= 140
|
2015-10-23 16:21:42 +02:00
|
|
|
tweet = "#{tweet[0, tweet.rindex(/\s/, 140 - url.size)]} #{url}"
|
2014-10-04 19:13:19 +02:00
|
|
|
end
|
2015-10-23 16:21:42 +02:00
|
|
|
tweet
|
2014-10-04 19:13:19 +02:00
|
|
|
end
|
|
|
|
|
2014-09-23 17:50:35 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def end_after_start
|
|
|
|
return if end_time.blank? || start_time.blank?
|
|
|
|
|
|
|
|
errors.add :end_time, :before_start if end_time <= start_time
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|