Module: ComboBox::Helpers::FormTagHelper

Defined in:
lib/combo_box/helpers/form_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#combo_box(object_name, method, choices = nil, options = {}, html_options = {}) ⇒ String

Returns a text field which has the same behavior of select but with a search action which permits to find easily in very long lists…

Parameters:

  • object_name (Symbol)

    Name of the used instance variable

  • method (Symbol)

    Attribute to control

  • choices (Symbol, String, Hash) (defaults to: nil)

    Name of data source like specified in `search_for` or a specific URL in its String form (like `“orders#search_for”`) or in its Hash form

  • options (Hash) (defaults to: {})

    Options to build the control

  • html_options (Hash) (defaults to: {})

    Extra-attributes to add to the tags

Returns:

  • (String)

    HTML code of the tags


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
# File 'lib/combo_box/helpers/form_tag_helper.rb', line 18

def combo_box(object_name, method, choices = nil, options = {}, html_options = {})
  object = instance_variable_get("@#{object_name}")
  if choices.nil?
    choices = {:action=>"search_for_#{method}"}
  elsif choices.is_a?(Symbol)
    choices = {:action=>"search_for_#{choices}"}
  elsif choices.is_a?(String)
    action = choices.split(/\#+/)
    choices = {:action=>action[1], :controller=>action[0]}
  end
  choices[:controller] ||= options.delete(:controller) || controller.controller_name
  # unless ComboBox::CompiledLabels.methods.include?("item_label_for_#{choices[:action]}_in_#{choices[:controller]}".to_sym)
  #   needed_controller = "#{choices[:controller].to_s.classify.pluralize}Controller"
  #   needed_controller.constantize
  #   unless ComboBox::CompiledLabels.methods.include?("item_label_for_#{choices[:action]}_in_#{choices[:controller]}".to_sym)
  #     raise Exception.new("It seems there is no search_for declaration corresponding to #{choices[:action]} in #{needed_controller}")
  #   end
  # end
  html_options[:id] ||= "#{object_name}_#{method}"
  html  = ""
  # ComboBox::CompiledLabels.send("item_label_for_#{choices[:action]}_in_#{choices[:controller]}", object.send(method.to_s.gsub(/_id$/,'')))
  html << tag(:input, :type=>:text, "data-combo-box"=>url_for(choices.merge(:format=>:json)), "data-value-container"=>html_options[:id], :value=>item_label(object.send(method.to_s.gsub(/_id$/,'')), choices[:action], choices[:controller]), :size=>html_options.delete(:size)||32)
  html << hidden_field(object_name, method, html_options)
  return html.html_safe
end

#combo_box_tag(name, choices = nil, options = {}, html_options = {}) ⇒ String

Returns a text field which has the same behavior of select but with a search action which permits to find easily in very long lists.

Parameters:

  • name (Symbol)

    Name of the field

  • choices (Symbol, String, Hash) (defaults to: nil)

    Name of data source like specified in `search_for` or a specific URL in its String form (like `“orders#search_for”`) or in its Hash form

  • options (Hash) (defaults to: {})

    Options to build the control

  • html_options (Hash) (defaults to: {})

    Extra-attributes to add to the tags

Options Hash (options):

  • label (String)

    Default label to display

Returns:

  • (String)

    HTML code of the tags


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/combo_box/helpers/form_tag_helper.rb', line 57

def combo_box_tag(name, choices = nil, options={}, html_options = {})
  if choices.nil? or (choices == controller_name.to_sym and not options.has_key?(:controller))
    choices = {:action=>"search_for"}
  elsif choices.is_a?(Symbol)
    choices = {:action=>"search_for_#{choices}", :controller=>(options.delete(:controller) || controller.controller_name)}
    if object = options.delete(:value)
      options[:label] = item_label(object, choices[:action], choices[:controller])
      html_options[:value] = object.id
    end
  elsif choices.is_a?(String)
    action = choices.split(/\#+/)
    choices = {:action=>action[1], :controller=>action[0]}
  end
  choices[:controller] ||= options.delete(:controller) || controller.controller_name
  html_options[:id] ||= name.to_s.gsub(/\W+/, '_').gsub(/(^_+|_+$)/, '')
  html  = ""
  html << tag(:input, :type=>:text, "data-combo-box"=>url_for(choices.merge(:format=>:json)), "data-value-container"=>html_options[:id], :size=>html_options.delete(:size)||32, :value=>options.delete(:label))
  html << hidden_field_tag(name, html_options.delete(:value), html_options)
  return html.html_safe
end