lib/formtastic/custom_attributes.rb in custom-attributes-0.2.5 vs lib/formtastic/custom_attributes.rb in custom-attributes-0.2.12

- old
+ new

@@ -34,14 +34,16 @@ def add_custom_attribute_template_for(attribute_type) storage_type = @object.custom_attributes.supported_attribute_types[attribute_type] i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym] + index = 0 value_fields = @object.custom_attributes.attributes_of_type(attribute_type).collect do |attribute| - custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value) + custom_field_input(attribute_type, [attribute_type, storage_type], attribute.label, attribute.value, index, {}) + index += 1 end - field_template = custom_field_input(attribute_type, [attribute_type, storage_type], "", nil, :template => true) + field_template = custom_field_input(attribute_type, [attribute_type, storage_type], "", nil, "%nr%", :template => true) attribute_human_name = ::I18n.t(attribute_type, :count => 1, :scope => i18n_scope + [:attribute_names]).capitalize @@ -49,11 +51,12 @@ template.link_to(Formtastic::Util.html_safe(attribute_human_name), "#add-#{attribute_type}", :class => "add-link", :title => ::I18n.t(:add, :scope => :default_actions, :model => attribute_human_name), :'data-attribute-type' => attribute_type - ) << field_template << label_data_list_for(attribute_type, @object.custom_attributes.defined_labels_for(attribute_type)), + ) << inline_hints_for(attribute_type, {}) << + field_template << label_data_list_for(attribute_type, @object.custom_attributes.defined_labels_for(attribute_type)), :class => "field-addition" ) @supported_attribute_types[attribute_type] = attribute_human_name @supported_attribute_templates << content_tag( @@ -67,14 +70,17 @@ def add_custom_association_attribute_template_for(association_name, options) association = @object.class.reflect_on_association(association_name) storage_type = association.klass.model_name.underscore.to_sym + index = 0 value_fields = @object.send(association_name).collect do |item| - custom_field_input(storage_type, [storage_type], item.name, item) + result = custom_field_input(storage_type, [storage_type], item.name, item, index, {}) + index += 1 + result end - field_template = custom_field_input(storage_type, [storage_type], "", nil, :template => true) + field_template = custom_field_input(association_name, [storage_type], "", nil, "%nr%", :template => true) attribute_human_name = association.klass.model_name.human value_fields << content_tag(:li, template.link_to(Formtastic::Util.html_safe(attribute_human_name), "#add-#{storage_type}", @@ -120,28 +126,32 @@ )), :class => "add-custom-attribute") ) end - def field_name_for(attribute_type, field_type) + def field_name_for(attribute_type, field_type, index) if @object.custom_attributes.supported_attribute_types.keys.include? attribute_type - "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{field_type}][]" + "#{@object.class.model_name.underscore}[custom_attributes][#{attribute_type}][#{index}][#{field_type}]" else - "#{@object.class.model_name.underscore}[#{attribute_type}][#{field_type}][]" + "#{@object.class.model_name.underscore}[#{attribute_type}_attributes][#{index}][#{field_type}]" end end - def custom_field_input(attribute_type, field_method_priority, label, value, options = {}) - label_name = field_name_for attribute_type, "label" + def custom_field_input(attribute_type, field_method_priority, label, value, index, options) + label_name = field_name_for attribute_type, "label", index label_field = template.text_field_tag(label_name, label, :list => "#{attribute_type}-label-suggestions", :autocomplete => "off") input_field_method = field_method_priority.collect { |m| "custom_#{m}_input".to_sym }.find { |m| self.respond_to? m, true } - input_field = send(input_field_method, attribute_type, value, options) + input_field = send(input_field_method, attribute_type, value, index, options) - content_tag(options[:template] ? :div : :li, - Formtastic::Util.html_safe(content_tag(:div, label_field, :class => "label") << - input_field) << inline_hints_for(field_method_priority.first, options), + field_contents = Formtastic::Util.html_safe( + content_tag(:div, label_field, :class => "label") << input_field + ) + field_contents << template.hidden_field_tag(field_name_for(attribute_type, "_destroy", index), + false, :class => "delete_check") unless options[:template] + + content_tag(options[:template] ? :div : :li, field_contents, :class => options[:template] ? "template" : "value optional #{attribute_type}" ) end def custom_attribute_delete_link(attribute_human_name) @@ -149,25 +159,25 @@ escape_html_entities(" ") + template.link_to(escape_html_entities(label), "#remove", :class => "delete-link") end ## Field addition support - def custom_string_input(attribute_type, value, options = {}) - default_custom_field_handler(:text_field_tag, attribute_type, value) + def custom_string_input(attribute_type, value, index, options = {}) + default_custom_field_handler(:text_field_tag, attribute_type, value, index) end - def custom_email_input(attribute_type, value, options = {}) - default_custom_field_handler(:email_field_tag, attribute_type, value) + def custom_email_input(attribute_type, value, index, options = {}) + default_custom_field_handler(:email_field_tag, attribute_type, value, index) end - def custom_url_input(attribute_type, value, options = {}) - default_custom_field_handler(:url_field_tag, attribute_type, value) + def custom_url_input(attribute_type, value, index, options = {}) + default_custom_field_handler(:url_field_tag, attribute_type, value, index) end - def default_custom_field_handler(method, attribute_type, value) + def default_custom_field_handler(method, attribute_type, value, index) i18n_scope = [:activerecord, :custom_attributes, @object.class.model_name.underscore.to_sym] attribute_human_name = ::I18n.t(attribute_type, :count => 1, :scope => i18n_scope + [:attribute_names]).capitalize - template.send(method, field_name_for(attribute_type, "value"), value) << + template.send(method, field_name_for(attribute_type, "value", index), value) << custom_attribute_delete_link(attribute_human_name) end delegate :content_tag, :to => :template \ No newline at end of file