agenda-libre-ruby/app/models/event.rb

94 lines
2.9 KiB
Ruby
Raw Normal View History

# This is the central ADL class, where are managed all events
2013-12-28 23:45:13 +01:00
class Event < ActiveRecord::Base
extend SimpleCalendar
2014-07-01 15:50:39 +02:00
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
2013-12-28 23:45:13 +01: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
validate :end_after_start
2014-08-23 16:59:42 +02:00
geocoded_by :full_address, lookup: :nominatim
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
scope :moderated, -> { where moderated: true }
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-09-05 22:15:38 +02:00
scope :in, -> days { where 'end_time <= ?', (days || 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)
scope :month, (lambda do |start_date|
2014-09-05 22:15:38 +02:00
start_date ||= Date.today
where '? <= end_time and start_time <= ?',
2014-09-05 22:15:38 +02:00
start_date.beginning_of_month.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}%" }
scope :geo, -> { where 'latitude is not null and longitude is not null' }
2013-12-29 23:25:38 +01:00
before_validation on: :create do
self.submission_time = DateTime.now
self.decision_time = DateTime.now
2014-05-31 23:13:43 +02:00
self.submitter = contact if submitter.empty?
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 = DateTime.now
end
end
2014-09-03 02:14:21 +02:00
def as_json(_options = {})
{ type: 'Feature', properties: {
name: title,
popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
+ ">#{city}: #{title}</a>"
2014-09-03 02:14:21 +02:00
}, geometry: {
type: 'Point',
coordinates: [longitude, latitude]
} }
end
2014-08-23 16:59:42 +02:00
def full_address
[address, city, related_region.name].compact.join ', '
2014-08-23 16:59:42 +02:00
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
2013-12-28 23:45:13 +01:00
end