Class: Seiten::Page
- Inherits:
-
Object
- Object
- Seiten::Page
- Defined in:
- lib/seiten/page.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
get children of page.
-
#id ⇒ Object
Returns the value of attribute id.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#redirect ⇒ Object
Returns the value of attribute redirect.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .all ⇒ Object
-
.find(id) ⇒ Object
find page by id.
-
.find_by_parent_id(parent_id) ⇒ Object
find all pages by parent_id.
-
.find_by_slug(slug) ⇒ Object
find a page by slug.
-
.get_breadcrumb(page) ⇒ Object
get breadcrumb of given page (reversed).
Instance Method Summary collapse
-
#active?(current_page) ⇒ Boolean
true if page is equal current_page or parent of current_page.
-
#branch_root ⇒ Object
TODO: Find a better name for this get root page of current page branch.
-
#external? ⇒ Boolean
returns true if slug starts with http:// or https://.
-
#initialize(options = {}) ⇒ Page
constructor
initialize Page object with attributes.
-
#parent ⇒ Object
get parent of page.
- #parent? ⇒ Boolean
-
#parent_of?(child) ⇒ Boolean
true if child is children of page.
Constructor Details
#initialize(options = {}) ⇒ Page
initialize Page object with attributes
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/seiten/page.rb', line 8 def initialize(={}) @id = [:id] @parent_id = [:parent_id] @title = [:title] @slug = [:slug] @external = [:external] @redirect = [:redirect] @layout = [:layout] = [:metadata].each_with_object({}){|(k,v), h| h[k.to_sym] = v} if [:metadata] end |
Instance Attribute Details
#children ⇒ Object
get children of page
80 81 82 |
# File 'lib/seiten/page.rb', line 80 def children @children 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
Returns the value of attribute layout.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def layout @layout end |
#metadata ⇒ Object
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def 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 |
#redirect ⇒ Object
Returns the value of attribute redirect.
5 6 7 |
# File 'lib/seiten/page.rb', line 5 def redirect @redirect 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 |
Class Method Details
.all ⇒ Object
21 22 23 |
# File 'lib/seiten/page.rb', line 21 def all Seiten::PageStore.current.pages end |
.find(id) ⇒ Object
find page by id
26 27 28 |
# File 'lib/seiten/page.rb', line 26 def find(id) all.select { |page| page.id == id }.first end |
.find_by_parent_id(parent_id) ⇒ Object
find all pages by parent_id
31 32 33 |
# File 'lib/seiten/page.rb', line 31 def find_by_parent_id(parent_id) all.select { |page| page.parent_id == parent_id } end |
.find_by_slug(slug) ⇒ Object
find a page by slug
36 37 38 39 40 41 |
# File 'lib/seiten/page.rb', line 36 def find_by_slug(slug) if slug slug = slug[1..-1] if slug[0] == "/" end all.select { |page| page.slug == slug }.first end |
.get_breadcrumb(page) ⇒ Object
get breadcrumb of given page (reversed)
44 45 46 47 48 49 50 51 |
# File 'lib/seiten/page.rb', line 44 def (page) pages ||= [] pages << page if page.parent pages << (page.parent) end pages.flatten end |
Instance Method Details
#active?(current_page) ⇒ Boolean
true if page is equal current_page or parent of current_page
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/seiten/page.rb', line 97 def active?(current_page) if current_page if id == current_page.id true elsif parent_of?(current_page) true else false end end end |
#branch_root ⇒ Object
TODO: Find a better name for this get root page of current page branch
71 72 73 74 75 76 77 |
# File 'lib/seiten/page.rb', line 71 def branch_root if self.parent? self.parent.branch_root else self end end |
#external? ⇒ Boolean
returns true if slug starts with http:// or https://
56 57 58 |
# File 'lib/seiten/page.rb', line 56 def external? !!(slug.match(/^https?:\/\/.+/)) end |
#parent ⇒ Object
get parent of page
61 62 63 |
# File 'lib/seiten/page.rb', line 61 def parent Page.find(parent_id) end |
#parent? ⇒ Boolean
65 66 67 |
# File 'lib/seiten/page.rb', line 65 def parent? parent.present? end |
#parent_of?(child) ⇒ Boolean
true if child is children of page
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/seiten/page.rb', line 85 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 |