Skip to content
Extraits de code Groupes Projets
event.rb 3,96 ko
Newer Older
  • Learn to ignore specific revisions
  • # 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
    
    echarp's avatar
    echarp a validé
      # This is the eventual scheduled first event
      belongs_to :event, optional: true
    
      validate :end_after_start
    
      RULES = %w[daily weekly monthly monthly_day].freeze
    
    echarp's avatar
    echarp a validé
      validates :rule, inclusion: RULES, allow_nil: true
    
      validates :description, presence: true
      validates :city, presence: true
    
      validates :region, presence: true
    
      validates :url, allow_nil: true, format: %r{\Ahttps?:\/\/.*\..*\z}
    
      validates :contact, email: true, allow_nil: true
      validates :submitter, email: true, presence: true
    
      geocoded_by :full_address
    
      after_validation :geocode, if: (lambda do |obj|
    
        obj.address_changed? || obj.city_changed? || latitude.nil?
    
      # 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é
      default_scope { includes(:taggings, :region) }
    
    echarp's avatar
    echarp a validé
      scope :moderated, ->(*) { where moderated: true }
      scope :unmoderated, ->(*) { where moderated: false }
    
      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 <= ?',
    
              Date.new(year.to_i, 1, 1).beginning_of_week,
              Date.new(year.to_i, 12, 31).end_of_week.end_of_day
    
        where '? <= end_time and start_time <= ?',
    
              start_date.to_date.beginning_of_month.beginning_of_week,
              start_date.to_date.end_of_month.end_of_week.end_of_day
    
      scope :period, (lambda do |year, week|
    
        start_date = Date.commercial(
          year.to_i,
          (week || (Time.zone.today + 7.days).cweek).to_i
        )
    
        where '? <= end_time and start_time <= ?',
              start_date, start_date.end_of_week.end_of_day
      end)
    
      scope :region, (lambda do |region|
    
        temp = Region.find region
        where region: [temp, temp.regions].flatten
      end)
    
      scope :locality, ->(locality) { where locality: locality }
    
      scope :geo, -> { where 'latitude is not null and longitude is not null' }
    
        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 full_address
    
        # Only uses the region if it is a sub-region and not a country
    
        [address, city, region.try(:region).present? ? region : nil,
         region.try(:region)].compact.join ', '
    
    echarp's avatar
    echarp a validé
      end
    
        tag_list.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
    
      def description
        # Remove empty paragraphs, which are added by tinymce
        self[:description]&.remove '<p> </p>'
      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