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.
Files changed (61) hide show
  1. data/CHANGELOG.rdoc +12 -0
  2. data/README.rdoc +30 -2
  3. data/Rakefile +23 -4
  4. data/lib/generators/nested_form/install_generator.rb +1 -1
  5. data/lib/nested_form/{builder.rb → builder_mixin.rb} +4 -4
  6. data/lib/nested_form/builders.rb +23 -0
  7. data/lib/nested_form/engine.rb +14 -0
  8. data/lib/nested_form/view_helper.rb +25 -9
  9. data/lib/nested_form.rb +1 -2
  10. data/spec/dummy/Rakefile +7 -0
  11. data/spec/dummy/app/assets/javascripts/application.js +5 -0
  12. data/spec/dummy/app/assets/javascripts/jquery.js +8981 -0
  13. data/spec/dummy/app/assets/javascripts/jquery_nested_form.js +71 -0
  14. data/spec/dummy/app/assets/javascripts/projects.js +2 -0
  15. data/spec/dummy/app/assets/javascripts/prototype.js +6082 -0
  16. data/{lib/generators/nested_form/templates → spec/dummy/app/assets/javascripts}/prototype_nested_form.js +0 -0
  17. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  18. data/spec/dummy/app/assets/stylesheets/projects.css +4 -0
  19. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  20. data/spec/dummy/app/controllers/projects_controller.rb +5 -0
  21. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  22. data/spec/dummy/app/helpers/projects_helper.rb +2 -0
  23. data/spec/dummy/app/models/milestone.rb +3 -0
  24. data/spec/dummy/app/models/project.rb +4 -0
  25. data/spec/dummy/app/models/task.rb +3 -0
  26. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  27. data/spec/dummy/app/views/projects/new.html.erb +14 -0
  28. data/spec/dummy/config/application.rb +42 -0
  29. data/spec/dummy/config/boot.rb +10 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +27 -0
  33. data/spec/dummy/config/environments/production.rb +54 -0
  34. data/spec/dummy/config/environments/test.rb +39 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/inflections.rb +10 -0
  37. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  38. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  39. data/spec/dummy/config/initializers/session_store.rb +8 -0
  40. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  41. data/spec/dummy/config/locales/en.yml +5 -0
  42. data/spec/dummy/config/routes.rb +59 -0
  43. data/spec/dummy/config.ru +4 -0
  44. data/spec/dummy/db/development.sqlite3 +0 -0
  45. data/spec/dummy/db/migrate/20110710143903_initial_tables.rb +23 -0
  46. data/spec/dummy/db/schema.rb +29 -0
  47. data/spec/dummy/db/test.sqlite3 +0 -0
  48. data/spec/dummy/public/404.html +26 -0
  49. data/spec/dummy/public/422.html +26 -0
  50. data/spec/dummy/public/500.html +26 -0
  51. data/spec/dummy/public/favicon.ico +0 -0
  52. data/spec/dummy/script/rails +6 -0
  53. data/spec/dummy/test/functional/projects_controller_test.rb +7 -0
  54. data/spec/dummy/test/unit/helpers/projects_helper_test.rb +4 -0
  55. data/spec/form_spec.rb +31 -0
  56. data/spec/nested_form/builder_spec.rb +33 -31
  57. data/spec/nested_form/view_helper_spec.rb +26 -12
  58. data/spec/spec_helper.rb +9 -48
  59. metadata +84 -59
  60. data/Gemfile +0 -3
  61. 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
+ });
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.