Class: Formtastic::SemanticFormBuilder
- Inherits:
-
Object
- Object
- Formtastic::SemanticFormBuilder
- Defined in:
- lib/dependent_select/semantic_form_builder.rb
Instance Method Summary collapse
-
#dependent_select_input(method, options) ⇒ Object
Options: parent_id - The DOM ID of the parent field to observe url_template - The jQuery-compatible template used to generate the URL parent_method - If provided, parent_id and url_template can be created automatically option_template - The jQuery-compatible template used to generate the select options.
Instance Method Details
#dependent_select_input(method, options) ⇒ Object
Options: parent_id - The DOM ID of the parent field to observe url_template - The jQuery-compatible template used to generate the URL parent_method - If provided, parent_id and url_template can be created automatically option_template - The jQuery-compatible template used to generate the select options
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 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dependent_select/semantic_form_builder.rb', line 9 def dependent_select_input(method, ) html = select_input(method, ) = {}.merge() = .delete(:input_html) || {} input_name = generate_association_input_name(method) [:id] ||= generate_html_id(input_name, "") if [:parent_method] parent_input_name = generate_association_input_name([:parent_method]) [:parent_id] ||= generate_html_id(parent_input_name, "") child_reflection = reflection_for(method) parent_reflection = reflection_for([:parent_method]) if child_reflection && parent_reflection && parent_reflection.macro == :belongs_to [:url_template] ||= DependentSelect.default_url_template .gsub('${resource_name}', child_reflection.class_name.underscore) .gsub('${plural_resource_name}', child_reflection.class_name.underscore.pluralize) .gsub('${parent_resource_name}', parent_reflection.class_name.underscore) .gsub('${plural_parent_resource_name}', parent_reflection.class_name.underscore.pluralize) .gsub('${parent_parameter}', parent_reflection.foreign_key.to_s) end end unless [:parent_id].blank? || [:url_template].blank? # convert to camelcase keys, which is the convention in Javascript = .inject({}) do |hash, pair| hash[pair[0].to_s.camelize(:lower)] = pair[1] hash end html += "<script>$(document).ready(function() { $('##{html_options[:id]}').dependentSelect('#{options[:parent_id]}', '#{options[:url_template]}', #{js_options.to_json}) });</script>".html_safe end return html end |