Class: Garage::Docs::Document

Inherits:
Object
  • Object
show all
Includes:
Authorizable, Representer
Defined in:
lib/garage/docs/document.rb

Instance Attribute Summary collapse

Attributes included from Representer

#params, #partial, #representer_attrs, #selector

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Representer

#default_url_options, #handle_definition?, included, #link_path_for, #partial?, #render_hash, #represent!, representers, #to_resource

Methods included from Authorizable

#authorize!, #build_permissions, #effective_permissions

Constructor Details

#initialize(pathname, cached = false) ⇒ Document

Returns a new instance of Document.



50
51
52
53
# File 'lib/garage/docs/document.rb', line 50

def initialize(pathname, cached = false)
  @pathname = pathname
  @cached = cached
end

Instance Attribute Details

#cachedObject

Returns the value of attribute cached.



48
49
50
# File 'lib/garage/docs/document.rb', line 48

def cached
  @cached
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



47
48
49
# File 'lib/garage/docs/document.rb', line 47

def pathname
  @pathname
end

Class Method Details

.allObject



5
6
7
# File 'lib/garage/docs/document.rb', line 5

def all
  application.documents
end

.build_permissions(perms, other, target) ⇒ Object



29
30
31
# File 'lib/garage/docs/document.rb', line 29

def build_permissions(perms, other, target)
  perms.permits! :read
end

.find_by_name(name) ⇒ Object



9
10
11
# File 'lib/garage/docs/document.rb', line 9

def find_by_name(name)
  all.find {|document| document.name == name }
end

.rendererObject



13
14
15
16
17
18
19
20
# File 'lib/garage/docs/document.rb', line 13

def renderer
  @renderer ||= Redcarpet::Markdown.new(
    Garage::Docs::Renderer.new(with_toc_data: true),
    fenced_code_blocks: true,
    no_intra_emphasis: true,
    tables: true,
  )
end

.toc_rendererObject



22
23
24
25
26
27
# File 'lib/garage/docs/document.rb', line 22

def toc_renderer
  @toc_renderer ||= Redcarpet::Markdown.new(
    Garage::Docs::TocRenderer.new,
    no_intra_emphasis: true
  )
end

Instance Method Details

#bodyObject



89
90
91
# File 'lib/garage/docs/document.rb', line 89

def body
  pathname.read
end

#cache_key(type) ⇒ Object



63
64
65
# File 'lib/garage/docs/document.rb', line 63

def cache_key(type)
  "garage-doc-#{type}-#{pathname}"
end

#examples(*args) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/garage/docs/document.rb', line 99

def examples(*args)
  if resource_class && resource_class.respond_to?(:garage_examples)
    resource_class.garage_examples(*args)
  else
    []
  end
end

#humanized_nameObject



59
60
61
# File 'lib/garage/docs/document.rb', line 59

def humanized_name
  name.split('-').map(&:humanize).join(' / ')
end

#nameObject



55
56
57
# File 'lib/garage/docs/document.rb', line 55

def name
  relative_base_name.to_s.gsub('/', '-')
end

#renderObject Also known as: rendered_body



77
78
79
80
81
82
83
84
85
# File 'lib/garage/docs/document.rb', line 77

def render
  if cached
    Rails.cache.fetch(cache_key(:render)) do
      self.class.renderer.render(body).html_safe
    end
  else
    self.class.renderer.render(body).html_safe
  end
end

#resource_classObject



93
94
95
96
97
# File 'lib/garage/docs/document.rb', line 93

def resource_class
  @resource_class ||= extract_resource_class || relative_base_name.camelize.singularize.constantize
rescue NameError
  nil
end

#tocObject



67
68
69
70
71
72
73
74
75
# File 'lib/garage/docs/document.rb', line 67

def toc
  if cached
    Rails.cache.fetch(cache_key(:toc)) do
      self.class.toc_renderer.render(body).html_safe
    end
  else
    self.class.toc_renderer.render(body).html_safe
  end
end

#visible_to?(user) ⇒ Boolean

If you need authentication logic, assign a Proc to Garage.docs.configuration.docs_authorization_method.

Example:

Garage.docs.configuration.docs_authorization_method do |args|
  if name.start_with?("admin_")
    args[:user].admin?
  else
    true
  end
end

Returns:

  • (Boolean)


120
121
122
123
124
125
126
# File 'lib/garage/docs/document.rb', line 120

def visible_to?(user)
  if method = Garage.configuration.docs.docs_authorization_method
    method.call(document: self, user: user)
  else
    true
  end
end