Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
generate nested_form.js file when installing plugin, could use some work
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jan 12, 2010
1 parent d63c5aa commit d8fb714
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
52 changes: 52 additions & 0 deletions install.rb
@@ -0,0 +1,52 @@
path = "public/javascripts/nested_form.js"
puts "Generating #{path}"
File.open("#{Rails.root}/#{path}", "w") do |file|
file.print <<-EOS
$(function() {
$('form a.add_nested_fields').live('click', function() {
// Setup
var assoc = $(this).attr('data-association'); // Name of child
var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
// Make the context correct by replacing new_<parents> with the generated ID
// of each of the parent objects
var context = ($(this).parents('.fields').children('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
// context will be something like this for a brand new form:
// project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
// or for an edit form:
// project[tasks_attributes][0][assignments_attributes][1]
if(context) {
var parent_names = context.match(/[a-z]+_attributes/g) || []
var parent_ids = context.match(/[0-9]+/g)
for(i = 0; i < parent_names.length; i++) {
if(parent_ids[i]) {
content = content.replace(
new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
'$1[' + parent_ids[i] + ']'
)
}
}
}
// Make a unique ID for the new child
var regexp = new RegExp('new_' + assoc, 'g');
var new_id = new Date().getTime();
content = content.replace(regexp, new_id)
$(this).parent().before(content);
return false;
});
$('form a.remove_nested_fields').live('click', function() {
var hidden_field = $(this).prev('input[type=hidden]')[0];
if(hidden_field) {
hidden_field.value = '1';
}
$(this).closest('.fields').hide();
return false;
});
});
EOS
end
2 changes: 1 addition & 1 deletion lib/nested_form/builder.rb
Expand Up @@ -4,7 +4,7 @@ def link_to_add(name, association)
@fields ||= {}
@template.after_nested_form do
model_object = object.class.reflect_on_association(association).klass.new
@template.concat('<div style="display: none">')
@template.concat(%Q[<div id="#{association}_fields_blueprint" style="display: none">])
fields_for(association, model_object, :child_index => "new_#{association}", &@fields[association])
@template.concat('</div>')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/nested_form/builder_spec.rb
Expand Up @@ -29,7 +29,7 @@
mock(@template).after_nested_form { |block| block.call }
@builder.fields_for(:tasks) { @template.concat("Task") }
@builder.link_to_add("Add", :tasks)
@template.output_buffer.should == '<div style="display: none"><div class="fields">Task</div></div>'
@template.output_buffer.should == '<div id="tasks_fields_blueprint" style="display: none"><div class="fields">Task</div></div>'
end
end
end

0 comments on commit d8fb714

Please sign in to comment.