121 lines
3.5 KiB
Ruby
121 lines
3.5 KiB
Ruby
# encoding: utf-8
|
|
|
|
# This is the central ADL class, where are managed all events
|
|
class Event < ActiveRecord::Base
|
|
extend SimpleCalendar
|
|
strip_attributes
|
|
has_paper_trail
|
|
|
|
belongs_to :region
|
|
has_many :notes, dependent: :destroy
|
|
|
|
validates :title, presence: true
|
|
validate :end_after_start
|
|
validates :description, presence: true
|
|
validates :city, presence: true
|
|
validates :region, presence: true
|
|
validates :url, presence: true, format: %r{\Ahttps?:\/\/.*\..*\z}
|
|
validates :contact, email: true
|
|
validates :submitter, email: true
|
|
validates :tags, presence: true, format: /\A[\p{Alnum}\s-]*\z/
|
|
|
|
geocoded_by :full_address
|
|
# after_validation :geocode, if: -> (obj) { obj.address_changed? }
|
|
after_validation :geocode
|
|
|
|
scope :moderated, -> { where moderated: true }
|
|
scope :unmoderated, -> { where moderated: false }
|
|
scope :last_year, -> { where '? <= end_time', 1.year.ago }
|
|
scope :past, -> { where 'start_time <= ?', Time.zone.now }
|
|
scope :future, -> { where '? <= end_time', Time.zone.now }
|
|
scope :daylimit, -> d { where 'end_time <= ?', (d || 30).to_i.days.from_now }
|
|
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_of_day
|
|
end)
|
|
scope :month, (lambda do |start_date|
|
|
start_date ||= Time.zone.today
|
|
where '? <= end_time and start_time <= ?',
|
|
start_date.beginning_of_month.beginning_of_week,
|
|
start_date.end_of_month.end_of_week.end_of_day
|
|
end)
|
|
scope :region, -> region { where region: region unless region == 'all' }
|
|
scope :locality, -> locality { where locality: locality }
|
|
scope :tag, -> tag { where 'tags like ?', "%#{tag}%" }
|
|
scope :geo, -> { where 'latitude is not null and longitude is not null' }
|
|
|
|
before_validation do
|
|
# Tags are always downcased
|
|
self.tags = tags.mb_chars.downcase if tags
|
|
end
|
|
|
|
before_validation on: :create do
|
|
self.submission_time = Time.zone.now
|
|
self.decision_time = Time.zone.now
|
|
|
|
# Populate submitter using contact info if absent
|
|
self.submitter ||= contact
|
|
end
|
|
|
|
before_validation on: :update do
|
|
if address_changed?
|
|
self.latitude = nil
|
|
self.longitude = nil
|
|
end
|
|
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 = Time.zone.now
|
|
end
|
|
end
|
|
|
|
def as_json(_options = {})
|
|
popup = "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\""
|
|
popup += " >#{start_time.to_date} #{city}: #{title}</a>"
|
|
{ type: 'Feature', properties: {
|
|
id: id,
|
|
name: title,
|
|
tags: tags,
|
|
popupContent: popup
|
|
}, geometry: {
|
|
type: 'Point', coordinates: [longitude, latitude]
|
|
} }
|
|
end
|
|
|
|
def full_address
|
|
[address, city, region.try(:name)].compact.join ', '
|
|
end
|
|
|
|
def hashtags
|
|
tags.split.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
|
|
end
|
|
|
|
def to_s
|
|
"#{start_time.to_date} #{city}: #{title} #{hashtags.join(' ')}"
|
|
end
|
|
|
|
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
|
|
|
|
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
|
|
end
|