Skip to content
Extraits de code Groupes Projets
event.rb 2,75 ko
Newer Older
# This is the central ADL class, where are managed all events
class Event < ActiveRecord::Base
  extend SimpleCalendar

echarp's avatar
echarp a validé
  belongs_to :related_region, foreign_key: 'region', class_name: Region
  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
echarp's avatar
echarp a validé
  geocoded_by :full_address, lookup: :nominatim
echarp's avatar
echarp a validé
  # after_validation :geocode, if: -> (obj) { obj.address_changed? }
  after_validation :geocode
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 }
echarp's avatar
echarp a validé
  scope :past, -> { where 'start_time <= ?', DateTime.now }
  scope :future, -> { where '? <= end_time', DateTime.now }
echarp's avatar
echarp a validé
  scope :in, -> days { where 'end_time <= ?', (days || 30).to_i.days.from_now }
    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|
echarp's avatar
echarp a validé
    start_date ||= Date.today
    where '? <= end_time and start_time <= ?',
echarp's avatar
echarp a validé
          start_date.beginning_of_month.beginning_of_week,
  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' }
  before_validation on: :create do
    self.submission_time = DateTime.now
    self.decision_time = DateTime.now
    self.submitter = contact if submitter.empty?
  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

    if moderated? && moderated_was != moderated
      self.decision_time = DateTime.now
    end
  end

echarp's avatar
echarp a validé
  def as_json(_options = {})
    { type: 'Feature', properties: {
      name: title,
      popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
        + ">#{city}: #{title}</a>"
echarp's avatar
echarp a validé
    }, geometry: {
      type: 'Point',
      coordinates: [longitude, latitude]
    } }
echarp's avatar
echarp a validé

  def full_address
    [address, city, related_region.name].compact.join ', '
echarp's avatar
echarp a validé
  end