Module: VisitCard::Helpers::VcardsHelper::FormBuilder

Defined in:
lib/visit_card/helpers/vcards_helper.rb

Instance Method Summary collapse

Instance Method Details

#check_box_collection(method, collection, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/visit_card/helpers/vcards_helper.rb', line 13

def check_box_collection(method, collection, options = {})
  output = "<fieldset class='#{method.to_s.downcase}'><legend>#{method.to_s.humanize}</legend>"
  if collection.is_a? Array # array collection - hbtm through bit mask
    collection.each do |value|
      options.delete(:id)
      output << check_box_collection_check_box(method, value, options) << "\n"
      output << label(method, value.humanize, :value => value.downcase) << "\n"
    end
    output << check_box_collection_hidden_field(method)
  else # hash collection - hbtm through join table
    collection.each do |key, value|
      options[:id] = "#{object_name}_#{method}_#{value}"
      output << check_box_collection_check_box(method, key, options) << "\n"
      output << label(method, value.humanize, :value => value.downcase) << "\n"
    end
  end
  output << '</fieldset>'
  output.html_safe
end

#check_box_collection_check_box(method, value, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/visit_card/helpers/vcards_helper.rb', line 33

def check_box_collection_check_box(method, value, options = {})
  if options[:id] # hbtm through join table
    name = "#{object_name}[#{method.to_s.singularize}_ids][]"
    checked = object.send(:"#{method.to_s.singularize}_ids").include?(value)
  else # hbtm through bit mask
    name = "#{object_name}[#{method}][]"
    checked = object.send("#{method}?", value)
  end
  ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).
                                   to_check_box_collection_check_box_tag(name, value, checked, options)
end

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



45
46
47
48
# File 'lib/visit_card/helpers/vcards_helper.rb', line 45

def check_box_collection_hidden_field(method, options = {})
  options[:name] ||= "#{object_name}[#{method}][]"
  ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("hidden", options)
end