Skip to content

Commit

Permalink
Allow nested associations with same name ryanb#237
Browse files Browse the repository at this point in the history
  • Loading branch information
groe committed Feb 11, 2013
1 parent 8655e35 commit 70a7197
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions vendor/assets/javascripts/jquery_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
// or for an edit form:
// project[tasks_attributes][0][assignments_attributes][1]
if (context) {
var prevParentsId = "";
var prevParentsName = "";
var parentNames = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || [];
var parentIds = context.match(/[0-9]+/g) || [];

for(var i = 0; i < parentNames.length; i++) {
if(parentIds[i]) {
content = content.replace(
new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'),
new RegExp('(_' + prevParentsId + parentNames[i] + ')_.+?_', 'g'),
'$1_' + parentIds[i] + '_');
prevParentsId += "_" + parentNames[i] + "_" + parentIds[i] + "_";

content = content.replace(
new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
new RegExp('(' + prevParentsName + '\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
'$1[' + parentIds[i] + ']');
prevParentsName += "\\[" + parentNames[i] + "\\]\\[" + parentIds[i] + "\\]";
}
}
}
Expand Down

0 comments on commit 70a7197

Please sign in to comment.