Class: Helmsman::Helm

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/helmsman/helm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Helm

Returns a new instance of Helm.



9
10
11
12
13
14
15
16
17
# File 'lib/helmsman/helm.rb', line 9

def initialize(options = {})
  @disabled   = options.fetch(:disabled) { false }
  @visible    = options.fetch(:visible)  { true }
  @current    = options.fetch(:current)  { false }
  @li_class   = options[:li_class] || []
  @i18n_scope = options.fetch(:i18n_scope)
  @i18n_key   = options.fetch(:i18n_key)
  @url        = options[:url]
end

Instance Attribute Details

#additionalObject

Returns the value of attribute additional.



3
4
5
# File 'lib/helmsman/helm.rb', line 3

def additional
  @additional
end

#i18n_keyObject

Returns the value of attribute i18n_key.



3
4
5
# File 'lib/helmsman/helm.rb', line 3

def i18n_key
  @i18n_key
end

#i18n_scopeObject

Returns the value of attribute i18n_scope.



3
4
5
# File 'lib/helmsman/helm.rb', line 3

def i18n_scope
  @i18n_scope
end

#nameObject



69
70
71
# File 'lib/helmsman/helm.rb', line 69

def name
  @name || I18n.translate("#{i18n_scope}#{i18n_key}").html_safe
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/helmsman/helm.rb', line 3

def url
  @url
end

Instance Method Details

#configure_li_classObject



36
37
38
39
40
41
42
43
44
# File 'lib/helmsman/helm.rb', line 36

def configure_li_class
  if @li_class.respond_to?(:to_ary)
    @li_class.to_ary
  else
    @li_class.to_s.split(/\s+/)
  end.tap do |li_class|
    yield li_class
  end.select(&:present?).uniq.sort * ' '
end

#current?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/helmsman/helm.rb', line 104

def current?
  @current
end

#default_disabled_tooltip_translation_keyObject



88
89
90
# File 'lib/helmsman/helm.rb', line 88

def default_disabled_tooltip_translation_key
  "#{i18n_scope}disabled_tooltip"
end

#disabled?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/helmsman/helm.rb', line 92

def disabled?
  @disabled
end

#disabled_titleObject



73
74
75
76
77
78
# File 'lib/helmsman/helm.rb', line 73

def disabled_title
  I18n.translate(
    disabled_tooltip_translation_key,
    default: I18n.translate(default_disabled_tooltip_translation_key)
  ).html_safe
end

#disabled_tooltip_translation_keyObject



84
85
86
# File 'lib/helmsman/helm.rb', line 84

def disabled_tooltip_translation_key
  "#{i18n_scope}#{i18n_key}_disabled_tooltip"
end

#enabled?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/helmsman/helm.rb', line 100

def enabled?
  !@disabled
end

#html_safe?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/helmsman/helm.rb', line 23

def html_safe?
  true
end

#li_contentObject



31
32
33
34
# File 'lib/helmsman/helm.rb', line 31

def li_content
  link = link_to_if(enabled?, name, url) if url
  disabled? ? link : "#{link}#{additional}".html_safe
end

#li_optionsObject



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

def li_options
  if enabled?
    li_class = configure_li_class do |li_class|
      if current?
        li_class << Helmsman.current_css_class
      end
    end
    {
      class: li_class
    }
  else
    li_class = configure_li_class do |li_class|
      li_class << Helmsman.disabled_css_class
    end
    {
      rel:   'tooltip',
      title: disabled_title,
      class: li_class,
      data:  { placement: 'bottom' }
    }
  end
end

#name_translation_keyObject



80
81
82
# File 'lib/helmsman/helm.rb', line 80

def name_translation_key
  "#{i18n_scope}#{i18n_key}"
end

#to_sObject



19
20
21
# File 'lib/helmsman/helm.rb', line 19

def to_s
  visible? ? (:li, li_content, li_options) : ''.html_safe
end

#to_strObject



27
28
29
# File 'lib/helmsman/helm.rb', line 27

def to_str
  to_s
end

#visible?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/helmsman/helm.rb', line 96

def visible?
  @visible
end