Sha256: 306e45863678800c42b550a186f3827f8354e76af2855f42521065bba4af73d0

Contents?: true

Size: 863 Bytes

Versions: 1

Compression:

Stored size: 863 Bytes

Contents

class Page < ActiveRecord::Base
  acts_as_tree
  has_paper_trail
  attr_accessible :content, :parent, :parent_id, :permalink
  serialize       :content, Hash

  class << self
    def find_by_path(path)
      return unless page = root

      page.walk(path) do |last, part|
        last.children.find_by_permalink(part) if last
      end
    end

    def update_content(path, content = {})
      page = root || Page.create(permalink: '')

      page = page.walk(path) do |last, part|
        last.children.find_or_create_by_permalink(part)
      end

      page.update_attributes(content: content)

      page
    end
  end

  def path
    if parent
      Pathname.new(parent.path) + permalink
    else
      Pathname.new("/")
    end.to_s
  end

  def walk(path, &block)
    parts = Pathname.new(path.to_s).each_filename

    parts.reduce(self, &block)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mercury_engine-0.0.1 app/models/page.rb