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

36 lines
1.0 KiB
Ruby
Raw Normal View History

2013-12-28 23:45:13 +01:00
class Event < ActiveRecord::Base
belongs_to :region, foreign_key: 'region'
2014-01-06 11:22:39 +01:00
has_many :notes
2013-12-28 23:45:13 +01:00
validates_presence_of :region
default_scope { where moderated: 1 }
2014-01-05 22:10:11 +01:00
scope :past, -> { where('end_time < now()').order(start_time: :desc) }
scope :future, -> { where('end_time >= now()').order(start_time: :asc) }
scope :future_30, -> {
2014-01-02 00:21:49 +01:00
where('start_time >= now() and end_time <= ?', Date.today + 30)
.order(:start_time)
}
2013-12-28 23:45:13 +01:00
scope :year, -> year {
where "end_time >= ? and start_time < ?",
2013-12-29 17:45:53 +01:00
"#{year}-1-1", "#{year.to_i+1}-1-1"
2013-12-28 23:45:13 +01:00
}
scope :month, -> year, month {
2014-01-02 00:21:49 +01:00
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"
2013-12-28 23:45:13 +01:00
}
scope :region, -> region { where region: region }
2013-12-29 20:42:00 +01:00
scope :tag, -> tag { where "tags like ?", "%#{tag}%" }
2013-12-29 23:25:38 +01:00
before_validation(on: :create) do
self.submission_time = Date.today
self.decision_time = Date.today
end
2013-12-29 23:25:38 +01:00
def same_day?
start_time.to_date == end_time.to_date
end
2013-12-28 23:45:13 +01:00
end