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

53 lines
1.7 KiB
Ruby
Raw Normal View History

2013-12-28 23:45:13 +01:00
class Event < ActiveRecord::Base
extend SimpleCalendar
2014-07-01 15:50:39 +02:00
belongs_to :related_region, foreign_key: 'region', class_name: Region
has_many :notes, dependent: :destroy
has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City
2013-12-28 23:45:13 +01:00
2014-07-01 15:50:39 +02:00
validates_presence_of :title, :description, :city, :related_region, :url, :contact
validates_format_of :url, with: /\Ahttps?:\/\/.*\z/
2014-05-26 01:06:43 +02:00
validates :contact, email: true
validates :submitter, email: true
2014-05-31 23:13:43 +02:00
scope :moderated, -> { where moderated: true }
2014-07-18 23:20:30 +02:00
scope :past, -> { where('end_time < ?', Date.today).order(start_time: :desc) }
scope :future, -> { where('end_time >= ?', Date.today).order(start_time: :asc) }
2014-01-05 22:10:11 +01:00
scope :future_30, -> {
2014-07-18 23:20:30 +02:00
where('start_time >= ? and end_time <= ?', Date.today, Date.today + 30)
.order :start_time
2014-01-02 00:21:49 +01:00
}
2013-12-28 23:45:13 +01:00
scope :year, -> year {
where '? <= end_time and start_time <= ?',
Date.new(year, 1, 1).beginning_of_week, Date.new(year, 12, 31).end_of_week
2013-12-28 23:45:13 +01:00
}
scope :month, -> start_date {
where '? <= end_time and start_time <= ?',
start_date.beginning_of_week,
start_date.next_month.end_of_week
2013-12-28 23:45:13 +01:00
}
scope :region, -> region { where 'region = ? or locality', region }
scope :tag, -> tag { where 'tags like ?', "%#{tag}%" }
2013-12-29 23:25:38 +01:00
2014-05-31 23:13:43 +02:00
before_validation on: :create do
self.submission_time = Date.today
self.decision_time = Date.today
2014-05-31 23:13:43 +02:00
if self.submitter.empty?
self.submitter = self.contact
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
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