# -*- encoding : utf-8 -*-
class Block < ActiveRecord::Base

  ContentTypes = {
    TitleContent: "Titre",
    TextContent: "Texte",
    ImageContent: "Image",
    LinkContent:"Lien",
    BreakContent: "Séparation",
    HtmlContent: "Code HTML",
    DownloadContent: "Téléchargement",
    GalleryContent: "Galerie",
    DynamicContent: "Contenu dynamique",
    TableContent: "Tableau",
    BlockContent: "Bloc",
    MapContent: "Plan",
   }
  

  belongs_to :blockable, :polymorphic => true
  
  #has_many :portlets, :dependent => :destroy, -> {:order => :position}
  
  has_many :portlets,-> { order :position }, dependent: :destroy
  
  accepts_nested_attributes_for :portlets


  def allow_types(*type)
    if type == [:all]
      ContentTypes
    else
      allow_types = {}
      type.each do |content|
    
        allow_types[content] = ContentTypes[content]
      end
      allow_types
    end
  end

def alloweds_types
  allow_types :all #:TitleContent, :ImageContent
  
end

  def dup
    @block = Block.new(self.attributes)
    @block.id = nil
    @block.save
    
    self.portlets.each do |portlet|
      new_p = portlet.dup
      new_p.save
      @block.portlets << new_p
    end
    @block
  end

end