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-07-01 15:50:39 +02:00
|
|
|
belongs_to :related_region, foreign_key: 'region', class_name: Region
|
2014-06-29 13:37:08 +02:00
|
|
|
has_many :notes, dependent: :destroy
|
2014-08-06 14:47:47 +02:00
|
|
|
has_one :related_city, foreign_key: :name, primary_key: :city,
|
|
|
|
class_name: City
|
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
|
|
|
|
validates :related_region, presence: true
|
2014-09-26 01:26:33 +02:00
|
|
|
validates :url, presence: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
2014-08-06 14:47:47 +02:00
|
|
|
validates :contact, presence: true
|
2014-05-26 01:06:43 +02:00
|
|
|
validates :contact, email: true
|
|
|
|
validates :submitter, email: true
|
2014-09-26 01:26:33 +02:00
|
|
|
validates :tags, presence: false, 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 }
|
2014-09-02 01:02:51 +02:00
|
|
|
scope :past, -> { where 'start_time <= ?', DateTime.now }
|
|
|
|
scope :future, -> { where '? <= end_time', DateTime.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|
|
2014-09-05 22:15:38 +02:00
|
|
|
start_date ||= Date.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
|
2014-07-28 16:16:27 +02:00
|
|
|
self.submission_time = DateTime.now
|
|
|
|
self.decision_time = DateTime.now
|
2014-05-31 23:13:43 +02:00
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
self.submitter = contact if submitter.empty?
|
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
|
2014-07-28 16:16:27 +02:00
|
|
|
self.decision_time = DateTime.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-03 02:14:21 +02:00
|
|
|
def as_json(_options = {})
|
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,
|
2014-08-06 14:47:47 +02:00
|
|
|
popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
|
2014-10-27 22:54:55 +01:00
|
|
|
+ ">#{start_time.to_date} #{city}: #{title}</a>"
|
2014-09-03 02:14:21 +02: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
|
2014-10-05 12:57:46 +02:00
|
|
|
[address, city, related_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
|
2014-10-09 12:36:47 +02:00
|
|
|
tags.split.map { |tag| "##{tag.gsub(/-/, '_').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
|