Module: Documentation::ViewHelpers

Included in:
ApplicationHelper
Defined in:
lib/documentation/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#documentation_authorizerObject

Return the documentation authorizer



108
109
110
# File 'lib/documentation/view_helpers.rb', line 108

def documentation_authorizer
  @documentation_authorizer ||= Documentation.config.authorizer.new(controller)
end

#documentation_breadcrumb_for(page, options = {}) ⇒ Object

Return a breadcrumb for the given page



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/documentation/view_helpers.rb', line 21

def documentation_breadcrumb_for(page, options = {})
  options[:root_link] = options[:root_link].nil? ? t('documentation.helpers.documentation_breadcrumb_for.default_root_link') : options[:root_link]
  options[:class]     ||= 'breadcrumb'
  
  String.new.tap do |s|
    s << "<nav class='#{options[:class]}'><ul>"
    
    if options[:root_link]
      s << "<li><a href='#{documentation_doc_root}'>#{options[:root_link]}</a></li>"
    end
    
    if page.is_a?(::Documentation::Page)
      page.breadcrumb.each do |child|
        s << "<li><a href='#{documentation_doc_root}/#{child.full_permalink}'>#{child.title}</a></li>"
      end
    end
    
    s << "</ul></nav>"
  end.html_safe
end

#documentation_content_for(page) ⇒ Object

Return appropriate content for a given page



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/documentation/view_helpers.rb', line 73

def documentation_content_for(page)
  # Get the content
  content = page.compiled_content.to_s
  
  # Insert navigation
  content.gsub!("<p class=''>{{nav}}</p>") do 
    children = page.children
    children = children.select { |c| documentation_authorizer.can_view_page?(c) }
    items = children.map { |c| "<li><a href='#{documentation_doc_root}/#{c.full_permalink}'>#{c.title}</a></li>" }.join
    "<ul class='pages'>#{items}</ul>"
  end
  
  # Set the document root as appropriate
  content.gsub!("{{docRoot}}", documentation_doc_root)
  
  # Return HTML safe content
  content.html_safe
end

#documentation_doc_rootObject

Return the documentation document root



95
96
97
98
99
100
101
102
103
# File 'lib/documentation/view_helpers.rb', line 95

def documentation_doc_root
  @documentation_doc_root ||= begin
    if controller.is_a?(Documentation::ApplicationController)
      ::Documentation::Engine.mounted_path.to_s
    else
      ::Documentation.config.preview_path_prefix.to_s.sub(/\/$/, '')
    end
  end
end

#documentation_edit_page_path(page) ⇒ Object

Path to edit a page in the manager UI



7
8
9
# File 'lib/documentation/view_helpers.rb', line 7

def documentation_edit_page_path(page)
  "#{::Documentation::Engine.mounted_path}/edit/#{page.full_permalink}"
end

#documentation_navigation_tree_for(page) ⇒ Object

Return a default navigation tree for the given page



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/documentation/view_helpers.rb', line 45

def documentation_navigation_tree_for(page)
  String.new.tap do |s|
    s << "<ul>"
    if page.is_a?(::Documentation::Page)
      page.navigation.select { |p,c| documentation_authorizer.can_view_page?(p) }.each do |p, children|
        s << "<li>"
        s << "<a #{page == p ? "class='active'" : ''} href='#{documentation_doc_root}/#{p.full_permalink}'>#{p.title}</a>"
        unless children.empty?
          s << "<ul>"
          children.select { |c| documentation_authorizer.can_view_page?(c) }.each do |p|
            s << "<li><a #{page == p ? "class='active'" : ''} href='#{documentation_doc_root}/#{p.full_permalink}'>#{p.title}</a></li>"
          end
          s << "</ul>"
        end
        s << "</li>"            
      end
    else
      ::Documentation::Page.roots.each do |page|
        s << "<li><a href='#{documentation_doc_root}/#{page.full_permalink}'>#{page.title}</a></li>"
      end
    end
    s << "</ul>"
  end.html_safe
end

#documentation_page_path(page) ⇒ Object

Path to view a page in the manager UI



14
15
16
# File 'lib/documentation/view_helpers.rb', line 14

def documentation_page_path(page)
  "#{::Documentation::Engine.mounted_path}/#{page.try(:full_permalink)}"
end