# -*- encoding : utf-8 -*-
class EventContent < ActiveRecord::Base
	belongs_to :image_file

	has_one :portlet, :as => :content, :dependent => :destroy
	TYPES = [["Rencontre avec l'artiste","1"], ["Autre","2"]]
	
	belongs_to :cible, :polymorphic => true
	accepts_nested_attributes_for :cible
	
	
  
	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 }
  
  attr_accessor :start_at_date, :start_at_time
  
  
  scope :after, lambda { |date|
  	        where("(start_at >= ?)", date )
  	    }
  	scope :before, lambda { |date|
  	        where("start_at <= ?", date )
  	    }
  
  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

	
	
	def url
		
		if self.cible
			cible.url
		else
			""	
		end
		
	end
  
  after_initialize do
  	    if self.start_at

  				self.start_at_date = self.start_at.strftime('%d/%m/%Y')
  				self.start_at_time = self.start_at.strftime('%H:%M')
 
  		end
		
  		
  	  end

end
