Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework Test Improvement #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
9 changes: 6 additions & 3 deletions lib/generators/nested_form/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ def self.source_root
File.dirname(__FILE__) + "/templates"
end


def copy_jquery_file
copy_file 'nested_form.js', 'public/javascripts/nested_form.js'
if File.exists?('public/javascripts/prototype.js')
copy_file 'prototype_nested_form.js', 'public/javascripts/nested_form.js'
else
copy_file 'jquery_nested_form.js', 'public/javascripts/nested_form.js'
end
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/generators/nested_form/templates/prototype_nested_form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
document.observe('click', function(e, el) {
if (el = e.findElement('form a.add_nested_fields')) {
// Setup
var assoc = el.readAttribute('data-association'); // Name of child
var content = $(assoc + '_fields_blueprint').innerHTML; // Fields template

// Make the context correct by replacing new_<parents> with the generated ID
// of each of the parent objects
var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('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);

el.insert({ before: content });
return false;
}
});

document.observe('click', function(e, el) {
if (el = e.findElement('form a.remove_nested_fields')) {
var hidden_field = el.previous(0);
if(hidden_field) {
hidden_field.value = '1';
}
el.ancestors()[0].hide();
return false;
}
});