Class: Skyline::Page
- Extended by:
- ActiveSupport::Memoizable
- Includes:
- Positionable
- Defined in:
- app/models/skyline/page.rb
Defined Under Namespace
Classes: Data
Class Method Summary collapse
-
.find_by_url(url_parts, root = nil) ⇒ Array<Skyline::Page,Array<String>>
Find the page by url_parts and return the page and url_parts left over after we’ve found the deepest page.
-
.group_by_parent_id ⇒ Object
returns an Array of hashes.
-
.menu(level = 1, nesting = nil) ⇒ Object
- build menu of certain level ==== Parameters level<Integer>
- level of the menu that has to be returned nesting<Array>
-
the nesting of page starting with the root node.
- .reorder(pages) ⇒ Object
- .right_prefix ⇒ Object
- .root ⇒ Object
Instance Method Summary collapse
-
#create_new!(position) ⇒ Object
- create a new page on position ==== Parameters position<Symbol>
-
Can either be :below, :above or :in.
- #destroyable? ⇒ Boolean
- #keep_history? ⇒ Boolean
- #menu ⇒ Object
- #move_behind=(parent_id) ⇒ Object
- #nesting ⇒ Object
- #parents ⇒ Object
-
#path ⇒ Object
Returns the path (url excluding current url_path), with trailing / for the root page: nil pages directly in the root: / other pages, ie: /a/b/c/.
- #root? ⇒ Boolean
- #url ⇒ Object
Methods inherited from Article
#data_class, #depublish, #depublishable?, #destroy, #editable_by?, #enable_locking?, #enable_multiple_variants?, #enable_publishing?, #preview_wrapper_page, #previewable?, publishable?, #published?, #renderable?, #renderable_scope, #right_prefix, #rollbackable?, #set_default_variant, #set_default_variant!, #site, #sites, #title, to_param
Class Method Details
.find_by_url(url_parts, root = nil) ⇒ Array<Skyline::Page,Array<String>>
Find the page by url_parts and return the page and url_parts left over after we’ve found the deepest page.
136 137 138 139 140 141 142 143 144 |
# File 'app/models/skyline/page.rb', line 136 def find_by_url(url_parts, root = nil) root ||= self.root return nil unless root return [root, []] if url_parts.empty? if child = root.children.find_by_url_part(url_parts.first) return self.find_by_url(url_parts[1..-1], child) end return [root, url_parts] end |
.group_by_parent_id ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/skyline/page.rb', line 65 def group_by_parent_id out = {} pages = self.connection.select_all(" SELECT page.id, page.parent_id, data.navigation_title as navigation_title, data.title as title, page.locked, page.published_publication_id, page.default_variant_id, published_publication.variant_id AS published_publication_variant_id, published_publication.version AS published_publication_version, default_variant.version AS default_variant_version FROM #{self.table_name} AS page LEFT JOIN skyline_page_data AS data ON data.id=page.default_variant_data_id LEFT JOIN skyline_article_versions AS published_publication ON published_publication.id=page.published_publication_id LEFT JOIN skyline_article_versions AS default_variant ON default_variant.id=default_variant_id WHERE page.type='Skyline::Page' ORDER BY page.position") pages.each do |o| class << o def id; self["id"].to_i; end def parent_id; self["parent_id"].blank? ? nil : self["parent_id"].to_i; end def title if self["navigation_title"].blank? self["title"].blank? ? "n/a" : self["title"] else self["navigation_title"] end end def published?; self["published_publication_id"].present?; end def identical_to_publication? self["published_publication_variant_id"] == self["default_variant_id"] && self["published_publication_version"] == self["default_variant_version"] end def open; true; end end out[o.parent_id] ||= [] out[o.parent_id] << o end out end |
.menu(level = 1, nesting = nil) ⇒ Object
build menu of certain level
Parameters
- level<Integer>
-
level of the menu that has to be returned
- nesting<Array>
-
the nesting of page starting with the root node
Returns
- <Array>
-
an array of pages to display in the menu
120 121 122 123 124 125 126 |
# File 'app/models/skyline/page.rb', line 120 def (level = 1, nesting = nil) = [] << {:page => self.root, :children => []} if level == 1 && self.root. nesting ||= [self.root] += nesting[level-1]. if nesting[level-1] end |
.reorder(pages) ⇒ Object
146 147 148 149 150 151 |
# File 'app/models/skyline/page.rb', line 146 def reorder(pages) return unless pages.kind_of?(Array) pages.each_with_index do |parent_id, position| self.connection.execute("UPDATE #{self.table_name} SET position=#{position.to_i} WHERE id=#{parent_id.to_i}") end end |
.right_prefix ⇒ Object
57 58 59 |
# File 'app/models/skyline/page.rb', line 57 def right_prefix "page" end |
.root ⇒ Object
109 110 111 |
# File 'app/models/skyline/page.rb', line 109 def root self.first(:conditions => "parent_id IS NULL") end |
Instance Method Details
#create_new!(position) ⇒ Object
create a new page on position
Parameters
- position<Symbol>
-
Can either be :below, :above or :in
Returns
- <Page>
-
The new page
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/models/skyline/page.rb', line 201 def create_new!(position) page = Skyline::Page.new self.class.transaction do case position when :below page.parent = self.parent page.position = self.position + 1 self.class.connection.execute("UPDATE #{self.class.table_name} SET position=position+1 WHERE #{self.parent ? "parent_id=#{self.parent.id}" : 'parent_id IS NULL'} AND position>#{self.position}") when :above page.parent = self.parent page.position = self.position self.class.connection.execute("UPDATE #{self.class.table_name} SET position=position+1 WHERE #{self.parent ? "parent_id=#{self.parent.id}" : 'parent_id IS NULL'} AND position>=#{self.position}") when :in page.parent = self end page.sites << self.site page.save end page end |
#destroyable? ⇒ Boolean
186 187 188 |
# File 'app/models/skyline/page.rb', line 186 def destroyable? !self.persistent? && self.children.empty? && self.published_publication == nil end |
#keep_history? ⇒ Boolean
190 191 192 |
# File 'app/models/skyline/page.rb', line 190 def keep_history? Skyline::Configuration.enable_publication_history end |
#menu ⇒ Object
182 183 184 |
# File 'app/models/skyline/page.rb', line 182 def self.children.collect{|child| {:page => child, :children => child.} if child.published_publication_data.andand.}.compact end |
#move_behind=(parent_id) ⇒ Object
224 225 226 227 |
# File 'app/models/skyline/page.rb', line 224 def move_behind=(parent_id) @do_move_behind = true @move_behind = parent_id end |
#nesting ⇒ Object
158 159 160 |
# File 'app/models/skyline/page.rb', line 158 def nesting self.root? ? [self] : self.parent.nesting + [self] end |
#parents ⇒ Object
163 164 165 |
# File 'app/models/skyline/page.rb', line 163 def parents self.nesting.dup[0..-2] end |
#path ⇒ Object
Returns the path (url excluding current url_path), with trailing / for the root page: nil pages directly in the root: / other pages, ie: /a/b/c/
171 172 173 174 175 |
# File 'app/models/skyline/page.rb', line 171 def path return nil if self.root? path = "/" + self.parents[1..-1].collect{|page| page.url_part}.join("/") + "/" path.squeeze("/") end |
#root? ⇒ Boolean
154 155 156 |
# File 'app/models/skyline/page.rb', line 154 def root? !self.parent end |
#url ⇒ Object
177 178 179 180 |
# File 'app/models/skyline/page.rb', line 177 def url return "/" if self.root? self.path + self.url_part end |