nested_form 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +12 -0
- data/README.rdoc +30 -2
- data/Rakefile +23 -4
- data/lib/generators/nested_form/install_generator.rb +1 -1
- data/lib/nested_form/{builder.rb → builder_mixin.rb} +4 -4
- data/lib/nested_form/builders.rb +23 -0
- data/lib/nested_form/engine.rb +14 -0
- data/lib/nested_form/view_helper.rb +25 -9
- data/lib/nested_form.rb +1 -2
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +5 -0
- data/spec/dummy/app/assets/javascripts/jquery.js +8981 -0
- data/spec/dummy/app/assets/javascripts/jquery_nested_form.js +71 -0
- data/spec/dummy/app/assets/javascripts/projects.js +2 -0
- data/spec/dummy/app/assets/javascripts/prototype.js +6082 -0
- data/{lib/generators/nested_form/templates → spec/dummy/app/assets/javascripts}/prototype_nested_form.js +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +7 -0
- data/spec/dummy/app/assets/stylesheets/projects.css +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/projects_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/projects_helper.rb +2 -0
- data/spec/dummy/app/models/milestone.rb +3 -0
- data/spec/dummy/app/models/project.rb +4 -0
- data/spec/dummy/app/models/task.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/projects/new.html.erb +14 -0
- data/spec/dummy/config/application.rb +42 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +27 -0
- data/spec/dummy/config/environments/production.rb +54 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +59 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20110710143903_initial_tables.rb +23 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/test/functional/projects_controller_test.rb +7 -0
- data/spec/dummy/test/unit/helpers/projects_helper_test.rb +4 -0
- data/spec/form_spec.rb +31 -0
- data/spec/nested_form/builder_spec.rb +33 -31
- data/spec/nested_form/view_helper_spec.rb +26 -12
- data/spec/spec_helper.rb +9 -48
- metadata +84 -59
- data/Gemfile +0 -3
- data/lib/generators/nested_form/templates/jquery_nested_form.js +0 -51
@@ -0,0 +1,71 @@
|
|
1
|
+
jQuery(function($) {
|
2
|
+
window.NestedFormEvents = function() {
|
3
|
+
this.addFields = $.proxy(this.addFields, this);
|
4
|
+
this.removeFields = $.proxy(this.removeFields, this);
|
5
|
+
};
|
6
|
+
|
7
|
+
NestedFormEvents.prototype = {
|
8
|
+
addFields: function(e) {
|
9
|
+
// Setup
|
10
|
+
var link = e.currentTarget;
|
11
|
+
var assoc = $(link).attr('data-association'); // Name of child
|
12
|
+
var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
|
13
|
+
|
14
|
+
// Make the context correct by replacing new_<parents> with the generated ID
|
15
|
+
// of each of the parent objects
|
16
|
+
var context = ($(link).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
|
17
|
+
|
18
|
+
// context will be something like this for a brand new form:
|
19
|
+
// project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
|
20
|
+
// or for an edit form:
|
21
|
+
// project[tasks_attributes][0][assignments_attributes][1]
|
22
|
+
if (context) {
|
23
|
+
var parentNames = context.match(/[a-z_]+_attributes/g) || [];
|
24
|
+
var parentIds = context.match(/(new_)?[0-9]+/g) || [];
|
25
|
+
|
26
|
+
for(var i = 0; i < parentNames.length; i++) {
|
27
|
+
if(parentIds[i]) {
|
28
|
+
content = content.replace(
|
29
|
+
new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'),
|
30
|
+
'$1_' + parentIds[i] + '_');
|
31
|
+
|
32
|
+
content = content.replace(
|
33
|
+
new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
|
34
|
+
'$1[' + parentIds[i] + ']');
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
// Make a unique ID for the new child
|
40
|
+
var regexp = new RegExp('new_' + assoc, 'g');
|
41
|
+
var new_id = new Date().getTime();
|
42
|
+
content = content.replace(regexp, "new_" + new_id);
|
43
|
+
|
44
|
+
var field = this.insertFields(content, assoc, link);
|
45
|
+
$(link).closest("form")
|
46
|
+
.trigger({ type: 'nested:fieldAdded', field: field })
|
47
|
+
.trigger({ type: 'nested:fieldAdded:' + assoc, field: field });
|
48
|
+
return false;
|
49
|
+
},
|
50
|
+
insertFields: function(content, assoc, link) {
|
51
|
+
return $(content).insertBefore(link);
|
52
|
+
},
|
53
|
+
removeFields: function(e) {
|
54
|
+
var link = e.currentTarget;
|
55
|
+
var hiddenField = $(link).prev('input[type=hidden]');
|
56
|
+
hiddenField.val('1');
|
57
|
+
// if (hiddenField) {
|
58
|
+
// $(link).v
|
59
|
+
// hiddenField.value = '1';
|
60
|
+
// }
|
61
|
+
var field = $(link).closest('.fields');
|
62
|
+
field.hide();
|
63
|
+
$(link).closest("form").trigger({ type: 'nested:fieldRemoved', field: field });
|
64
|
+
return false;
|
65
|
+
}
|
66
|
+
};
|
67
|
+
|
68
|
+
window.nestedFormEvents = new NestedFormEvents();
|
69
|
+
$('form a.add_nested_fields').live('click', nestedFormEvents.addFields);
|
70
|
+
$('form a.remove_nested_fields').live('click', nestedFormEvents.removeFields);
|
71
|
+
});
|