bbenezech-nested_form 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ 0.0.4
2
+
3
+ Vendor added in gemspec (0.2.0 in Ryanb)
4
+
1
5
  0.0.3
2
6
 
3
7
  Added id (generated from name) to top-level wrapper for cleaner JS manipulation
@@ -10,12 +14,26 @@ Added a class on blueprint div so that it is possible for javascript to know if
10
14
 
11
15
  Initial release of the @fxposter version
12
16
 
17
+
18
+ Below is Ryanb's changelog
19
+
20
+ 0.2.0 (February 7, 2012)
21
+
22
+ * Integration tests (thanks fxposter) - issue #58
23
+
24
+ * Improved simple_form and 3.1 support (thanks fxposter)
25
+
26
+ * nested:fieldAdded event includes the newly added field (thanks harrigan)
27
+
28
+ * other minor bug fixes
29
+
13
30
  0.1.1 (April 23, 2011)
14
31
 
15
32
  * Support HTML options and block in add/remove link - issue #31
16
33
 
17
34
  * Added new RegEx to generate new objects IDs correctly - issue #26 and issue #30
18
35
 
36
+
19
37
  0.1.0 (March 26, 2011)
20
38
 
21
39
  * Prefix new records with "new_" so there's no possible conflict with existing records - issue #21
@@ -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,51 @@
1
+ document.observe('click', function(e, el) {
2
+ if (el = e.findElement('form a.add_nested_fields')) {
3
+ // Setup
4
+ var assoc = el.readAttribute('data-association'); // Name of child
5
+ var content = $(assoc + '_fields_blueprint').innerHTML; // Fields template
6
+
7
+ // Make the context correct by replacing new_<parents> with the generated ID
8
+ // of each of the parent objects
9
+ var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
10
+
11
+ // context will be something like this for a brand new form:
12
+ // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
13
+ // or for an edit form:
14
+ // project[tasks_attributes][0][assignments_attributes][1]
15
+ if(context) {
16
+ var parent_names = context.match(/[a-z_]+_attributes/g) || [];
17
+ var parent_ids = context.match(/(new_)?[0-9]+/g) || [];
18
+
19
+ for(i = 0; i < parent_names.length; i++) {
20
+ if(parent_ids[i]) {
21
+ content = content.replace(
22
+ new RegExp('(_' + parent_names[i] + ')_.+?_', 'g'),
23
+ '$1_' + parent_ids[i] + '_');
24
+
25
+ content = content.replace(
26
+ new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
27
+ '$1[' + parent_ids[i] + ']');
28
+ }
29
+ }
30
+ }
31
+
32
+ // Make a unique ID for the new child
33
+ var regexp = new RegExp('new_' + assoc, 'g');
34
+ var new_id = new Date().getTime();
35
+ content = content.replace(regexp, "new_" + new_id);
36
+
37
+ el.insert({ before: content });
38
+ return false;
39
+ }
40
+ });
41
+
42
+ document.observe('click', function(e, el) {
43
+ if (el = e.findElement('form a.remove_nested_fields')) {
44
+ var hidden_field = el.previous(0);
45
+ if(hidden_field) {
46
+ hidden_field.value = '1';
47
+ }
48
+ el.ancestors()[0].hide();
49
+ return false;
50
+ }
51
+ });
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bbenezech-nested_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-09 00:00:00.000000000 Z
13
+ date: 2012-02-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec-rails
17
- requirement: &70185571726600 !ruby/object:Gem::Requirement
17
+ requirement: &70352126786760 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.6.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70185571726600
25
+ version_requirements: *70352126786760
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mocha
28
- requirement: &70185571725940 !ruby/object:Gem::Requirement
28
+ requirement: &70352126786040 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70185571725940
36
+ version_requirements: *70352126786040
37
37
  description: Gem to conveniently handle multiple models in a single form with Rails
38
38
  3 and jQuery or Prototype. Version maintained by fxposter.
39
39
  email: ryan@railscasts.com
@@ -94,6 +94,8 @@ files:
94
94
  - spec/nested_form/builder_spec.rb
95
95
  - spec/nested_form/view_helper_spec.rb
96
96
  - spec/spec_helper.rb
97
+ - vendor/assets/javascripts/jquery_nested_form.js
98
+ - vendor/assets/javascripts/prototype_nested_form.js
97
99
  - CHANGELOG.rdoc
98
100
  - LICENSE
99
101
  - Rakefile