82 lines
2.6 KiB
Ruby
82 lines
2.6 KiB
Ruby
# This is the central ADL class, where are managed all events
|
|
class Event < ActiveRecord::Base
|
|
extend SimpleCalendar
|
|
|
|
belongs_to :related_region, foreign_key: 'region', class_name: Region
|
|
has_many :notes, dependent: :destroy
|
|
has_one :related_city, foreign_key: :name, primary_key: :city,
|
|
class_name: City
|
|
|
|
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
|
|
validates :contact, email: true
|
|
validates :submitter, email: true
|
|
|
|
scope :moderated, -> { where moderated: true }
|
|
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
|
|
where('start_time >= ? and end_time <= ?', DateTime.now, 30.days.from_now)
|
|
.order :start_time
|
|
end)
|
|
scope :year, (lambda do |year|
|
|
where '? <= end_time and start_time <= ?',
|
|
Date.new(year, 1, 1).beginning_of_week,
|
|
Date.new(year, 12, 31).end_of_week
|
|
end)
|
|
scope :month, (lambda do |start_date|
|
|
start_date ||= Date.today.beginning_of_month
|
|
where '? <= end_time and start_time <= ?',
|
|
start_date.beginning_of_week,
|
|
start_date.end_of_month.end_of_week
|
|
end)
|
|
scope :region, -> region { where 'region = ? or locality', region }
|
|
scope :tag, -> tag { where 'tags like ?', "%#{tag}%" }
|
|
|
|
before_validation on: :create do
|
|
self.submission_time = DateTime.now
|
|
self.decision_time = DateTime.now
|
|
|
|
self.submitter = contact if submitter.empty?
|
|
end
|
|
|
|
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
|
|
|
|
before_update do
|
|
if moderated? && moderated_was != moderated
|
|
self.decision_time = DateTime.now
|
|
end
|
|
end
|
|
|
|
def same_day?
|
|
start_time.to_date == end_time.to_date
|
|
end
|
|
|
|
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
|
|
end
|