Module: ComboBox::Helpers::FormTagHelper
- Defined in:
- lib/combo_box/helpers/form_tag_helper.rb
Instance Method Summary collapse
-
#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… -
#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.
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…
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, = {}, = {}) 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] ||= .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 [: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"=>[:id], :value=>item_label(object.send(method.to_s.gsub(/_id$/,'')), choices[:action], choices[:controller]), :size=>.delete(:size)||32) html << hidden_field(object_name, method, ) 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.
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, ={}, = {}) if choices.nil? or (choices == controller_name.to_sym and not .has_key?(:controller)) choices = {:action=>"search_for"} elsif choices.is_a?(Symbol) choices = {:action=>"search_for_#{choices}", :controller=>(.delete(:controller) || controller.controller_name)} if object = .delete(:value) [:label] = item_label(object, choices[:action], choices[:controller]) [:value] = object.id end elsif choices.is_a?(String) action = choices.split(/\#+/) choices = {:action=>action[1], :controller=>action[0]} end choices[:controller] ||= .delete(:controller) || controller.controller_name [: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"=>[:id], :size=>.delete(:size)||32, :value=>.delete(:label)) html << hidden_field_tag(name, .delete(:value), ) return html.html_safe end |