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
|
|
|
|
validates :description, presence: true
|
|
|
|
validates :city, presence: true
|
|
|
|
validates :related_region, presence: true
|
|
|
|
validates :url, presence: true, format: %r{\Ahttps?:\/\/.*\z}
|
|
|
|
validates :contact, presence: true
|
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-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 }
|
|
|
|
scope :last_year, -> { where 'start_time >= ?', 360.days.ago }
|
|
|
|
scope :past, (lambda do
|
|
|
|
where('end_time < ?', DateTime.now).order start_time: :desc
|
|
|
|
end)
|
|
|
|
scope :future, (lambda do
|
|
|
|
where('end_time >= ?', DateTime.now).order start_time: :asc
|
|
|
|
end)
|
|
|
|
scope :future_30, (lambda do
|
2014-08-01 00:40:26 +02:00
|
|
|
where('start_time >= ? and end_time <= ?', DateTime.now, 30.days.from_now)
|
2014-06-27 00:52:47 +02:00
|
|
|
.order :start_time
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
|
|
|
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,
|
|
|
|
Date.new(year, 12, 31).end_of_week
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
|
|
|
scope :month, (lambda do |start_date|
|
2014-08-09 18:59:11 +02:00
|
|
|
start_date ||= Date.today.beginning_of_month
|
2014-06-27 00:52:47 +02:00
|
|
|
where '? <= end_time and start_time <= ?',
|
2014-08-09 18:59:11 +02:00
|
|
|
start_date.beginning_of_week,
|
|
|
|
start_date.end_of_month.end_of_week
|
2014-08-06 14:47:47 +02:00
|
|
|
end)
|
2014-07-01 23:00:05 +02:00
|
|
|
scope :region, -> region { where 'region = ? or locality', region }
|
2014-06-01 15:06:04 +02:00
|
|
|
scope :tag, -> tag { where 'tags like ?', "%#{tag}%" }
|
2013-12-29 23:25:38 +01:00
|
|
|
|
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-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
|
|
|
|
|
2013-12-29 23:25:38 +01:00
|
|
|
def same_day?
|
|
|
|
start_time.to_date == end_time.to_date
|
|
|
|
end
|
2014-08-06 14:47:47 +02:00
|
|
|
|
|
|
|
def to_json
|
|
|
|
{ type: 'Feature', properties: {
|
|
|
|
name: title,
|
|
|
|
popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
|
|
|
|
+ ">#{city}: #{title}</a>"
|
|
|
|
},
|
|
|
|
geometry: {
|
|
|
|
type: 'Point',
|
|
|
|
coordinates: [related_city.longitude, related_city.latitude]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|