Class: Documentation::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Documentation::Page
- Defined in:
- app/models/documentation/page.rb
Instance Attribute Summary collapse
-
#parents ⇒ Object
Return a default empty array for parents.
Class Method Summary collapse
-
.find_from_path(path_string) ⇒ Object
Find a page by passing a path to the page from the root of the site.
-
.reorder(parent, order = []) ⇒ Object
Reorder pgaes.
-
.search(query, options = {}) ⇒ Object
Find a page using the searcher if one exists otherwise just fall back to AR searching.
Instance Method Summary collapse
-
#breadcrumb ⇒ Object
Return a full breadcrumb to this page (as it has been loaded).
-
#children ⇒ Object
Return all child pages.
-
#compile_content ⇒ Object
Create the compiled content.
-
#delete_from_index ⇒ Object
Delete this page from the index.
-
#full_permalink ⇒ Object
Return a full permalink tot his page.
-
#has_children? ⇒ Boolean
Does this page have children?.
-
#index ⇒ Object
Index this page.
-
#navigation ⇒ Object
Return pages which should be included in the navigation.
-
#preview_path ⇒ Object
Return the path where this page can be viewed in the site.
-
#set_permalink ⇒ Object
Set the permalink for this page.
Instance Attribute Details
#parents ⇒ Object
Return a default empty array for parents
33 34 35 |
# File 'app/models/documentation/page.rb', line 33 def parents @parents end |
Class Method Details
.find_from_path(path_string) ⇒ Object
Find a page by passing a path to the page from the root of the site
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'app/models/documentation/page.rb', line 182 def self.find_from_path(path_string) raise ActiveRecord::RecordNotFound, "Couldn't find page without a path" if path_string.blank? path_parts = path_string.split('/') path = [] path_parts.each_with_index do |p, i| page = self.where(:parent_id => (path.last ? path.last.id : nil)).find_by_permalink(p) if page page.parents = path.dup page.parent = path.last path << page else raise ActiveRecord::RecordNotFound, "Couldn't find page at #{path_string}" end end path.last end |
.reorder(parent, order = []) ⇒ Object
Reorder pgaes
202 203 204 205 206 207 208 209 210 |
# File 'app/models/documentation/page.rb', line 202 def self.reorder(parent, order = []) order = order.map(&:to_i) order = self.where(:parent_id => parent.id).map(&:id) if order.empty? order.each_with_index do |id, index| command = self.find_by_id!(id) command.position = index + 1 command.save end end |
.search(query, options = {}) ⇒ Object
Find a page using the searcher if one exists otherwise just fall back to AR searching. Returns a Documentation::SearchResult object.
172 173 174 175 176 |
# File 'app/models/documentation/page.rb', line 172 def self.search(query, = {}) if searcher = Documentation.config.searcher searcher.search(query, ) end end |
Instance Method Details
#breadcrumb ⇒ Object
Return a full breadcrumb to this page (as it has been loaded)
61 62 63 |
# File 'app/models/documentation/page.rb', line 61 def @breadcrumb ||= [parents, new_record? ? nil : self].flatten.compact end |
#children ⇒ Object
Return all child pages
93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/documentation/page.rb', line 93 def children @children ||= begin if self.new_record? [] else children = self.class.where(:parent_id => self.id) children.each { |c| c.parents = [parents, self].flatten } children end end end |
#compile_content ⇒ Object
Create the compiled content
143 144 145 146 147 148 |
# File 'app/models/documentation/page.rb', line 143 def compile_content mr = Documentation::MarkdownRenderer.new mr.page = self rc = Redcarpet::Markdown.new(mr, :space_after_headers => true, :fenced_code_blocks => true, :no_intra_emphasis => true, :highlight => true) self.compiled_content = rc.render(self.content.to_s).html_safe end |
#delete_from_index ⇒ Object
Delete this page from the index
162 163 164 165 166 |
# File 'app/models/documentation/page.rb', line 162 def delete_from_index if searcher = Documentation.config.searcher searcher.delete(self) end end |
#full_permalink ⇒ Object
Return a full permalink tot his page
79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/documentation/page.rb', line 79 def full_permalink @full_permalink ||= begin if parents.empty? self.permalink else previous = .compact.map(&:permalink).compact previous.empty? ? self.permalink : previous.join('/') end end end |
#has_children? ⇒ Boolean
Does this page have children?
108 109 110 |
# File 'app/models/documentation/page.rb', line 108 def has_children? !children.empty? end |
#index ⇒ Object
Index this page
153 154 155 156 157 |
# File 'app/models/documentation/page.rb', line 153 def index if searcher = Documentation.config.searcher searcher.index(self) end end |
#navigation ⇒ Object
Return pages which should be included in the navigation
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/documentation/page.rb', line 115 def if has_children? root_parent = parents[-1] if root_parent.nil? pages = self.class.roots else pages = (root_parent || self).children end else root_parent = parents[-2] || parents[-1] if root_parent.nil? || (root_parent.parent.nil? && parents.size <= 1) pages = self.class.roots else pages = (root_parent || self).children end end pages.map do |c| child_pages = [] child_pages = c.children if .include?(c) [c, child_pages] end end |
#preview_path ⇒ Object
Return the path where this page can be viewed in the site
68 69 70 71 72 73 74 |
# File 'app/models/documentation/page.rb', line 68 def preview_path if path = Documentation.config.preview_path_prefix "#{path}#{full_permalink}" else nil end end |
#set_permalink ⇒ Object
Set the permalink for this page
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/documentation/page.rb', line 38 def set_permalink proposed_permalink = self.title.parameterize index = 1 while self.permalink.blank? if self.class.where(:permalink => proposed_permalink, :parent_id => self.parent_id).exists? index += 1 proposed_permalink = self.title.parameterize + "-#{index}" else self.permalink = proposed_permalink end end end |