Class: Ratchet::ToggleNavHelper::ToggleNav

Inherits:
RatchetDesign::Helper show all
Defined in:
app/helpers/ratchet/toggle_nav_helper.rb

Direct Known Subclasses

TogglePanel, 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/ratchet/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



19
20
21
22
23
24
# File 'app/helpers/ratchet/toggle_nav_helper.rb', line 19

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

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

#display_tag(tag, options, body) ⇒ Object



26
27
28
29
# File 'app/helpers/ratchet/toggle_nav_helper.rb', line 26

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

#extract_block(&block) ⇒ Object



31
32
33
# File 'app/helpers/ratchet/toggle_nav_helper.rb', line 31

def extract_block(&block)
  capture(&block).html_safe
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/ratchet/toggle_nav_helper.rb', line 35

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-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 extract_block(&block) if block_given?
    concat label_text
  }
end

#set_toggle_options(options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/ratchet/toggle_nav_helper.rb', line 62

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