diff --git a/lib/cocoon.rb b/lib/cocoon.rb index 3118fb7c..8c9cc66c 100644 --- a/lib/cocoon.rb +++ b/lib/cocoon.rb @@ -1,4 +1,5 @@ require 'cocoon/view_helpers' +require 'cocoon/cells_helpers' module Cocoon class Engine < ::Rails::Engine diff --git a/lib/cocoon/cells_helpers.rb b/lib/cocoon/cells_helpers.rb new file mode 100644 index 00000000..c516c16b --- /dev/null +++ b/lib/cocoon/cells_helpers.rb @@ -0,0 +1,16 @@ +module Cocoon + module CellsHelpers + include ViewHelpers + + # :nodoc: + def render_partial(partial, partial_options) + render(view: partial, locals: partial_options) + end + + # :nodoc: + def insertion_template(association, f, new_object, form_parameter_name, render_options, override_partial) + CGI.unescapeHTML(render_association(association, f, new_object, form_parameter_name, render_options, override_partial).to_str).html_safe + end + + end +end diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 91f7153a..07fec6fe 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -49,10 +49,20 @@ def render_association(association, f, new_object, form_name, render_options={}, method_name = ancestors.include?('SimpleForm::FormBuilder') ? :simple_fields_for : (ancestors.include?('Formtastic::FormBuilder') ? :semantic_fields_for : :fields_for) f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder| partial_options = {form_name.to_sym => builder, :dynamic => true}.merge(locals) - render(partial, partial_options) + render_partial(partial, partial_options) end end + # :nodoc: + def render_partial(partial, partial_options) + render(partial, partial_options) + end + + # :nodoc: + def insertion_template(association, f, new_object, form_parameter_name, render_options, override_partial) + CGI.escapeHTML(render_association(association, f, new_object, form_parameter_name, render_options, override_partial).to_str).html_safe + end + # shows a link that will allow to dynamically add a new associated object. # # - *name* : the text to show in the link @@ -95,7 +105,7 @@ def link_to_add_association(*args, &block) new_object = create_object(f, association, force_non_association_create) new_object = wrap_object.call(new_object) if wrap_object.respond_to?(:call) - html_options[:'data-association-insertion-template'] = CGI.escapeHTML(render_association(association, f, new_object, form_parameter_name, render_options, override_partial).to_str).html_safe + html_options[:'data-association-insertion-template'] = insertion_template(association, f, new_object, form_parameter_name, render_options, override_partial) html_options[:'data-count'] = count if count > 0