class Humpyard::Page

Humpyard::Page is the model for your pages. It holds the Humpyard::Elements containing the content of your page and some meta data for the page itself.

Public Class Methods

root_page(options = {}) click to toggle source
# File app/models/humpyard/page.rb, line 29
def self.root_page(options = {})
  if options[:force_reload]
    @root_page = Humpyard::Page.select(:id).with_translated_attribute(:title_for_url, :index).first
  else
    @root_page ||= Humpyard::Page.select(:id).with_translated_attribute(:title_for_url, :index).first
  end
end

Public Instance Methods

ancestor_pages(options={}) click to toggle source

Get all ancestor pages

# File app/models/humpyard/page.rb, line 129
def ancestor_pages options={}
  if parent_page
    parent_page.ancestor_pages(options) + [parent_page(options)]
  else
    []
  end
end
child_pages(options={}) click to toggle source

Find the child pages

# File app/models/humpyard/page.rb, line 101
def child_pages options={}
  if content_data.is_humpyard_dynamic_page?
    content_data.child_pages
  else
    if options[:single_root] and is_root_page? 
      Page.where(["parent_id = ? or parent_id IS NULL and NOT id = ?", id, id])
    else
      children
    end
  end
end
human_url(options={}) click to toggle source

Return the human readable URL for the page.

Posible options values are

:locale

A locale given in the Humpyard::Config.locales. If no :locale is given the option will be ::I18n.locale by default

# File app/models/humpyard/page.rb, line 72
def human_url(options={})
  options[:locale] ||= ::I18n.locale
  options[:format] ||= :html
  
  unless Humpyard::config.locales.include? options[:locale].to_sym
    options[:locale] = Humpyard::config.locales.first
  end
  
  if options[:path_format]
    format = "/"
  else
    format = ".#{options[:format].to_s}" 
  end
  
  if self.title_for_url == 'index' or self.is_root_page?
    "/#{Humpyard::config.parsed_www_prefix(options).gsub(/[^\/]*$/, '')}"
  else
    "/#{Humpyard::config.parsed_www_prefix(options)}#{((self.ancestors.reverse + [self]).collect{|p| p.query_title_for_url(options[:locale])} - ['index']) * '/'}#{format}".gsub(/^index\//,'')
  end
end
is_ancestor_page_of?(page) click to toggle source

Is the given page an ancestor of the actual page

# File app/models/humpyard/page.rb, line 138
def is_ancestor_page_of? page
  page.ancestor_pages.include? self
end
is_root_page?() click to toggle source
# File app/models/humpyard/page.rb, line 37
def is_root_page?
  self.id and Humpyard::Page.root_page and self.id == Humpyard::Page.root_page.id
end
last_modified(options = {}) click to toggle source

Return the logical modification time for the page, suitable for http caching, generational cache keys, etc.

# File app/models/humpyard/page.rb, line 143
def last_modified options = {}
  changed_at = [Time.zone.at(::File.new("#{Rails.root}").mtime), created_at, updated_at, modified_at]
  
  if(options[:include_pages])
    changed_at << Humpyard::Page.select('updated_at').order('updated_at DESC').first.updated_at
  end
  
  (changed_at - [nil]).max.utc    
end
parent_page(options={}) click to toggle source

Get the parent page (even on dynamic pages)

# File app/models/humpyard/page.rb, line 114
def parent_page options={}
  if options[:single_root]
    if parent
      parent
    elsif is_root_page?
      nil
    else
      Humpyard::Page.root_page
    end
  else
    parent
  end
end
parse_path(path) click to toggle source
# File app/models/humpyard/page.rb, line 57
def parse_path(path)
  content_data.parse_path(path)
end
root_elements(yield_name = 'main') click to toggle source

Return the elements on a yield container. Includes shared elemenents from siblings or parents

# File app/models/humpyard/page.rb, line 43
def root_elements(yield_name = 'main')
  # my own elements
  ret = elements.where('container_id IS NULL and page_yield_name = ?', yield_name.to_s).order('position ASC')
  # sibling shared elements
  unless siblings.empty?
    ret += Humpyard::Element.where('container_id IS NULL and page_id in (?) and page_yield_name = ? and shared_state = ?', siblings, yield_name.to_s, Humpyard::Element::SHARED_STATES[:shared_on_siblings]).order('position ASC')
  end
  # ancestors shared elements
  unless ancestor_pages.empty?
    ret += Humpyard::Element.where('container_id IS NULL and page_id in (?) and page_yield_name = ? and shared_state = ?', ancestor_pages, yield_name.to_s, Humpyard::Element::SHARED_STATES[:shared_on_children]).order('position ASC')
  end
  ret
end
suggested_title_for_url_with_index(locale = I18n.locale) click to toggle source
# File app/models/humpyard/page.rb, line 94
def suggested_title_for_url_with_index(locale = I18n.locale)
  return 'index' if self.is_root_page? or Humpyard::Page.count == 0   
  suggested_title_for_url_without_index(locale)
end
template_name() click to toggle source
# File app/models/humpyard/page.rb, line 61
def template_name
  self[:template_name] || Humpyard::config.default_template_name
end