Skip to content

Commit

Permalink
Release : v0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumj authored Feb 14, 2019
2 parents 8017f6b + 3f35bd0 commit 6921c89
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
5 changes: 0 additions & 5 deletions lib/assets/javascripts/nested_form_fields.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ nested_form_fields.bind_nested_forms_links = () ->
# look for replacements in user defined code and substitute with the index
template_html = template_html.replace(new RegExp("__nested_field_for_replace_with_index__","g"), added_index)

# replace child template div tags with script tags to avoid form submission of templates
$parsed_template = $(template_html)
$child_templates = $parsed_template.closestChild('.form_template')
$child_templates.each () ->
$child = $(this)
$child.replaceWith($("<script id='#{$child.attr('id')}' type='text/html' />").html($child.html()))

if target?
$('#' + target).append($parsed_template)
Expand Down
32 changes: 13 additions & 19 deletions lib/nested_form_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def nested_fields_for(record_name, record_object = nil, fields_options = {}, &bl
fields_options[:wrapper_tag] ||= :fieldset
fields_options[:wrapper_options] ||= {}
fields_options[:namespace] = fields_options[:parent_builder].options[:namespace]
fields_options[:abstract] ||= false

return fields_for_has_many_association_with_template(record_name, record_object, fields_options, block)
end
Expand Down Expand Up @@ -49,7 +50,7 @@ def remove_nested_fields_link text = nil, html_options = {}, &block
args << ''
args << { class: "#{html_class.empty? ? '' : html_class} remove_nested_fields_link",
data: { delete_association_field_name: delete_association_field_name,
object_class: @object.class.name.underscore.downcase }.merge(html_data)
object_class: options[:abstract] ? @object.to_s.to_sym : @object.class.name.underscore.downcase }.merge(html_data)
}.merge(html_options)

@template.link_to *args, &block
Expand All @@ -59,7 +60,7 @@ def remove_nested_fields_link text = nil, html_options = {}, &block
private

def fields_for_has_many_association_with_template(association_name, association, options, block)
name = "#{object_name}[#{association_name}_attributes]"
name = "#{object_name}[#{association_name}#{options[:suffix] || "_attributes"}]"
association = convert_to_model(association)

if association.respond_to?(:persisted?)
Expand Down Expand Up @@ -87,24 +88,17 @@ def fields_for_has_many_association_with_template(association_name, association,


def nested_model_template name, association_name, options, block
for_template = self.options[:for_template]

# Render the outermost template in a script tag to avoid it from being submited with the form
# Render all deeper nested templates as hidden divs as nesting script tags messes up the html.
# When nested fields are added with javascript by using a template that contains nested templates,
# the outermost nested templates div's are replaced by script tags to prevent those nested templates
# fields from form subission.
#
@template.content_tag( for_template ? :div : :script,
type: for_template ? nil : 'text/html',
id: template_id(association_name),
class: for_template ? 'form_template' : nil,
style: for_template ? 'display:none' : nil ) do
@template.content_tag( :template, id: template_id(association_name)) do
nested_fields_wrapper(association_name, options[:wrapper_tag], options[:legend], options[:wrapper_options]) do
association_class = (options[:class_name] || object.public_send(association_name).klass.name).to_s.classify.constantize
if options[:abstract]
association_class = Struct.new(:to_s, :persisted?).new(association_name, false)
options[:include_id] = false
else
association_class = (options[:class_name] || object.public_send(association_name).klass.name).to_s.classify.constantize.new
end
fields_for_nested_model("#{name}[#{index_placeholder(association_name)}]",
association_class.new,
options.merge(for_template: true), block)
association_class,
options, block)
end
end
end
Expand All @@ -114,7 +108,7 @@ def template_id association_name
end

def association_path association_name
"#{object_name.gsub('][','_').gsub(/_attributes/,'').sub('[','_').sub(']','')}_#{association_name}"
options[:abstract] ? "#{object_name}_#{association_name}" : "#{object_name.gsub('][','_').gsub(/_attributes/,'').sub('[','_').sub(']','')}_#{association_name}"
end

def index_placeholder association_name
Expand Down
2 changes: 1 addition & 1 deletion lib/nested_form_fields/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module NestedFormFields
VERSION = "0.7.8"
VERSION = "0.8.0"
end

0 comments on commit 6921c89

Please sign in to comment.