Skip to content
Extraits de code Groupes Projets
event.rb 4 ko
Newer Older
# This is the central ADL class, where are managed all events
class Event < ApplicationRecord
  extend SimpleCalendar
  strip_attributes
  has_paper_trail ignore: %i[last_updated lock_version secret submitter
                             decision_time latitude longitude]
  belongs_to :region
  # This is the scheduled first event
  belongs_to :event
  validate :end_after_start
  RULES = %w[daily weekly monthly monthly_day].freeze
  validates :rule, allow_nil: true, inclusion: RULES
  validates :description, presence: true
  validates :city, presence: true
  validates :region, presence: true
echarp's avatar
echarp a validé
  validates :url, allow_nil: true, format: %r{\Ahttps?:\/\/.*\..*\z}
  validates :contact, email: true
echarp's avatar
echarp a validé
  validates :submitter, email: true, presence: false
  geocoded_by :full_address
  after_validation :geocode, if: (lambda do |obj|
    obj.address_changed? || obj.city_changed?
  end)
echarp's avatar
echarp a validé

echarp's avatar
echarp a validé
  # Mechanism to store some reason which can be used when sending notifications
  attr_accessor :reason
  after_create EventCallbacks
  after_update EventCallbacks
  after_destroy EventCallbacks

echarp's avatar
echarp a validé
  scope :moderated, ->(*) { where moderated: true }
  scope :unmoderated, ->(*) { where moderated: false }
echarp's avatar
echarp a validé
  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 }
echarp's avatar
echarp a validé
  scope :daylimit, ->(nb) { where 'end_time <= ?', nb.to_i.days.from_now }
    where '? <= end_time and start_time <= ?',
echarp's avatar
echarp a validé
          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 <= ?',
echarp's avatar
echarp a validé
          start_date.to_date.beginning_of_month.beginning_of_week,
          start_date.to_date.end_of_month.end_of_week.end_of_day
echarp's avatar
echarp a validé
  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 :geo, -> { where 'latitude is not null and longitude is not null' }
  before_validation on: :create do
    self.submission_time = Time.zone.now
    self.decision_time = Time.zone.now
    # Populate submitter using contact info if absent
echarp's avatar
echarp a validé
    self.submitter ||= contact
  before_validation on: :update do
    self.latitude = nil if address_changed?
    self.longitude = nil if address_changed?
echarp's avatar
echarp a validé
  def as_json(_options = {})
      id: id, name: title, start_time: start_time, end_time: end_time,
      place_name: place_name, address: address, city: city, locality: locality,
      tags: tag_list, popupContent: "<a href=\"/events/#{id}\">#{self}</a>"
echarp's avatar
echarp a validé
    }, geometry: { type: 'Point', coordinates: [longitude, latitude] } }
echarp's avatar
echarp a validé

  def full_address
    # Region seems to disturb openstreetmap :(
    # address, city].compact.join ', '
    [address, city, region.try(:name)].compact.join ', '
echarp's avatar
echarp a validé
  end
    tag_list.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]
    )
echarp's avatar
echarp a validé
    if tweet.size >= 140
      tweet = "#{tweet[0, tweet.rindex(/\s/, 140 - url.size)]} #{url}"
    errors.add :end_time, :before_start if
      end_time.present? && start_time.present? && end_time <= start_time