Class: Tungsten::ToggleNavHelper::ToggleNav

Inherits:
Helper
  • Object
show all
Defined in:
app/helpers/tungsten/toggle_nav_helper.rb

Direct Known Subclasses

ToggleRow

Instance Method Summary collapse

Constructor Details

#initialize(tag = :div, options = {}) ⇒ ToggleNav

Returns a new instance of ToggleNav.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/tungsten/toggle_nav_helper.rb', line 5

def initialize(tag = :div, options = {})

  if tag.is_a? Hash
    options = tag
    tag = :div
  end

  options[:tag] = tag

  @options = options
  @options[:name] ||= "radio-group-#{rand(10000)}-#{rand(10000)}"
  @count = 0
end

Instance Method Details

#display(body) ⇒ Object



31
32
33
34
35
36
# File 'app/helpers/tungsten/toggle_nav_helper.rb', line 31

def display(body)
  options = @options
  options.delete(:name)

  display_tag options.delete(:tag), options, body 
end

#display_tag(tag, options, body) ⇒ Object



38
39
40
41
42
# File 'app/helpers/tungsten/toggle_nav_helper.rb', line 38

def display_tag(tag, options, body )
  options[:class] = "toggle-nav #{@options[:class]}"
  options[:class] << " #{options[:type]}" if options[:type]
  (tag, options) { body }
end

#option(label = nil, options = {}, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/tungsten/toggle_nav_helper.rb', line 44

def option(label = nil, options = {}, &block)

  if label.is_a? Hash
    options = label
    label = options.delete(:label)
  end

  checked = options.delete(:checked) || false
  name = options.delete(:name) || @options[:name]
  label_text = (:span, class: 'label-text') { label } if label

  label_class = "#{options.delete(:class)} toggle-nav-label"
  label_id = "#{name}_#{@count += 1}"

  # The label should have an active class
  # when the radio is active
  options[:add_class] = "active; ##{label_id}& #{options[:add_clss]}"

  options = set_toggle_options(options)

  (:label, class: label_class, id: label_id ) {
    concat radio_button_tag(name, true, checked, options)
    concat capture(&block).html_safe if block_given?
    concat label_text
  }
end

#set_toggle_options(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/tungsten/toggle_nav_helper.rb', line 19

def set_toggle_options( options )
  options[:data] ||= {}
  options[:data] = options[:data].merge({
    show: options.delete(:show),
    hide: options.delete(:hide),
    add_class: options.delete(:add_class),
    remove_class: options.delete(:remove_class)
  })

  options
end