Class: Seiten::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#html_optionsObject

Returns the value of attribute html_options.



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

def html_options
  @html_options
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#layoutObject



126
127
128
# File 'lib/seiten/page.rb', line 126

def layout
  @layout || Seiten.config[:default_layout]
end

Returns the value of attribute navigation_id.



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

def navigation_id
  @navigation_id
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

#referObject

Returns the value of attribute refer.



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

def refer
  @refer
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

Instance Method Details

#active?(current_page) ⇒ Boolean

true if page is equal current_page or parent of current_page

Returns:

  • (Boolean)


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

#ancestorsObject



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


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

def breadcrumbs
  @breadcrumbs ||= self_and_ancestors.reverse
end

#childrenObject

get children of page



75
76
77
# File 'lib/seiten/page.rb', line 75

def children
  navigation.pages.where(parent_id: id)
end

#children?Boolean

Returns:

  • (Boolean)


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

def children?
  navigation.pages.find_by(parent_id: id).present?
end

#external?Boolean

returns true if slug starts with http:// or https://

Returns:

  • (Boolean)


28
29
30
# File 'lib/seiten/page.rb', line 28

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


23
24
25
# File 'lib/seiten/page.rb', line 23

def navigation
  Seiten::Navigation.find_by(id: navigation_id)
end

#parentObject

get parent of page



33
34
35
# File 'lib/seiten/page.rb', line 33

def parent
  navigation.pages.find(parent_id)
end

#parent?Boolean

Returns:

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

Returns:

  • (Boolean)


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

#pathObject



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?

  navigation_name = navigation.name == 'application' ? nil : navigation.name

  [:seiten, navigation_name.try(:to_sym), :page, { slug: slug }]
end

#rootObject

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_ancestorsObject



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

def self_and_ancestors
  @self_and_ancestors ||= ancestors.insert(0, self)
end

#template_pathObject Also known as: to_s



108
109
110
111
112
113
# File 'lib/seiten/page.rb', line 108

def template_path
  [
    navigation_id.gsub(/\./, '/'),
    slug.present? ? slug : Seiten.config[:root_page]
  ].join('/')
end