class Layout < DesignFile
  include ParserLayout
  
  attr_reader :content

  field :body
  
  embeds_one :menu
  belongs_to :design
  embeds_many :layout_parts
  
  before_save :parse_layout
  after_save :save_pages

  def content
    self.file.read.force_encoding("UTF-8") rescue ''
  end

  def self.exist_one?
    Layout.count > 0
  end

  def parse_layout
    html = Nokogiri::HTML(self.file.read)
    self.body = html.at_css("body").inner_html
    parse_body(self)
  end

  def save_pages
    self.design.pages.each(&:generate_parts)
  end
  
end