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
29 def self.root_page(options = {})
30   if options[:force_reload]
31     @root_page = Humpyard::Page.select(:id).with_translated_attribute(:title_for_url, :index).first
32   else
33     @root_page ||= Humpyard::Page.select(:id).with_translated_attribute(:title_for_url, :index).first
34   end
35 end

Public Instance Methods

ancestor_pages(options={}) click to toggle source

Get all ancestor pages

    # File app/models/humpyard/page.rb
129 def ancestor_pages options={}
130   if parent_page
131     parent_page.ancestor_pages(options) + [parent_page(options)]
132   else
133     []
134   end
135 end
child_pages(options={}) click to toggle source

Find the child pages

    # File app/models/humpyard/page.rb
101 def child_pages options={}
102   if content_data.is_humpyard_dynamic_page?
103     content_data.child_pages
104   else
105     if options[:single_root] and is_root_page? 
106       Page.where(["parent_id = ? or parent_id IS NULL and NOT id = ?", id, id])
107     else
108       children
109     end
110   end
111 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
72 def human_url(options={})
73   options[:locale] ||= ::I18n.locale
74   options[:format] ||= :html
75   
76   unless Humpyard::config.locales.include? options[:locale].to_sym
77     options[:locale] = Humpyard::config.locales.first
78   end
79   
80   if options[:path_format]
81     format = "/"
82   else
83     format = ".#{options[:format].to_s}" 
84   end
85   
86   if self.title_for_url == 'index' or self.is_root_page?
87     "/#{Humpyard::config.parsed_www_prefix(options).gsub(/[^\/]*$/, '')}"
88   else
89     "/#{Humpyard::config.parsed_www_prefix(options)}#{((self.ancestors.reverse + [self]).collect{|p| p.query_title_for_url(options[:locale])} - ['index']) * '/'}#{format}".gsub(/^index\//,'')
90   end
91 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
138 def is_ancestor_page_of? page
139   page.ancestor_pages.include? self
140 end
is_root_page?() click to toggle source
   # File app/models/humpyard/page.rb
37 def is_root_page?
38   self.id and Humpyard::Page.root_page and self.id == Humpyard::Page.root_page.id
39 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
143 def last_modified options = {}
144   changed_at = [Time.zone.at(::File.new("#{Rails.root}").mtime), created_at, updated_at, modified_at]
145   
146   if(options[:include_pages])
147     changed_at << Humpyard::Page.select('updated_at').order('updated_at DESC').first.updated_at
148   end
149   
150   (changed_at - [nil]).max.utc    
151 end
parent_page(options={}) click to toggle source

Get the parent page (even on dynamic pages)

    # File app/models/humpyard/page.rb
114 def parent_page options={}
115   if options[:single_root]
116     if parent
117       parent
118     elsif is_root_page?
119       nil
120     else
121       Humpyard::Page.root_page
122     end
123   else
124     parent
125   end
126 end
parse_path(path) click to toggle source
   # File app/models/humpyard/page.rb
57 def parse_path(path)
58   content_data.parse_path(path)
59 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
43 def root_elements(yield_name = 'main')
44   # my own elements
45   ret = elements.where('container_id IS NULL and page_yield_name = ?', yield_name.to_s).order('position ASC')
46   # sibling shared elements
47   unless siblings.empty?
48     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')
49   end
50   # ancestors shared elements
51   unless ancestor_pages.empty?
52     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')
53   end
54   ret
55 end
suggested_title_for_url_with_index(locale = I18n.locale) click to toggle source
   # File app/models/humpyard/page.rb
94 def suggested_title_for_url_with_index(locale = I18n.locale)
95   return 'index' if self.is_root_page? or Humpyard::Page.count == 0   
96   suggested_title_for_url_without_index(locale)
97 end
template_name() click to toggle source
   # File app/models/humpyard/page.rb
61 def template_name
62   self[:template_name] || Humpyard::config.default_template_name
63 end