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

53 lines
1.6 KiB
Ruby

class Event < ActiveRecord::Base
extend SimpleCalendar
belongs_to :region, foreign_key: 'region'
has_many :notes
has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City
validates_presence_of :title, :description, :city, :region, :url, :contact
validates_format_of :url, with: /\Ahttps?:\/\/.*\z/
validates :contact, email: true
validates :submitter, email: true
scope :moderated, -> { where moderated: 1 }
scope :past, -> { where('end_time < now()').order(start_time: :desc) }
scope :future, -> { where('end_time >= now()').order(start_time: :asc) }
scope :future_30, -> {
where('start_time >= now() and end_time <= ?', Date.today + 30)
.order :start_time
}
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
}
scope :month, -> start_date {
where '? <= end_time and start_time <= ?',
start_date.beginning_of_week,
start_date.next_month.end_of_week
}
scope :region, -> region { where 'region = ? or locality', region }
scope :tag, -> tag { where 'tags like ?', "%#{tag}%" }
before_validation(on: :create) do
self.submission_time = Date.today
self.decision_time = Date.today
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
def same_day?
start_time.to_date == end_time.to_date
end
end