Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion add_nested_fields.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 3 additions & 2 deletions lib/add_nested_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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!)

link_to_function("#{options[:label]}") do |page|
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
Expand All @@ -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
Expand Down