Class: Seiten::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/seiten/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  @id        = options[:id]
  @parent_id = options[:parent_id]
  @title     = options[:title]
  @slug      = options[:slug]
  @external  = options[:external]
  @redirect  = options[:redirect]
  @layout    = options[:layout]
    = options[:metadata].each_with_object({}){|(k,v), h| h[k.to_sym] = v} if options[:metadata]
end

Instance Attribute Details

#childrenObject

get children of page



80
81
82
# File 'lib/seiten/page.rb', line 80

def children
  @children
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/seiten/page.rb', line 5

def id
  @id
end

#layoutObject

Returns the value of attribute layout.



5
6
7
# File 'lib/seiten/page.rb', line 5

def layout
  @layout
end

#metadataObject

Returns the value of attribute metadata.



5
6
7
# File 'lib/seiten/page.rb', line 5

def 
  
end

#parent_idObject

Returns the value of attribute parent_id.



5
6
7
# File 'lib/seiten/page.rb', line 5

def parent_id
  @parent_id
end

#redirectObject

Returns the value of attribute redirect.



5
6
7
# File 'lib/seiten/page.rb', line 5

def redirect
  @redirect
end

#slugObject

Returns the value of attribute slug.



5
6
7
# File 'lib/seiten/page.rb', line 5

def slug
  @slug
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/seiten/page.rb', line 5

def title
  @title
end

Class Method Details

.allObject



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 get_breadcrumb(page)
  pages ||= []
  pages << page
  if page.parent
    pages << get_breadcrumb(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

Returns:

  • (Boolean)


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_rootObject

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://

Returns:

  • (Boolean)


56
57
58
# File 'lib/seiten/page.rb', line 56

def external?
  !!(slug.match(/^https?:\/\/.+/))
end

#parentObject

get parent of page



61
62
63
# File 'lib/seiten/page.rb', line 61

def parent
  Page.find(parent_id)
end

#parent?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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