class Event < ActiveRecord::Base belongs_to :region, foreign_key: 'region' validates_presence_of :region default_scope { where moderated: 1 } scope :future, -> { where('start_time >= now() and end_time <= ?', Date.today + 30) .order(:start_time) } scope :year, -> year { where "end_time >= ? and start_time < ?", "#{year}-1-1", "#{year.to_i+1}-1-1" } scope :month, -> year, month { where 'end_time >= ? and start_time <= ?', "#{year}-#{month.to_i-1}-23", "#{month == '12' ? year.to_i+1 : year}-#{month == '12' ? 1 : month.to_i+1}-7" } scope :region, -> region { where region: region } scope :tag, -> tag { where "tags like ?", "%#{tag}%" } before_validation(on: :create) do self.submission_time = Date.today self.decision_time = Date.today end def same_day? start_time.to_date == end_time.to_date end end