From 4079f69dc37e6c9ec93422da8bdeb92b05f1ebeb Mon Sep 17 00:00:00 2001 From: tmaeda Date: Fri, 7 Mar 2014 16:33:26 +0900 Subject: [PATCH 1/3] cosmetic --- lib/rails/form_helper.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/rails/form_helper.rb b/lib/rails/form_helper.rb index bd7db39..311c18b 100644 --- a/lib/rails/form_helper.rb +++ b/lib/rails/form_helper.rb @@ -1,8 +1,8 @@ ActionView::Helpers::FormBuilder.class_eval do - + def nested_fields_for(association, options={}, &block) raise ArgumentError, 'Missing block to nested_fields_for' unless block_given? - + options[:new_item_index] ||= 'new_nested_item' options[:new_object] ||= self.object.class.reflect_on_association(association).klass.new options[:item_template_class] ||= ['template', 'item', association.to_s.singularize].join(' ') @@ -10,33 +10,33 @@ def nested_fields_for(association, options={}, &block) options[:show_empty] ||= false options[:render_template] = options.key?(:render_template) ? options[:render_template] : true options[:escape_template] = options.key?(:escape_template) ? options[:escape_template] : true - + output = @template.capture { fields_for(association, &block) } output ||= template.raw "" - + if options[:show_empty] and self.object.send(association).empty? - output.safe_concat @template.capture { yield nil } + output.safe_concat @template.capture { yield nil } end - + template = render_nested_fields_template(association, options, &block) if options[:render_template] output.safe_concat template else add_nested_fields_template(association, template) end - + output end protected - + def render_nested_fields_template(association, options, &block) templates = @template.content_tag(:script, :type => 'text/html', :class => options[:item_template_class]) do template = fields_for(association, options[:new_object], :child_index => options[:new_item_index], &block) template = AwesomeNestedFields.escape_html_tags(template) if options[:escape_template] template end - + if options[:show_empty] empty_template = @template.content_tag(:script, :type => 'text/html', :class => options[:empty_template_class]) do template = @template.capture { yield nil } @@ -45,17 +45,17 @@ def render_nested_fields_template(association, options, &block) end templates.safe_concat empty_template end - + templates end - + def add_nested_fields_template(association, template) # It must be a hash, so we don't get repeated templates on deeply nested models @template.instance_variable_set(:@nested_fields_template_cache, {}) unless @template.instance_variable_get(:@nested_fields_template_cache) @template.instance_variable_get(:@nested_fields_template_cache)[association] = template create_nested_fields_template_helper! end - + def create_nested_fields_template_helper! def @template.nested_fields_templates @nested_fields_template_cache.reduce(ActiveSupport::SafeBuffer.new) do |buffer, entry| @@ -64,5 +64,5 @@ def @template.nested_fields_templates end end unless @template.respond_to?(:nested_fields_templates) end - + end From f98a667572b3fe17c64ac41fde66898d0c5b4e8b Mon Sep 17 00:00:00 2001 From: tmaeda Date: Fri, 7 Mar 2014 16:34:52 +0900 Subject: [PATCH 2/3] Change instantiation method of child object to be connected to the parent. --- lib/rails/form_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails/form_helper.rb b/lib/rails/form_helper.rb index 311c18b..ef84eec 100644 --- a/lib/rails/form_helper.rb +++ b/lib/rails/form_helper.rb @@ -4,7 +4,7 @@ def nested_fields_for(association, options={}, &block) raise ArgumentError, 'Missing block to nested_fields_for' unless block_given? options[:new_item_index] ||= 'new_nested_item' - options[:new_object] ||= self.object.class.reflect_on_association(association).klass.new + options[:new_object] ||= self.object.send(association).build options[:item_template_class] ||= ['template', 'item', association.to_s.singularize].join(' ') options[:empty_template_class] ||= ['template', 'empty', association.to_s.singularize].join(' ') options[:show_empty] ||= false From 1abe15b660a56e46cfbc574db0d397b46e243db5 Mon Sep 17 00:00:00 2001 From: tmaeda Date: Mon, 10 Mar 2014 14:16:45 +0900 Subject: [PATCH 3/3] Fix bug an unwanted child object is added to parent. --- lib/rails/form_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/rails/form_helper.rb b/lib/rails/form_helper.rb index ef84eec..7efefa4 100644 --- a/lib/rails/form_helper.rb +++ b/lib/rails/form_helper.rb @@ -4,7 +4,10 @@ def nested_fields_for(association, options={}, &block) raise ArgumentError, 'Missing block to nested_fields_for' unless block_given? options[:new_item_index] ||= 'new_nested_item' - options[:new_object] ||= self.object.send(association).build + unless options[:new_object] + options[:new_object] = self.object.send(association).build + self.object.send(association).destroy(options[:new_object]) + end options[:item_template_class] ||= ['template', 'item', association.to_s.singularize].join(' ') options[:empty_template_class] ||= ['template', 'empty', association.to_s.singularize].join(' ') options[:show_empty] ||= false