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
|
2015-07-25 18:32:27 +02:00
|
|
|
has_paper_trail
|
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
|
|
|
|
2014-07-01 23:00:05 +02:00
|
|
|
scope :moderated, -> { where moderated: true }
|
2014-08-06 14:47:47 +02:00
|
|
|
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 }
|
2014-11-05 21:25:18 +01:00
|
|
|
scope :daylimit, -> d { where 'end_time <= ?', (d || 30).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 <= ?',
|
2014-08-09 18:59:11 +02:00
|
|
|
Date.new(year, 1, 1).beginning_of_week,
|
2014-10-13 23:55:18 +02:00
|
|
|
Date.new(year, 12, 31).end_of_week.end_of_day
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
|
|
|
scope :month, (lambda do |start_date|
|
2015-04-18 17:24:15 +02:00
|
|
|
start_date ||= Time.zone.today
|
2014-06-27 00:52:47 +02:00
|
|
|
where '? <= end_time and start_time <= ?',
|
2014-09-05 22:15:38 +02:00
|
|
|
start_date.beginning_of_month.beginning_of_week,
|
2014-10-13 23:55:18 +02:00
|
|
|
start_date.end_of_month.end_of_week.end_of_day
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
2014-10-14 10:33:22 +02:00
|
|
|
scope :region, -> region { where region: region unless region == 'all' }
|
|
|
|
scope :locality, -> locality { where locality: locality }
|
2014-06-01 15:06:04 +02:00
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
2014-08-06 14:47:47 +02:00
|
|
|
if moderated? && moderated_was != moderated
|
2015-04-18 17:24:15 +02:00
|
|
|
self.decision_time = Time.zone.now
|
2014-07-28 16:16:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-03 02:14:21 +02:00
|
|
|
def as_json(_options = {})
|
2015-04-18 17:24:15 +02:00
|
|
|
popup = "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\""
|
|
|
|
popup += " >#{start_time.to_date} #{city}: #{title}</a>"
|
2014-08-06 14:47:47 +02:00
|
|
|
{ type: 'Feature', properties: {
|
2014-10-26 16:51:16 +01:00
|
|
|
id: id,
|
2014-08-06 14:47:47 +02:00
|
|
|
name: title,
|
2014-10-26 16:51:16 +01:00
|
|
|
tags: tags,
|
2015-04-18 17:24:15 +02:00
|
|
|
popupContent: popup
|
2014-09-03 02:14:21 +02:00
|
|
|
}, geometry: {
|
2015-04-18 17:24:15 +02:00
|
|
|
type: 'Point', coordinates: [longitude, latitude]
|
2014-09-03 02:14:21 +02:00
|
|
|
} }
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
2014-08-23 16:59:42 +02:00
|
|
|
|
|
|
|
def full_address
|
2015-02-15 15:50:47 +01: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
|
|
|
|
|
2014-10-04 19:13:19 +02:00
|
|
|
def to_tweet(url)
|
|
|
|
tweet = to_s
|
|
|
|
if (tweet.size + url.size >= 140)
|
|
|
|
tweet = tweet[0, tweet[0, 140 - url.size].rindex(/\s/)]
|
|
|
|
end
|
|
|
|
"#{tweet} #{url}"
|
|
|
|
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
|