Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Includes:
AssetTagHelper, CaptureHelper, FormOptionsHelper, FormTagHelper, TagHelper
Defined in:
lib/slide/selector.rb

Instance Method Summary collapse

Instance Method Details

#slide_selector(selectors, ranges, select_options, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slide/selector.rb', line 12

def slide_selector(selectors, ranges, select_options, options = {})
  options[:suggestions] = false if options[:suggestions].nil?
  options[:slider_step] = 1 if options[:slider_step].nil?
  options[:selector] = true if options[:selector].nil?
  options[:type] = options[:type] == 'amount' ? '$' : ''
  selectors = selectors.map &:to_s
  get_select_field = lambda { |selector, select_type, value = nil|
    result = self.hidden_field(selector)
    if result.include?('value') || value.present?
      result = Hash.from_xml(result)['input']
      selected = value.present? ? {value => value} : {result['value'] => result['value']}
      return self.select(selector, options_for_select(select_options.merge(selected), selected.to_a.last))
    else
      selected = select_type == 'min' ? select_options.first : select_options.to_a.last
      return self.select(selector, options_for_select(select_options, selected))
    end
  }
  "<div class='#{options[:class]}' data-slide-selector=true data-options='{\"suggestions\": #{options[:suggestions]}, \"selector\": #{options[:selector]}, \"type\": \"#{options[:type]}\"}' >".html_safe+
  (if options[:selector]
    get_select_field.call(selectors[0], 'min', (options[:value][0] if options[:value].present?))
  elsif options[:value].present?
    self.text_field(selectors[0], value: options[:value][0])
  else
    self.text_field(selectors[0])
  end)+
  text_field_tag(selectors[0]+'_'+selectors[1], nil, data: {'slider-step': options[:slider_step], 'slide-range': ranges.map(&:to_s)})+
  (if options[:selector]
    get_select_field.call(selectors[1], 'max', (options[:value][1] if options[:value].present?))
  elsif options[:value].present?
    self.text_field(selectors[1], value: options[:value][1])
  else
    self.text_field(selectors[1])
  end)+
  "</div>".html_safe
end