class Event < ActiveRecord::Base date_format = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i hour_format = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/i validates :title, :presence => true validates :start_at_date, :presence => true, :format => { :with => date_format } validates :start_at_time, :presence => true, :format => { :with => hour_format }, :if => :with_time? validates :stop_at_date, :presence => true, :format => { :with => date_format }, :if => :stop_date validates :stop_at_time, :presence => true, :format => { :with => hour_format }, :if => [:with_time?,:stop_date] attr_accessor :start_at_date, :start_at_time, :stop_at_date, :stop_at_time, :event_id has_one :block, :as => :blockable scope :between, lambda { |start, stop| after(start).before(stop) } scope :after, lambda { |date| where("(start_at >= ? or stop_at >= ?)", date, date ) } scope :before, lambda { |date| where("(stop_at <= ?) or (start_at <= ?)", date, date ) } after_create do if self.event_id and Event.where(:id => self.event_id).exists? event = Event.find(self.event_id) @block = event.block.dup @block.blockable = self @block.save else @block = Block.new(:block_name => "general") @block.blockable = self @block.save end ContentType.all.each do |content_type| @block.content_types << content_type end end def create_block(event_id=nil) if event_id else end end before_validation do self.start_at = self.start_at_date.to_s+" "+self.start_at_time.to_s if !self.with_cible self.cible_type = nil self.cible_id = nil end end belongs_to :cible, :polymorphic => true accepts_nested_attributes_for :cible def url if self.cible cible.url else "" end end after_initialize do if self.start_at if self.entire_day self.start_at_date = self.start_at.strftime('%d/%m/%Y') else self.start_at_date = self.start_at.strftime('%d/%m/%Y') self.start_at_time = self.start_at.strftime('%H:%M') end end if self.stop_at if self.entire_day self.stop_at_date = self.stop_at.strftime('%d/%m/%Y') else self.stop_at_date = self.stop_at.strftime('%d/%m/%Y') self.stop_at_time = self.stop_at.strftime('%H:%M') end end end def human_date end end