Class: Tolaria::HelpLink

Inherits:
Object
  • Object
show all
Defined in:
lib/tolaria/help_links.rb

Overview

Class representing a configured Tolaria help link.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, slug: nil, markdown_file: nil, link_to: nil) ⇒ HelpLink

Create a new HelpLink with the passed settings. You must provide title, the title of the link. To configure automatic rendering of a Markdown file, provide a string slug and the path to a markdown_file. A route to view the file will be constructed for you at /admin/help/:slug. To link to an arbitrary path or URI, provide it as link_to.



38
39
40
41
42
43
44
# File 'lib/tolaria/help_links.rb', line 38

def initialize(title:, slug:nil, markdown_file:nil, link_to:nil)
  @title = title.to_s.freeze
  @slug = slug.to_s.freeze
  @markdown_file = markdown_file.to_s.freeze
  @link_to = link_to.to_s.freeze
  validate!
end

Instance Attribute Details

The path to link to when not rendering a Markdown file



30
31
32
# File 'lib/tolaria/help_links.rb', line 30

def link_to
  @link_to
end

#markdown_fileObject (readonly)

The file path to the Markdown file



28
29
30
# File 'lib/tolaria/help_links.rb', line 28

def markdown_file
  @markdown_file
end

#slugObject (readonly)

Part part of the link at /admin/help/:slug when rendering a Markdown file



26
27
28
# File 'lib/tolaria/help_links.rb', line 26

def slug
  @slug
end

#titleObject (readonly)

The title of the link



24
25
26
# File 'lib/tolaria/help_links.rb', line 24

def title
  @title
end

Instance Method Details

True if this HelpLink is a link to an arbitrary path.

Returns:

  • (Boolean)


47
48
49
# File 'lib/tolaria/help_links.rb', line 47

def link_type?
  link_to.present?
end

#markdown_type?Boolean

True if this HelpLink is a Markdown file.

Returns:

  • (Boolean)


52
53
54
# File 'lib/tolaria/help_links.rb', line 52

def markdown_type?
  markdown_file.present?
end

#to_paramObject

Quack like an ActiveRecord::Base model, returns slug.



57
58
59
# File 'lib/tolaria/help_links.rb', line 57

def to_param
  slug
end

#validate!Object

Raises RuntimeError if this HelpLink is incorrectly configured.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tolaria/help_links.rb', line 62

def validate!

  if title.blank?
    raise RuntimeError, "HelpLinks must provide a string title"
  end

  file_configured = (slug.present? && markdown_file.present?)
  link_configured = link_to.present?

  unless file_configured || link_configured
    raise RuntimeError, "Incomplete HelpLink config. You must provide link_to, or both slug and markdown_file."
  end

  if file_configured && link_configured
    raise RuntimeError, "Ambiguous HelpLink config. You must provide link_to, or both slug and markdown_file, but not all three."
  end

end