Class: Cardboard::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cardboard::Page
- Includes:
- UrlConcern, RankedModel
- Defined in:
- app/models/cardboard/page.rb
Instance Attribute Summary collapse
-
#parent_url ⇒ Object
Returns the value of attribute parent_url.
Class Method Summary collapse
-
.arrange(root_page = nil) ⇒ Object
Arrange array of nodes into a nested hash of the form => children, where children = {} if the node has no children.
- .clear_arranged_pages ⇒ Object
-
.find_by_url(full_url) ⇒ Object
class methods.
- .homepage ⇒ Object
-
.root ⇒ Object
overwritten setters/getters.
Instance Method Summary collapse
- #children ⇒ Object
- #depth ⇒ Object
-
#get(field) ⇒ Object
slideshow = @page.get(“slideshow”) slideshow.field(“image1”) slideshow.each{|p| p.field(“image”)} slideshow.get(“slide1”).
- #meta_seo ⇒ Object
- #meta_seo=(hash) ⇒ Object
- #parent ⇒ Object
- #parent=(new_parent) ⇒ Object
- #parent_id=(new_parent_id) ⇒ Object
-
#parent_url_options ⇒ Object
Get all other pages.
- #root? ⇒ Boolean
-
#seo ⇒ Object
SEO children inherit their parent’s SEO settings (these can be overwritten).
- #seo=(hash) ⇒ Object
- #siblings ⇒ Object
- #split_path ⇒ Object
- #template_hash ⇒ Object
Methods included from UrlConcern
Instance Attribute Details
#parent_url ⇒ Object
Returns the value of attribute parent_url.
7 8 9 |
# File 'app/models/cardboard/page.rb', line 7 def parent_url @parent_url end |
Class Method Details
.arrange(root_page = nil) ⇒ Object
Arrange array of nodes into a nested hash of the form => children, where children = {} if the node has no children
Example: => {
#<Cardboard::Page => {
#<Cardboard::Page => {}
#<Cardboard::Page => => {}
}}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/cardboard/page.rb', line 167 def self.arrange(root_page = nil) root_page ||= self.root return unless root_page # TODO: use root_page... Rails.cache.fetch("arranged_pages") do pages = self.preordered pages.inject(ActiveSupport::OrderedHash.new) do |ordered_hash, page| (["/"] + page.split_path).inject(ordered_hash) do |insertion_hash, subpath| insertion_hash.each do |parent, children| insertion_hash = children if subpath == parent.slug end insertion_hash end[page] = ActiveSupport::OrderedHash.new ordered_hash end end end |
.clear_arranged_pages ⇒ Object
187 188 189 190 |
# File 'app/models/cardboard/page.rb', line 187 def self.clear_arranged_pages # clear cache when a page changes Rails.cache.delete("arranged_pages") end |
.find_by_url(full_url) ⇒ Object
class methods
57 58 59 |
# File 'app/models/cardboard/page.rb', line 57 def self.find_by_url(full_url) Cardboard::Url.urlable_for(full_url, type: self.name) end |
.homepage ⇒ Object
42 |
# File 'app/models/cardboard/page.rb', line 42 def self.homepage; self.root; end |
.root ⇒ Object
overwritten setters/getters
38 39 40 41 |
# File 'app/models/cardboard/page.rb', line 38 def self.root #TODO: check that join work correctly with_path('/').rank(:position).first end |
Instance Method Details
#children ⇒ Object
145 146 147 |
# File 'app/models/cardboard/page.rb', line 145 def children Cardboard::Page.with_path(url) end |
#depth ⇒ Object
153 154 155 156 |
# File 'app/models/cardboard/page.rb', line 153 def depth # root is depth 0 split_path.size end |
#get(field) ⇒ Object
slideshow = @page.get(“slideshow”) slideshow.field(“image1”) slideshow.each{|p| p.field(“image”)} slideshow.get(“slide1”)
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/cardboard/page.rb', line 77 def get(field) f = field.split(".") parts = self.parts.where(identifier: f.first) if template_hash[f.first.to_sym][:repeatable] raise "Part is repeatable, expected each loop" unless f.size == 1 parts else part = parts.first return nil unless part f.size == 1 ? part : part.attr(f.last) end end |
#meta_seo ⇒ Object
52 53 54 |
# File 'app/models/cardboard/page.rb', line 52 def .slice("description", "title") end |
#meta_seo=(hash) ⇒ Object
49 50 51 |
# File 'app/models/cardboard/page.rb', line 49 def (hash) self. = .merge(hash) end |
#parent ⇒ Object
113 114 115 |
# File 'app/models/cardboard/page.rb', line 113 def parent @parent ||= Cardboard::Page.find_by_url(path) end |
#parent=(new_parent) ⇒ Object
132 133 134 135 |
# File 'app/models/cardboard/page.rb', line 132 def parent=(new_parent) return nil if new_parent && !new_parent.is_a?(Cardboard::Page) self.path = new_parent ? new_parent.url : "/" end |
#parent_id=(new_parent_id) ⇒ Object
137 138 139 |
# File 'app/models/cardboard/page.rb', line 137 def parent_id=(new_parent_id) self.parent = Cardboard::Page.where(identifier: new_parent_id).first end |
#parent_url_options ⇒ Object
Get all other pages
118 119 120 121 122 123 124 125 |
# File 'app/models/cardboard/page.rb', line 118 def # @parent_url_options ||= begin Cardboard::Page.all.inject(["/"]) do |result, elm| result << elm.url unless elm.id == self.id result end.sort # end end |
#root? ⇒ Boolean
44 45 46 47 |
# File 'app/models/cardboard/page.rb', line 44 def root? return @root unless @root.nil? @root = self.id == Page.root.id end |
#seo ⇒ Object
SEO children inherit their parent’s SEO settings (these can be overwritten)
94 95 96 97 98 99 100 101 |
# File 'app/models/cardboard/page.rb', line 94 def seo @_seo ||= begin seo = self. seo = self.parent.seo.merge(seo) if parent seo = Page.root.seo.merge(seo) unless root? seo end end |
#seo=(hash) ⇒ Object
103 104 105 106 107 |
# File 'app/models/cardboard/page.rb', line 103 def seo=(hash) # to hash is important here for strong parameters self. = hash.to_hash @_seo = nil end |
#siblings ⇒ Object
149 150 151 |
# File 'app/models/cardboard/page.rb', line 149 def siblings Cardboard::Page.with_path(path).where("cardboard_pages.id != ?", id) end |
#split_path ⇒ Object
109 110 111 |
# File 'app/models/cardboard/page.rb', line 109 def split_path path[1..-1].split("/") end |
#template_hash ⇒ Object
65 66 67 |
# File 'app/models/cardboard/page.rb', line 65 def template_hash @template_hash ||= self.template.fields end |