Module: JqueryUiForm::Inputs::CheckBoxesInput

Included in:
FormBuilder
Defined in:
lib/jquery_ui_form/inputs/check_boxes_input.rb

Instance Method Summary collapse

Instance Method Details

#check_boxes_input(method, options = {}) ⇒ Object



5
6
7
8
9
10
11
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
# File 'lib/jquery_ui_form/inputs/check_boxes_input.rb', line 5

def check_boxes_input(method, options = {})
  # radio_button(method, tag_value, options)
  legend, options = label_text(method, options)
  current_values = options.delete(:values) || current_values_from_association(method)
  collection = options.delete(:collection) || collection_from_association(method)
  label_options = options.delete(:html_label) || {}
  
  options[:class] = options[:class].gsub('ui-check_boxes-input','ui-boolean-input')
  buttonset = options.delete(:buttonset) || false
  
  fieldset_options = {}
  fieldset_options[:class] = "to-buttonset" if buttonset
  
  
  label_method = options.delete(:label_method) || :name
  value_method = options.delete(:value_method) || :id
  
  output = fieldset(legend.html_safe, fieldset_options) do
    template.concat(inline_hint(options.delete(:hint)))
    template.concat(inline_error(method))
    collection.each do |row|
      label_text = label_from_association(row,label_method)
      value_text = value_from_association(row,value_method)
      checked = current_values.include?(value_text.to_s)
      if buttonset
        template.concat(template.check_box_tag("#{@object_name}[#{method}][]", value_text, checked, options.merge(:id => name_to_id("#{@object_name}_#{method}_#{value_text}"))))
        template.concat(label(name_to_id("#{method}_#{value_text}"), label_text, label_options))
      else
        template.concat(column do
          template.concat(label(name_to_id("#{method}_#{value_text}"), label_text, label_options))
          template.concat(template.check_box_tag("#{@object_name}[#{method}][]", value_text, checked, options.merge(:id => name_to_id("#{@object_name}_#{method}_#{value_text}"))))
        end)
      end
    end
  end
end

#collection_from_association(method) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jquery_ui_form/inputs/check_boxes_input.rb', line 69

def collection_from_association(method)
  return [] unless @object || @object.respond_to?(method)
  return [] unless @object.send(method).is_a?(Array)
  if target = @object.send(method).first
    target.class.all
  else
    begin 
      collection = method.to_s.classify.constantize 
      collection.all
    rescue 
      return []
    end
  end
end

#current_values_from_association(method) ⇒ Object



63
64
65
66
67
# File 'lib/jquery_ui_form/inputs/check_boxes_input.rb', line 63

def current_values_from_association(method)
  return [] unless @object || @object.respond_to?(method)
  return [] unless @object.send(method).is_a?(Array)
  @object.send(method).map{|row| row.id.to_s}
end

#label_from_association(row, label_method) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/jquery_ui_form/inputs/check_boxes_input.rb', line 42

def label_from_association(row, label_method)
  return row[0] if row.is_a?(Array)
  return row if row.is_a?(String)
  if row.respond_to?(label_method)
    row.send(label_method)
  else
    row
  end
  
end

#value_from_association(row, value_method) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/jquery_ui_form/inputs/check_boxes_input.rb', line 53

def value_from_association(row, value_method)
  return row[1] if row.is_a?(Array)
  return row if row.is_a?(String)
  if row.respond_to?(value_method)
    row.send(value_method)
  else
    row
  end
end