class PagePart
   
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name
  field :content, localize: true
  field :kind
  field :public_r_tag
  field :public_r_tag_object_id, :default => nil
  field :public_r_tag_option, :default => nil
  field :page_title, localize: true
  field :widget_path
  
  field :widget_style
  field :widget_field , :type => Array
  field :widget_options ,:type=>Hash
  field :widget_data_count

  belongs_to :page
  belongs_to :module_app

  before_save :delete_empty_widget_field
  after_save :update_parent

  # def module_app
  #   if self[:module_app]
  #     self[:module_app]
  #   elsif self[:kind] == 'module_widget'
  #     self.page.module_app
  #   else
  #     nil
  #   end
  # end

  protected
  
  def delete_empty_widget_field
  	if self.widget_field
        self.widget_field.reject! { |wf| (wf[0].blank? || wf[1].blank?) }
  	end
    # self.widget_field.delete("") if self.widget_field
  end

  def update_parent
    self.page.save
  end

end