Class: Page

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/page.rb

Direct Known Subclasses

BlogPost

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#readonly?

Class Method Details

.create_help_pageObject



194
195
196
197
198
199
# File 'app/models/page.rb', line 194

def self.create_help_page
  help_page = Page.create
  help_page.add_flag :help
  n = help_page.nav_node; n.hidden_menu = true; n.save;
  return help_page
end

.create_imprintObject

imprint



203
204
205
206
207
# File 'app/models/page.rb', line 203

def self.create_imprint
  imprint_page = Page.create
  imprint_page.add_flag :imprint
  return imprint_page
end

.create_intranet_root(attrs = {}) ⇒ Object



170
171
172
173
174
175
176
177
# File 'app/models/page.rb', line 170

def self.create_intranet_root(attrs = {})
  root_page = Page.find_by_flag :root
  root_page = self.create_root unless root_page
  intranet_root = root_page.child_pages.create(title: "Intranet")
  intranet_root.update_attributes attrs
  intranet_root.add_flag :intranet_root
  return intranet_root
end

.create_root(attrs = {}) ⇒ Object



151
152
153
154
155
156
157
# File 'app/models/page.rb', line 151

def self.create_root(attrs = {})
  root_page = Page.create(title: "Root")
  root_page.update_attributes attrs
  root_page.add_flag :root
  n = root_page.nav_node; n.slim_menu = true; n.save; n = nil
  return root_page
end

.find_help_pageObject

help page



186
187
188
# File 'app/models/page.rb', line 186

def self.find_help_page
  Page.find_by_flag( :help )
end

.find_imprintObject



208
209
210
# File 'app/models/page.rb', line 208

def self.find_imprint
  Page.find_by_flag :imprint
end

.find_intranet_rootObject

intranet root



162
163
164
# File 'app/models/page.rb', line 162

def self.find_intranet_root
  Page.find_by_flag( :intranet_root )
end

.find_or_create_help_pageObject



190
191
192
# File 'app/models/page.rb', line 190

def self.find_or_create_help_page
  self.find_help_page || self.create_help_page
end

.find_or_create_intranet_rootObject



166
167
168
# File 'app/models/page.rb', line 166

def self.find_or_create_intranet_root
  self.find_intranet_root || self.create_intranet_root
end

.find_or_create_rootObject



147
148
149
# File 'app/models/page.rb', line 147

def self.find_or_create_root
  self.find_root || self.create_root
end

.find_rootObject

root



143
144
145
# File 'app/models/page.rb', line 143

def self.find_root
  Page.find_by_flag( :root )
end

.intranet_rootObject



179
180
181
# File 'app/models/page.rb', line 179

def self.intranet_root
  self.find_or_create_intranet_root
end

Instance Method Details

#<<(child) ⇒ Object

Add a child to this page. This could be a blog entry or another page or even a group. Example:

my_page << another_page


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/page.rb', line 70

def <<(child)
  unless child.in? self.children
    if child.in? self.descendants
      link = DagLink.where(
        ancestor_type: 'Page', ancestor_id: self.id, 
        descendant_type: child.class.name, descendant_id: child.id
      ).first
      link.make_direct
      link.save
    else
      self.child_pages << child if child.kind_of? Page
      self.child_groups << child if child.kind_of? Group
    end
  end
end

#attachments_by_type(type) ⇒ Object

This return alls attachments of a certain type. type could be something like ‘image` or `pdf`. It will be compared to the attachments’ mime type.



116
117
118
# File 'app/models/page.rb', line 116

def attachments_by_type( type )
  attachments.find_by_type type
end

#blog_entriesObject

This method returns all Page objects that can be regarded as blog entries of self. Blog entries are simply child pages of self that have the :blog_entry flag. They won’t show up in the vertical menu.

Page: “My Blog”

|------------------ Page: "Entry 1"
|------------------ Page: "Entry 2"


132
133
134
# File 'app/models/page.rb', line 132

def blog_entries
  self.child_pages.where(type: "BlogPost").order('created_at DESC')
end

#fill_cacheObject



13
14
15
# File 'app/models/page.rb', line 13

def fill_cache
  group
end

#groupObject

This is the group the page belongs to, for example:

group_1
  |----- group_2
  |        |------ page_2 : belongs to group_2
  |
  |--------------- page_1 : belongs to group_1


34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/page.rb', line 34

def group
  # Do not use `ancestor_groups.last` here. Where this would work somethimes, it depends on the
  # order of creation of the links.
  cached do
    next_parent = parent_groups.first || parent_pages.first
    until next_parent.nil? or next_parent.kind_of? Group
      next_parent = next_parent.parent_groups.first || next_parent.parent_pages.first
    end
    next_parent
  end
end

#redirect_toObject

Redirection




90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/page.rb', line 90

def redirect_to

  # http://example.com
  return super if super.kind_of?(String) && super.include?("://")

  # users#index
  if super.kind_of?(String) && super.include?("#")
    controller, action = super.split("#")
    return { controller: controller, action: action } if controller && action
  end

  # { controller: "users", action: "index" }
  return super if super.kind_of? Hash

  # else
  return nil
end

#titleObject

This is the page title. If the title is not given in the database, try to translate the flag of the page, e.g. for the ‘imprint’ page.



22
23
24
# File 'app/models/page.rb', line 22

def title
  super.present? ? super : I18n.translate(self.flags.first, default: '')
end

#to_paramObject

This sets the format of the Page urls to be

example.com/pages/24-products

rather than just

example.com/pages/24


57
58
59
# File 'app/models/page.rb', line 57

def to_param
  "#{id} #{title}".parameterize
end