2016-09-17 17:19:41 +02:00
|
|
|
|
require 'schedule'
|
|
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
|
# This is the central ADL class, where are managed all events
|
2017-05-27 09:34:24 +02:00
|
|
|
|
class Event < ApplicationRecord
|
2014-06-27 00:52:47 +02:00
|
|
|
|
extend SimpleCalendar
|
2016-09-17 17:19:41 +02:00
|
|
|
|
include Schedule
|
2016-12-17 16:59:11 +01:00
|
|
|
|
acts_as_taggable
|
2014-12-14 13:29:52 +01:00
|
|
|
|
strip_attributes
|
2017-04-22 20:01:47 +02:00
|
|
|
|
has_paper_trail ignore: %i[last_updated lock_version secret submitter
|
|
|
|
|
decision_time latitude longitude]
|
2014-06-27 00:52:47 +02:00
|
|
|
|
|
2015-02-15 15:50:47 +01:00
|
|
|
|
belongs_to :region
|
2017-10-21 22:12:19 +02:00
|
|
|
|
# This is the eventual scheduled first event
|
|
|
|
|
belongs_to :event, optional: true
|
2014-06-29 13:37:08 +02:00
|
|
|
|
has_many :notes, dependent: :destroy
|
2016-09-11 17:40:49 +02:00
|
|
|
|
has_many :events, 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
|
2017-04-22 20:01:47 +02:00
|
|
|
|
RULES = %w[daily weekly monthly monthly_day].freeze
|
2018-01-01 17:52:33 +01:00
|
|
|
|
validates :rule, inclusion: RULES, allow_nil: true
|
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
|
2017-04-22 11:35:37 +02:00
|
|
|
|
validates :url, allow_nil: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2018-04-08 23:37:12 +02:00
|
|
|
|
validates :contact, email: true, allow_nil: true
|
|
|
|
|
validates :submitter, email: true, presence: true
|
2014-01-01 21:26:35 +01:00
|
|
|
|
|
2014-10-16 23:31:02 +02:00
|
|
|
|
geocoded_by :full_address
|
2016-12-17 17:44:35 +01:00
|
|
|
|
after_validation :geocode, if: (lambda do |obj|
|
2019-11-05 19:46:18 +01:00
|
|
|
|
obj.address_changed? || obj.city_changed? || latitude.nil?
|
2016-12-17 17:44:35 +01:00
|
|
|
|
end)
|
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
|
2016-05-21 12:02:19 +02:00
|
|
|
|
attr_accessor :reason
|
2015-10-22 21:14:36 +02:00
|
|
|
|
|
2016-09-11 17:40:49 +02:00
|
|
|
|
before_create EventCallbacks
|
2015-10-22 21:14:36 +02:00
|
|
|
|
after_create EventCallbacks
|
2016-09-11 17:40:49 +02:00
|
|
|
|
|
|
|
|
|
before_update EventCallbacks
|
2015-10-22 21:14:36 +02:00
|
|
|
|
after_update EventCallbacks
|
2016-09-11 17:40:49 +02:00
|
|
|
|
|
2015-10-22 21:14:36 +02:00
|
|
|
|
after_destroy EventCallbacks
|
|
|
|
|
|
2019-12-21 21:00:16 +01:00
|
|
|
|
default_scope { includes(:taggings, :region) }
|
2015-12-08 00:43:26 +01:00
|
|
|
|
scope :moderated, ->(*) { where moderated: true }
|
|
|
|
|
scope :unmoderated, ->(*) { where moderated: false }
|
2015-04-18 17:24:15 +02:00
|
|
|
|
scope :past, -> { where 'start_time <= ?', Time.zone.now }
|
|
|
|
|
scope :future, -> { where '? <= end_time', Time.zone.now }
|
2017-05-29 22:18:04 +02:00
|
|
|
|
scope :daylimit, ->(nb) { where 'end_time <= ?', nb.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|
|
2019-06-09 17:52:47 +02:00
|
|
|
|
start_date = Date.commercial(
|
|
|
|
|
year.to_i,
|
|
|
|
|
(week || (Time.zone.today + 7.days).cweek).to_i
|
|
|
|
|
)
|
2015-11-10 23:29:05 +01:00
|
|
|
|
where '? <= end_time and start_time <= ?',
|
|
|
|
|
start_date, start_date.end_of_week.end_of_day
|
|
|
|
|
end)
|
2017-09-17 18:25:34 +02:00
|
|
|
|
scope :region, (lambda do |region|
|
2019-02-12 14:19:52 +01:00
|
|
|
|
return if region == 'all'
|
|
|
|
|
|
2017-09-17 18:25:34 +02:00
|
|
|
|
temp = Region.find region
|
|
|
|
|
where region: [temp, temp.regions].flatten
|
|
|
|
|
end)
|
2015-11-10 23:40:56 +01:00
|
|
|
|
scope :locality, ->(locality) { where locality: locality }
|
2016-12-17 16:59:11 +01:00
|
|
|
|
scope :tag, ->(tag) { tagged_with 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-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
|
2017-05-29 22:18:04 +02:00
|
|
|
|
self.submitter ||= contact
|
2014-01-01 21:26:35 +01:00
|
|
|
|
end
|
|
|
|
|
|
2014-08-25 11:12:22 +02:00
|
|
|
|
before_validation on: :update do
|
2016-05-21 12:02:19 +02:00
|
|
|
|
self.latitude = nil if address_changed?
|
|
|
|
|
self.longitude = nil if address_changed?
|
2014-08-25 11:12:22 +02:00
|
|
|
|
end
|
|
|
|
|
|
2014-08-23 16:59:42 +02:00
|
|
|
|
def full_address
|
2018-05-06 21:20:48 +02:00
|
|
|
|
# Only uses the region if it is a sub-region and not a country
|
2018-05-06 21:25:35 +02:00
|
|
|
|
[address, city, region.try(:region).present? ? region : nil,
|
|
|
|
|
region.try(:region)].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
|
2016-12-17 16:59:11 +01:00
|
|
|
|
tag_list.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
|
2014-10-04 18:12:03 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-03-03 16:42:33 +01:00
|
|
|
|
def description
|
|
|
|
|
# Remove empty paragraphs, which are added by tinymce
|
|
|
|
|
self[:description]&.remove '<p> </p>'
|
|
|
|
|
end
|
|
|
|
|
|
2014-10-04 18:12:03 +02:00
|
|
|
|
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,
|
2016-05-21 12:02:19 +02:00
|
|
|
|
host: ActionMailer::Base.default_url_options[:host]
|
|
|
|
|
)
|
2015-10-23 16:21:42 +02:00
|
|
|
|
|
|
|
|
|
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
|
2016-05-21 12:02:19 +02:00
|
|
|
|
errors.add :end_time, :before_start if
|
2017-04-22 20:01:47 +02:00
|
|
|
|
end_time.present? && start_time.present? && end_time <= start_time
|
2014-09-23 17:50:35 +02:00
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
|
end
|