# -*- encoding : utf-8 -*-
class Portlet < ApplicationRecord
  belongs_to :block
  belongs_to :content, :polymorphic => true
  accepts_nested_attributes_for :content

  attr_accessor :skip_before_update


  def blockable_parent
    if self.block
      blockable = self.block.blockable
    
      while blockable.class.to_s == "BlockContent"
        blockable = blockable.portlet.block.blockable
      end
    
      blockable
      
    end
  end




  def dup
    @portlet = Portlet.new(self.attributes)
    @portlet.id = nil
    @portlet.content = self.content.dup
    
    
    @portlet.save
    @portlet
  end
  



  before_create do
    
  end

  before_update do
  
  end

  before_destroy do
    

  end
  
  after_save do
    self.blockable_parent.update_attribute(:updated_at,Time.now) if self.blockable_parent
    #self.blockable_parent.save
    
  end
  
  after_destroy do
    self.blockable_parent.update_attribute(:updated_at,Time.now) if self.blockable_parent
    #self.blockable_parent.save
    
  end


end