Class: Cardboard::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cardboard::Page
- Includes:
- RankedModel
- Defined in:
- app/models/cardboard/page.rb
Instance Attribute Summary collapse
-
#is_root ⇒ Object
Returns the value of attribute is_root.
-
#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
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”).
- #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
-
#slug=(value) ⇒ Object
overwritten setters/getters.
- #split_path ⇒ Object
- #url ⇒ Object
- #using_slug_backup=(value) ⇒ Object
- #using_slug_backup? ⇒ Boolean
Instance Attribute Details
#is_root ⇒ Object
Returns the value of attribute is_root.
5 6 7 |
# File 'app/models/cardboard/page.rb', line 5 def is_root @is_root end |
#parent_url ⇒ Object
Returns the value of attribute parent_url.
5 6 7 |
# File 'app/models/cardboard/page.rb', line 5 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 => => {}
}}
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'app/models/cardboard/page.rb', line 207 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
227 228 229 230 |
# File 'app/models/cardboard/page.rb', line 227 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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/cardboard/page.rb', line 56 def self.find_by_url(full_url) return nil unless full_url path, slug = self.path_and_slug(full_url) page = self.where(path: path, slug: slug).first if slug && page.nil? #use arel instead of LIKE/ILIKE page = self.where(path: path).where(self.arel_table[:slugs_backup].matches("% #{slug}\n%")).first page.using_slug_backup = true if page end page end |
.homepage ⇒ Object
74 |
# File 'app/models/cardboard/page.rb', line 74 def self.homepage; self.root; end |
.root ⇒ Object
70 71 72 73 |
# File 'app/models/cardboard/page.rb', line 70 def self.root # Homepage is the highest position in the root path where(path: "/").rank(:position).first end |
Instance Method Details
#children ⇒ Object
185 186 187 |
# File 'app/models/cardboard/page.rb', line 185 def children Cardboard::Page.where(path: url) end |
#depth ⇒ Object
193 194 195 196 |
# File 'app/models/cardboard/page.rb', line 193 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”)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/cardboard/page.rb', line 91 def get(field) f = field.split(".") parent_part = self.parts.where(identifier: f.first).first return nil unless parent_part part = parent_part.try(:subparts) if parent_part.repeatable? raise "Part is repeatable, expected each loop" unless f.size == 1 part || [] else return nil unless part f.size == 1 ? part.first : part.first.attr(f.last) end end |
#parent ⇒ Object
153 154 155 |
# File 'app/models/cardboard/page.rb', line 153 def parent @parent ||= Cardboard::Page.find_by_url(path) end |
#parent=(new_parent) ⇒ Object
172 173 174 175 |
# File 'app/models/cardboard/page.rb', line 172 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
177 178 179 |
# File 'app/models/cardboard/page.rb', line 177 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
158 159 160 161 162 163 164 165 |
# File 'app/models/cardboard/page.rb', line 158 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
76 77 78 79 |
# File 'app/models/cardboard/page.rb', line 76 def root? @root_id ||= Page.root.id @root_id == self.id end |
#seo ⇒ Object
SEO children inherit their parent’s SEO settings (these can be overwritten)
128 129 130 131 132 133 134 135 |
# File 'app/models/cardboard/page.rb', line 128 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
137 138 139 140 141 |
# File 'app/models/cardboard/page.rb', line 137 def seo=(hash) # to hash is important here for strong parameters self. = hash.to_hash @_seo = nil end |
#siblings ⇒ Object
189 190 191 |
# File 'app/models/cardboard/page.rb', line 189 def siblings Cardboard::Page.where("path = ? AND id != ?", path, id) end |
#slug=(value) ⇒ Object
overwritten setters/getters
38 39 40 41 |
# File 'app/models/cardboard/page.rb', line 38 def slug=(value) # the user can overwrite the auto generated slug self[:slug] = value.present? ? value.to_url : nil end |
#split_path ⇒ Object
148 149 150 151 |
# File 'app/models/cardboard/page.rb', line 148 def split_path # path.sub(/^\//,'').split("/") # "/path/" => ["path"] path[1..-1].split("/") end |
#url ⇒ Object
143 144 145 146 |
# File 'app/models/cardboard/page.rb', line 143 def url return "/" if slug.blank? #|| self.root? "#{path}#{slug}/" end |
#using_slug_backup=(value) ⇒ Object
51 52 53 |
# File 'app/models/cardboard/page.rb', line 51 def using_slug_backup=(value) @using_slug_backup = value end |
#using_slug_backup? ⇒ Boolean
47 48 49 |
# File 'app/models/cardboard/page.rb', line 47 def using_slug_backup? @using_slug_backup || false end |