diff --git a/README.md b/README.md index 5403946..e0ea43f 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ You can optionally also supply: *:object =>* defaults to creating a new instance of an object using the camel-cased name of the attribute i.e. above that would be ProductVariation.new +*:after_insertion =>* Javascript to be run just after the insertion has been made + remove_nested_fields_for(form_builder, dom_class) ------------------------------------------------ remove_nested_fields_for adds a link to a javascript function that if new, removes the partial, and if existing sets a hidden field _marking it's removal _delete=1, informing rails to delete the record and hides the partial. diff --git a/add_nested_fields.gemspec b/add_nested_fields.gemspec index 78eb8a1..d16208f 100644 --- a/add_nested_fields.gemspec +++ b/add_nested_fields.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = %q{add_nested_fields} - s.version = "0.1.0" + s.version = "0.1.1" s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= s.authors = ["Jon Baker. Go Tripod Ltd"] diff --git a/lib/add_nested_fields.rb b/lib/add_nested_fields.rb index b642da7..5ff2f82 100644 --- a/lib/add_nested_fields.rb +++ b/lib/add_nested_fields.rb @@ -5,7 +5,7 @@ def add_nested_fields_for(form_builder, field, dom_id, *args) options = { :partial => field.to_s.singularize, :label => I18n.t('add_nested_fields.add') + ' ' + \ - I18n.t(field, :scope => [:activerecord, :attributes, form_builder.object.class.to_s.underscore.downcase]).downcase, + I18n.t(field, :scope => [:activerecord, :attributes, form_builder.object.class.to_s.underscore.downcase]).downcase, :object => field.to_s.classify.constantize.new }.merge(args.extract_options!) @@ -13,6 +13,7 @@ def add_nested_fields_for(form_builder, field, dom_id, *args) form_builder.fields_for field, options[:object] , :child_index => 'NEW_RECORD' do |f| html = render(:partial => options[:partial], :locals => { :f => f }) page << "$('#{dom_id}').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });" + page << options[:after_insertion] unless options[:after_insertion].blank? end end end @@ -31,7 +32,7 @@ def remove_nested_fields_for(form_builder, class_id, *args) page << "#{confirm} $(this).up('.#{class_id}').remove()" end else - form_builder.hidden_field( :_delete, :value => "0") + link_to_function(options[:label]) do |page| + form_builder.hidden_field( :_destroy, :value => "0") + link_to_function(options[:label]) do |page| page << "#{confirm} $(this).up('.#{class_id}').hide();$(this).previous().value = 1" end end