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
|
2013-12-28 23:45:13 +01:00
|
|
|
class Event < ActiveRecord::Base
|
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
|
2016-09-17 17:19:41 +02:00
|
|
|
has_paper_trail ignore: [: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
|
2016-09-11 17:40:49 +02:00
|
|
|
# This is the scheduled first event
|
|
|
|
belongs_to :event
|
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
|
2016-09-17 17:19:41 +02:00
|
|
|
RULES = %w(daily weekly monthly monthly_day).freeze
|
2016-09-11 19:13:10 +02:00
|
|
|
validates :rule, allow_nil: true, inclusion: RULES
|
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-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|
|
|
|
|
obj.address_changed? || obj.city_changed?
|
|
|
|
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
|
|
|
|
|
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 }
|
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
|
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
|
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-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,
|
2016-12-17 16:59:11 +01:00
|
|
|
tags: tag_list, 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-20 16:25:53 +02:00
|
|
|
# Region seems to disturb openstreetmap :(
|
|
|
|
# [address, city, region.try(:name)].compact.join ', '
|
|
|
|
[address, city].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
|
|
|
|
|
|
|
|
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
|
|
|
|
!end_time.blank? && !start_time.blank? && end_time <= start_time
|
2014-09-23 17:50:35 +02:00
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|