class Event < ActiveRecord::Base
  belongs_to :image_file
  
  validates :title, :presence => true
  validates :slug, :presence => true, :uniqueness => true
  has_one :block, :as => :blockable
  
  after_create :after_creation
  
	before_validation do
		self.slug = self.title.to_slug
		
	end
	
	after_initialize do
	    if self.start_at			
			self.start_at = self.start_at.strftime('%d/%m/%Y')
		end
	
	end

	def after_creation
	  @block = Block.new(:block_name => "Contenu")
		@block.blockable = self
		@block.save
    
		
	end
  
  def alloweds_types
  self.block.allow_types :TitleContent, :TextContent, :ImageContent, :LinkContent, :GalleryContent, :HtmlContent
  
  end
  
	#where("enabled = ?",true ).
	scope :recents, -> {order("start_at DESC, created_at DESC")}
	
	scope :between, lambda { |start, stop|
	        after(start).before(stop)
	    }
	
	scope :after, lambda { |date|
	        where("(start_at >= ?)", date )
	    }
	scope :before, lambda { |date|
	        where("(start_at <= ?)", date )
	    }

	
	
  
  
  
end
