Class: Documentation::Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentsObject

Return a default empty array for parents



34
35
36
# File 'app/models/documentation/page.rb', line 34

def parents
  @parents
end

Class Method Details

.find_from_path(path_string) ⇒ Object

Find a page by passing a path to the page from the root of the site

Raises:

  • (ActiveRecord::RecordNotFound)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/documentation/page.rb', line 183

def self.find_from_path(path_string)
  raise ActiveRecord::RecordNotFound, "Couldn't find page without a path" if path_string.blank?
  path_parts = path_string.split('/')
  path = []
  path_parts.each_with_index do |p, i|
    page = self.where(:parent_id => (path.last ? path.last.id : nil)).find_by_permalink(p)
    if page
      page.parents = path.dup
      page.parent = path.last
      path << page
    else
      raise ActiveRecord::RecordNotFound, "Couldn't find page at #{path_string}"
    end
  end
  path.last
end

.reorder(parent, order = []) ⇒ Object

Reorder pgaes



203
204
205
206
207
208
209
210
211
# File 'app/models/documentation/page.rb', line 203

def self.reorder(parent, order = [])
  order = order.map(&:to_i)
  order = self.where(:parent_id => parent.id).map(&:id) if order.empty?
  order.each_with_index do |id, index|
    command = self.find_by_id!(id)
    command.position = index + 1
    command.save
  end
end

.search(query, options = {}) ⇒ Object

Find a page using the searcher if one exists otherwise just fall back to AR searching. Returns a Documentation::SearchResult object.



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

def self.search(query, options = {})
  if searcher = Documentation.config.searcher
    searcher.search(query, options)
  end
end

Instance Method Details

Return a full breadcrumb to this page (as it has been loaded)



62
63
64
# File 'app/models/documentation/page.rb', line 62

def breadcrumb
  @breadcrumb ||= [parents, new_record? ? nil : self].flatten.compact
end

#childrenObject

Return all child pages



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/documentation/page.rb', line 94

def children
  @children ||= begin
    if self.new_record?
      []
    else
      children = self.class.where(:parent_id => self.id)
      children.each { |c| c.parents = [parents, self].flatten }
      children
    end
  end
end

#compile_contentObject

Create the compiled content



144
145
146
147
148
149
# File 'app/models/documentation/page.rb', line 144

def compile_content
  mr = Documentation::MarkdownRenderer.new
  mr.page = self
  rc = Redcarpet::Markdown.new(mr, :space_after_headers => true, :fenced_code_blocks => true, :no_intra_emphasis => true, :highlight => true)
  self.compiled_content = rc.render(self.content.to_s).html_safe
end

#delete_from_indexObject

Delete this page from the index



163
164
165
166
167
# File 'app/models/documentation/page.rb', line 163

def delete_from_index
  if searcher = Documentation.config.searcher
    searcher.delete(self)
  end
end

Return a full permalink tot his page



80
81
82
83
84
85
86
87
88
89
# File 'app/models/documentation/page.rb', line 80

def full_permalink
  @full_permalink ||= begin
    if parents.empty?
      self.permalink
    else
      previous = breadcrumb.compact.map(&:permalink).compact
      previous.empty? ? self.permalink : previous.join('/')
    end
  end
end

#has_children?Boolean

Does this page have children?

Returns:

  • (Boolean)


109
110
111
# File 'app/models/documentation/page.rb', line 109

def has_children?
  !children.empty?
end

#indexObject

Index this page



154
155
156
157
158
# File 'app/models/documentation/page.rb', line 154

def index
  if searcher = Documentation.config.searcher
    searcher.index(self)
  end
end

Return pages which should be included in the navigation



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/documentation/page.rb', line 116

def navigation
  if has_children?
    root_parent = parents[-1]
    if root_parent.nil?
      pages = self.class.roots
    else
      pages = (root_parent || self).children
    end
  else
    root_parent = parents[-2] || parents[-1]
    if root_parent.nil? || (root_parent.parent.nil? && parents.size <= 1)
      pages = self.class.roots
    else
      pages = (root_parent || self).children
    end
  end
  
  
  pages.map do |c|
    child_pages = []
    child_pages = c.children if breadcrumb.include?(c)
    [c, child_pages]
  end
end

#preview_pathObject

Return the path where this page can be viewed in the site



69
70
71
72
73
74
75
# File 'app/models/documentation/page.rb', line 69

def preview_path
  if path = Documentation.config.preview_path_prefix
    "#{path}#{full_permalink}"
  else
    nil
  end
end

Set the permalink for this page



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/documentation/page.rb', line 39

def set_permalink
  proposed_permalink = self.title.parameterize
  index = 1
  while self.permalink.blank?
    if self.class.where(:permalink => proposed_permalink, :parent_id => self.parent_id).exists?
      index += 1
      proposed_permalink = self.title.parameterize + "-#{index}"
    else
      self.permalink = proposed_permalink
    end
  end
end