Created
January 8, 2013 21:31
-
-
Save jkeck/4488169 to your computer and use it in GitHub Desktop.
This would live in the lib directory of a gem. That gem would also provide some JS and some sort of controller logic about how to handle the data / form fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FormExt | |
module FormHelper | |
def self.included(arg) | |
ActionView::Helpers::FormBuilder.send(:include, FormExt::FormBuilder) | |
end | |
def multi_valued_text_field_tag(object_name, method, options = {}) | |
output = '' | |
if options and options[:value] and options[:value].is_a?(Array) | |
options.merge!(:"data-multivalued" => true) | |
options[:value].each_with_index do |value, index| | |
options.merge!(:name => "#{object_name}[#{method}][]") | |
options.merge!(:id => "#{object_name}_#{method}_#{index}") | |
output.concat text_field(object_name, method, options.merge(:value => value)) | |
output.concat "<input type='button' value='Delete' name='#{object_name}_#{method}_#{index}' data-delete-multival='true' />" unless index == 0 | |
output.concat options[:delimiter] if options[:delimiter] | |
end | |
else | |
output.concat text_field(object_name, method, options) | |
end | |
output.concat "<br/><input type='submit' value='Add #{method}' name='add_#{object_name}_#{method}' data-add-multival='true' />" unless options[:from_form_builder] | |
output.html_safe | |
end | |
end | |
module FormBuilder | |
def multi_valued_text_field(method, options = {}) | |
output = '' | |
object = object_from_options(options) | |
merge_data_attribute(object, method, options) | |
if options[:"data-multivalued"] | |
values = object.send(method) | |
values = [values] unless values.is_a?(Array) | |
values.each_with_index do |value, index| | |
options.merge!(:name => "#{object_name}[#{method}][]") | |
options.merge!(:id => "#{object_name}_#{method}_#{index}") | |
output.concat @template.multi_valued_text_field_tag(@object_name, method, objectify_options(options).merge(:value => value, :from_form_builder => true)) | |
output.concat "<input type='submit' value='Delete' name='delete_#{object_name}_#{method}_#{index}' data-delete-multival='true' />" unless index == 0 | |
output.concat options[:delimiter] if options[:delimiter] | |
end | |
else | |
output.concat @template.multi_valued_text_field_tag(@object_name, method, objectify_options(options).merge(:from_form_builder => true)) | |
end | |
output.concat "<br/><input type='submit' value='Add #{method}' name='add_#{object_name}_#{method}' data-add-multival='true' />" | |
output.html_safe | |
end | |
private | |
def object_from_options(options) | |
objectified_options = objectify_options(options) | |
return objectified_options[:object] unless objectified_options[:object].blank? | |
end | |
def merge_data_attribute(object, method, options = {}) | |
if object# and object.send(method).is_a?(Array) | |
options.merge!(:"data-multivalued" => true) | |
end | |
end | |
end | |
end | |
ActionView::Helpers::FormHelper.send(:include, FormExt::FormHelper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment