class PressRelease < ActiveRecord::Base
  belongs_to :image_file
  has_many :blocks, :as => :blockable
  
  validates :title, :presence => true
  validates :slug, :presence => true #, :uniqueness => true
  
  
  
	scope :before, lambda { |date|
	        where("(press_releases.published_at <= ?)", date )
	    }
  
  
  after_create do
      
    LangSite.all.each do |lang|
      @block = Block.new(:block_name => "general", :lang_site => lang)
      @block.blockable = self
      @block.save
      
    end
  
  
  end
  
  
  before_validation do
    
        

    if !self.slug?
      self.slug = self.title

    end

    self.slug = self.slug.to_slug
    
  
  end    
  
  
  def generate_bloc
    
    if self.blocks.count == 0
      LangSite.all.each do |lang|
        @block = Block.new(:block_name => "general", :lang_site => lang)
        @block.blockable = self
        @block.save
      
      end
    end
    
    
    @block = self.blocks.find_by_lang_site_id(1)
    
    if @block.portlets.count == 0
    
      bloc_content = BlockContent.new
      bloc_content.nbr_columns = 1
      bloc_content.center = true
      bloc_content.save
    
      block = Block.new(:block_name => "")
      block.blockable = bloc_content
      block.save
    
      portlet = @block.portlets.new
      portlet.content = bloc_content
      portlet.save
    
   
    
    
      text_content = TextContent.create(:content => self.description_longue)
      portlet = block.portlets.new
      portlet.content = text_content
      portlet.save
    
  
    end
    
    

    
  end
    

    
  
end
