Sha256: 4e33d627015e0cf47b6bad04f395d52be5fd232f94d7302cd4ef65fba23d8965

Contents?: true

Size: 877 Bytes

Versions: 1

Compression:

Stored size: 877 Bytes

Contents

class Page < Content
  belongs_to :parent, :class_name => 'Page'
  has_many :children, :class_name => 'Page', :foreign_key => 'parent_id'
  
  acts_as_taggable_on :tags
  
  before_validation :ensure_hierarchy
  has_permalink :title, :scope => :parent_id, :unless => :homepage?
  
  validates_presence_of :title, :body
  
  # Tree structure
  scope :roots, where(:parent_id => nil)
  
  def self.root
    roots.first
  end

  def root?
    self == Page.root || Page.count == 0 && new_record?
  end
  alias :homepage? :root?

  def ancestors
    node, nodes = self, []
    nodes << node = node.parent while node.parent
    nodes
  end

  def url
    links = (ancestors.map(&:permalink) << permalink).delete_if(&:blank?)
    
    "/#{links.join('/')}"
  end

  private
    def ensure_hierarchy
      self.parent = site.homepage if !parent_id && site && site.homepage
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
content_engine-0.1.0 app/models/page.rb