Class: Seiten::Page
- Inherits:
-
Object
- Object
- Seiten::Page
- Defined in:
- lib/seiten/page.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#html_options ⇒ Object
Returns the value of attribute html_options.
-
#id ⇒ Object
Returns the value of attribute id.
- #layout ⇒ Object
-
#navigation_id ⇒ Object
Returns the value of attribute navigation_id.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#refer ⇒ Object
Returns the value of attribute refer.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#active?(current_page) ⇒ Boolean
true if page is equal current_page or parent of current_page.
- #ancestors ⇒ Object
- #breadcrumbs ⇒ Object
-
#children ⇒ Object
get children of page.
- #children? ⇒ Boolean
-
#external? ⇒ Boolean
returns true if slug starts with http:// or https://.
-
#initialize(options = {}) ⇒ Page
constructor
initialize Page object with attributes.
- #navigation ⇒ Object
-
#parent ⇒ Object
get parent of page.
- #parent? ⇒ Boolean
-
#parent_of?(child) ⇒ Boolean
true if child is children of page.
- #path ⇒ Object
-
#root ⇒ Object
get root page of current page branch.
- #self_and_ancestors ⇒ Object
- #template_path ⇒ Object (also: #to_s)
Constructor Details
#initialize(options = {}) ⇒ Page
initialize Page object with attributes
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/seiten/page.rb', line 9 def initialize(={}) @navigation_id = [:navigation_id] @id = [:id] @parent_id = [:parent_id] @title = [:title] @slug = [:slug] @refer = [:refer] @layout = [:layout] @data = [:data].each_with_object({}){|(k,v), h| h[k.to_sym] = v} if [:data] @data ||= {} @html_options = [:html].each_with_object({}){|(k,v), h| h[k.to_sym] = v} if [:html] @html_options ||= {} end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def data @data end |
#html_options ⇒ Object
Returns the value of attribute html_options.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def @html_options end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def id @id end |
#layout ⇒ Object
126 127 128 |
# File 'lib/seiten/page.rb', line 126 def layout @layout || Seiten.config[:default_layout] end |
#navigation_id ⇒ Object
Returns the value of attribute navigation_id.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def @navigation_id end |
#parent_id ⇒ Object
Returns the value of attribute parent_id.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def parent_id @parent_id end |
#refer ⇒ Object
Returns the value of attribute refer.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def refer @refer end |
#slug ⇒ Object
Returns the value of attribute slug.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def slug @slug end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def title @title end |
Instance Method Details
#active?(current_page) ⇒ Boolean
true if page is equal current_page or parent of current_page
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/seiten/page.rb', line 96 def active?(current_page) if current_page if id == current_page.id true elsif parent_of?(current_page) true else false end end end |
#ancestors ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/seiten/page.rb', line 41 def ancestors return @ancestors unless @ancestors.nil? @ancestors = [] return @ancestors unless parent? ancestor = parent loop do @ancestors << ancestor ancestor = ancestor.parent break if ancestor.nil? end @ancestors end |
#breadcrumbs ⇒ Object
61 62 63 |
# File 'lib/seiten/page.rb', line 61 def @breadcrumbs ||= self_and_ancestors.reverse end |
#children ⇒ Object
get children of page
75 76 77 |
# File 'lib/seiten/page.rb', line 75 def children .pages.where(parent_id: id) end |
#children? ⇒ Boolean
79 80 81 |
# File 'lib/seiten/page.rb', line 79 def children? .pages.find_by(parent_id: id).present? end |
#external? ⇒ Boolean
returns true if slug starts with http:// or https://
28 29 30 |
# File 'lib/seiten/page.rb', line 28 def external? !!(slug.match(/^https?:\/\/.+/)) end |
#navigation ⇒ Object
23 24 25 |
# File 'lib/seiten/page.rb', line 23 def Seiten::Navigation.find_by(id: ) end |
#parent ⇒ Object
get parent of page
33 34 35 |
# File 'lib/seiten/page.rb', line 33 def parent .pages.find(parent_id) end |
#parent? ⇒ Boolean
37 38 39 |
# File 'lib/seiten/page.rb', line 37 def parent? parent.present? end |
#parent_of?(child) ⇒ Boolean
true if child is children of page
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/seiten/page.rb', line 84 def parent_of?(child) page = self if child if page.id == child.parent_id true else child.parent.nil? ? false : page.parent_of?(child.parent) end end end |
#path ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/seiten/page.rb', line 116 def path return refer if refer return '#' if slug.nil? return slug if external? = .name == 'application' ? nil : .name [:seiten, .try(:to_sym), :page, { slug: slug }] end |
#root ⇒ Object
get root page of current page branch
66 67 68 69 70 71 72 |
# File 'lib/seiten/page.rb', line 66 def root if self.parent? self.parent.root else self end end |
#self_and_ancestors ⇒ Object
57 58 59 |
# File 'lib/seiten/page.rb', line 57 def self_and_ancestors @self_and_ancestors ||= ancestors.insert(0, self) end |