cocoon 1.2.8 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -216,7 +216,7 @@ This should be called within the form builder.
216
216
  - html_options: extra html-options (see [`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
217
217
  There are some special options, the first three allow to control the placement of the new link-data:
218
218
  - `data-association-insertion-traversal` : the jquery traversal method to allow node selection relative to the link. `closest`, `next`, `children`, etc. Default: absolute selection
219
- - `data-association-insertion-node` : the jquery selector of the node. Default: parent node
219
+ - `data-association-insertion-node` : the jquery selector of the node as string, or a function that takes the `link_to_add_association` node as the parameter and returns a node. Default: parent node
220
220
  - `data-association-insertion-method` : jquery method that inserts the new data. `before`, `after`, `append`, `prepend`, etc. Default: `before`
221
221
  - `data-association-insertion-position` : old method specifying where to insert new data.
222
222
  - this setting still works but `data-association-insertion-method` takes precedence. may be removed in a future version.
@@ -441,11 +441,11 @@ $(document).ready(function() {
441
441
  });
442
442
  ```
443
443
 
444
- The `association-insertion-node` will determine where to add it. You can choose any selector here, or specify this (default it is the parent-container).
444
+ The `association-insertion-node` will determine where to add it. You can choose any selector here, or specify this. Also, you can pass a function that returns an arbitrary node. The default is the parent-container, if you don't specify anything.
445
445
 
446
446
  The `association-insertion-method` will determine where to add it in relation with the node. Any jQuery DOM Manipulation method can be set but we recommend sticking to any of the following: `before`, `after`, `append`, `prepend`. It is unknown at this time what others would do.
447
447
 
448
- The `association-insertion-traversal` will allow node selection to be relative to the link.
448
+ The `association-insertion-traversal` will allow node selection to be relative to the link.
449
449
 
450
450
  For example:
451
451
 
@@ -458,6 +458,43 @@ $(document).ready(function() {
458
458
  });
459
459
  ```
460
460
 
461
+ (if you pass `association-insertion-node` as a function, this value will be ignored)
462
+
463
+
464
+ Note, if you want to add templates to the specific location which is:
465
+
466
+ - not a direct parent or sibling of the link
467
+ - the link appears multiple times - for instance, inside a deeply nested form
468
+
469
+ you need to specify `association-insertion-node` as a function.
470
+
471
+
472
+ For example, suppose Task has many SubTasks in the [Example](#examples), and have subtask forms like the following.
473
+
474
+ ```haml
475
+ .row
476
+ .col-lg-12
477
+ .add_sub_task= link_to_add_association 'add a new sub task', f, :sub_tasks
478
+ .row
479
+ .col-lg-12
480
+ .sub_tasks_form
481
+ fields_for :sub_tasks do |sub_task_form|
482
+ = render 'sub_task_fields', f: sub_task_form
483
+ ```
484
+
485
+ Then this will do the thing.
486
+
487
+ ```javascript
488
+ $(document).ready(function() {
489
+ $(".add_sub_task a").
490
+ data("association-insertion-method", 'append').
491
+ data("association-insertion-node", function(link){
492
+ return link.closest('.row').next('.row').find('.sub_tasks_form')
493
+ });
494
+ });
495
+ ```
496
+
497
+
461
498
  ### Partial
462
499
 
463
500
  If no explicit partial name is given, `cocoon` looks for a file named `_<association-object_singular>_fields`.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.8
1
+ 1.2.9
@@ -14,6 +14,29 @@
14
14
  return '_' + id + '_$1';
15
15
  }
16
16
 
17
+ var getInsertionNodeElem = function(insertionNode, insertionTraversal, $this){
18
+
19
+ if (!insertionNode){
20
+ return $this.parent();
21
+ }
22
+
23
+ if (typeof insertionNode == 'function'){
24
+ if(insertionTraversal){
25
+ console.warn('association-insertion-traversal is ignored, because association-insertion-node is given as a function.')
26
+ }
27
+ return insertionNode($this);
28
+ }
29
+
30
+ if(typeof insertionNode == 'string'){
31
+ if (insertionTraversal){
32
+ return $this[insertionTraversal](insertionNode);
33
+ }else{
34
+ return insertionNode == "this" ? $this : $(insertionNode);
35
+ }
36
+ }
37
+
38
+ }
39
+
17
40
  $(document).on('click', '.add_fields', function(e) {
18
41
  e.preventDefault();
19
42
  var $this = $(this),
@@ -52,27 +75,23 @@
52
75
  count -= 1;
53
76
  }
54
77
 
55
- if (insertionNode){
56
- if (insertionTraversal){
57
- insertionNode = $this[insertionTraversal](insertionNode);
58
- } else {
59
- insertionNode = insertionNode == "this" ? $this : $(insertionNode);
60
- }
61
- } else {
62
- insertionNode = $this.parent();
78
+ var insertionNodeElem = getInsertionNodeElem(insertionNode, insertionTraversal, $this)
79
+
80
+ if( !insertionNodeElem || (insertionNodeElem.length == 0) ){
81
+ console.warn("Couldn't find the element to insert the template. Make sure your `data-association-insertion-*` on `link_to_add_association` is correct.")
63
82
  }
64
83
 
65
84
  $.each(new_contents, function(i, node) {
66
85
  var contentNode = $(node);
67
86
 
68
- insertionNode.trigger('cocoon:before-insert', [contentNode]);
87
+ insertionNodeElem.trigger('cocoon:before-insert', [contentNode]);
69
88
 
70
89
  // allow any of the jquery dom manipulation methods (after, before, append, prepend, etc)
71
90
  // to be called on the node. allows the insertion node to be the parent of the inserted
72
91
  // code and doesn't force it to be a sibling like after/before does. default: 'before'
73
- var addedContent = insertionNode[insertionMethod](contentNode);
92
+ var addedContent = insertionNodeElem[insertionMethod](contentNode);
74
93
 
75
- insertionNode.trigger('cocoon:after-insert', [contentNode]);
94
+ insertionNodeElem.trigger('cocoon:after-insert', [contentNode]);
76
95
  });
77
96
  });
78
97
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cocoon"
8
- s.version = "1.2.8"
8
+ s.version = "1.2.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Van der Auwera"]
12
- s.date = "2016-02-25"
12
+ s.date = "2016-03-21"
13
13
  s.description = "Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven."
14
14
  s.email = "nathan@dixis.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocoon
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.8
4
+ version: 1.2.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-25 00:00:00.000000000 Z
12
+ date: 2016-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -334,7 +334,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
334
334
  version: '0'
335
335
  segments:
336
336
  - 0
337
- hash: -2685326948987823638
337
+ hash: 3439139141720742655
338
338
  required_rubygems_version: !ruby/object:Gem::Requirement
339
339
  none: false
340
340
  requirements: