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

131 lines
4.2 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
strip_attributes
has_paper_trail ignore: [:last_updated, :lock_version, :secret,
:submitter, :decision_time,
:latitude, :longitude]
belongs_to :region
# This is the scheduled first event
belongs_to :event
has_many :notes, dependent: :destroy
has_many :events, dependent: :destroy
2013-12-28 23:45:13 +01:00
validates :title, presence: true
validate :end_after_start
RULES = %w(daily weekly monthly).freeze
2016-09-11 19:13:10 +02:00
validates :rule, allow_nil: true, inclusion: RULES
validates :description, presence: true
validates :city, presence: true
validates :region, presence: true
validates :url, presence: true, format: %r{\Ahttps?:\/\/.*\..*\z}
2014-05-26 01:06:43 +02:00
validates :contact, email: true
validates :submitter, email: true
2014-12-14 21:48:52 +01:00
validates :tags, presence: true, format: /\A[\p{Alnum}\s-]*\z/
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
2015-11-10 23:29:05 +01:00
# Mechanism to store some reason which can be used when sending notifications
attr_accessor :reason
before_validation EventCallbacks
before_create EventCallbacks
after_create EventCallbacks
before_update EventCallbacks
after_update EventCallbacks
after_destroy EventCallbacks
2015-12-08 00:43:26 +01: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 }
scope :past, -> { where 'start_time <= ?', Time.zone.now }
scope :future, -> { where '? <= end_time', Time.zone.now }
scope :daylimit, ->(d) { where 'end_time <= ?', d.to_i.days.from_now }
scope :year, (lambda do |year|
where '? <= end_time and start_time <= ?',
2015-11-10 23:29:05 +01:00
Date.new(year.to_i, 1, 1).beginning_of_week,
Date.new(year.to_i, 12, 31).end_of_week.end_of_day
end)
scope :month, (lambda do |start_date|
where '? <= end_time and start_time <= ?',
2015-11-10 23:29:05 +01:00
start_date.to_date.beginning_of_month.beginning_of_week,
start_date.to_date.end_of_month.end_of_week.end_of_day
end)
2015-11-10 23:29:05 +01:00
scope :period, (lambda do |year, week|
start_date = DateTime.commercial(year.to_i, week.to_i)
where '? <= end_time and start_time <= ?',
start_date, start_date.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' }
2013-12-29 23:25:38 +01:00
before_validation on: :create do
self.submission_time = Time.zone.now
self.decision_time = Time.zone.now
2014-05-31 23:13:43 +02:00
# Populate submitter using contact info if absent
self.submitter = contact if submitter.blank?
end
before_validation on: :update do
self.latitude = nil if address_changed?
self.longitude = nil if address_changed?
end
2014-09-03 02:14:21 +02:00
def as_json(_options = {})
{ type: 'Feature', properties: {
id: id, name: title, start_time: start_time, end_time: end_time,
place_name: place_name, address: address, city: city, locality: locality,
tags: tags, popupContent: "<a href=\"/events/#{id}\">#{self}</a>"
2015-11-10 23:29:05 +01:00
}, geometry: { type: 'Point', coordinates: [longitude, latitude] } }
end
2014-08-23 16:59:42 +02:00
def full_address
# Region seems to disturb openstreetmap :(
# [address, city, region.try(:name)].compact.join ', '
[address, city].compact.join ', '
2014-08-23 16:59:42 +02:00
end
def schedule
IceCube::Schedule.new(start_time, end_time: end_time) do |s|
s.add_recurrence_rule IceCube::Rule.send(rule).count(repeat + 1)
end
end
def hashtags
2015-08-20 11:10:35 +02:00
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 = Rails.application.routes.url_helpers.event_url(
self,
host: ActionMailer::Base.default_url_options[:host]
)
tweet = "#{self} #{url}"
2016-02-13 17:24:23 +01:00
if tweet.size >= 140
tweet = "#{tweet[0, tweet.rindex(/\s/, 140 - url.size)]} #{url}"
end
tweet
end
private
def end_after_start
errors.add :end_time, :before_start if
!end_time.blank? && !start_time.blank? && end_time <= start_time
end
2013-12-28 23:45:13 +01:00
end