class Article < ActiveRecord::Base
  belongs_to :image_file
	validates :title, :presence => true
	validates :slug, :presence => true, :uniqueness => true
	has_one :block, :as => :blockable
	has_many :comments, :as => :commentable
	belongs_to :category
	has_many :enabled_comments,:conditions => {:enabled => true}, :source => :comments, :as => :commentable
	after_create :after_creation
	
	before_validation do
		self.slug = self.slug.to_slug
		self.tags_cache = self.tags_cache.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
		
	end
	
	

	def after_creation
	    @block = Block.new(:block_name => "Contenu")
		@block.blockable = self
		@block.save
		
		ContentType.all.each do |content_type|
			@block.content_types << content_type
			
		end 
	
	end
	
	scope :recents, where("enabled = ?",true ).order("published_at DESC")
	
	scope :between, lambda { |start, stop|
	        after(start).before(stop)
	    }
	
	scope :after, lambda { |date|
	        where("(published_at >= ?)", date )
	    }
	scope :before, lambda { |date|
	        where("(published_at <= ?)", date )
	    }

	
	
	after_initialize do
	    if self.published_at			
			self.published_at = self.published_at.strftime('%d/%m/%Y')
		end
	
	end


end
