Class: Skyline::Page
- Includes:
- Positionable
- Defined in:
- app/models/skyline/page.rb
Defined Under Namespace
Classes: Data
Instance Attribute Summary
Attributes inherited from Article
#becomes_default_variant_after_save
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
#can_have_multiple_variants?, can_have_multiple_variants?, #data_class, #depublish, #depublishable?, #destroy, #dup, #editable_by?, lockable?, #lockable?, #preview_wrapper_page, #previewable?, publishable?, #published?, #renderable?, #renderable_scope, #right_prefix, #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.
137 138 139 140 141 142 143 144 145 |
# File 'app/models/skyline/page.rb', line 137 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
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 108 |
# File 'app/models/skyline/page.rb', line 66 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
121 122 123 124 125 126 127 |
# File 'app/models/skyline/page.rb', line 121 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
147 148 149 150 151 152 |
# File 'app/models/skyline/page.rb', line 147 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
58 59 60 |
# File 'app/models/skyline/page.rb', line 58 def right_prefix "page" end |
.root ⇒ Object
110 111 112 |
# File 'app/models/skyline/page.rb', line 110 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
159 160 161 |
# File 'app/models/skyline/page.rb', line 159 def nesting @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
155 156 157 |
# File 'app/models/skyline/page.rb', line 155 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 |