Class: Humpyard::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/humpyard/page.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.root_page(options = {}) ⇒ Object



29
30
31
32
33
34
35
# 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

Instance Method Details

#ancestor_pages(options = {}) ⇒ Object

Get all ancestor pages



129
130
131
132
133
134
135
# 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 = {}) ⇒ Object

Find the child pages



101
102
103
104
105
106
107
108
109
110
111
# 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 = {}) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Boolean

Is the given page an ancestor of the actual page

Returns:

  • (Boolean)


138
139
140
# File 'app/models/humpyard/page.rb', line 138

def is_ancestor_page_of? page
  page.ancestor_pages.include? self
end

#is_root_page?Boolean

Returns:

  • (Boolean)


37
38
39
# 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 = {}) ⇒ Object

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



143
144
145
146
147
148
149
150
151
# 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 = {}) ⇒ Object

Get the parent page (even on dynamic pages)



114
115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



57
58
59
# File 'app/models/humpyard/page.rb', line 57

def parse_path(path)
  content_data.parse_path(path)
end

#root_elements(yield_name = 'main') ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



94
95
96
97
# 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_nameObject



61
62
63
# File 'app/models/humpyard/page.rb', line 61

def template_name
  self[:template_name] || Humpyard::config.default_template_name
end