cocooned 2.0.4 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b263ee9e9f62225ffda1bf6023baf4b2f26643ca3951bffe3789c251a3da267
4
- data.tar.gz: b390024eaa76fd227ac9e32b292aaf75987e9a3f42200069e51d02440cdd9ea9
3
+ metadata.gz: 0af704c95699d29310c3a1d09da4a4a4d67d90c8d375d34a2090211fefe03099
4
+ data.tar.gz: 1db3b3325459d3ee9450ba1f6b414016e07ab86b69e6bb2c2cbeb8ab6dfdd73a
5
5
  SHA512:
6
- metadata.gz: 35f0aecab1c7b427c52b99cf8d1665ac3a06a5d8bcb980a33d2577e18bdef322897ea080da60a05d7218b90977c53cc919cc14e967c314247c0ff4cd76f83d8e
7
- data.tar.gz: ff5d977c74da72179fa80cc2b62ad65bd709f7cce6734ebbd4f82123ac7f3349c6a865f030f035a5d32af6dc9f1522142a0705b1f7b610f6d7bd064bbbaf85fd
6
+ metadata.gz: 315b49b7e2a344f59d0192c6a12609321b18821b45afb53f9371d52a59153e7462dbe8fc74c3eabab50ea687e2e4d548c648b3499329a2f9007b67fa8b8e1acc
7
+ data.tar.gz: 8997c949d1a29a913a5ae5e93c8039e747afecfde72590a282c283323342a44226f8ebf63d7a9ceb8d189f783677d8bcc1fc5ab99fa38fcefec74e9e031f08f5
data/CHANGELOG.md CHANGED
@@ -6,6 +6,32 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## Version 2.1.1 (2024-02-10)
10
+
11
+ ### Fixed
12
+
13
+ * Template lookup in nested use cases (#61)
14
+ Template lookup used to be done on the whole HTML document so add triggers can be declared anywhere inside or outside a Cocooned container. In nested use cases, this could lead to incorrect template being used to build grand-child items when multiple child items had been built since the last form save. Lookup is now done first inside the closest Cocooned item if any to ensure the correct template will be used.
15
+
16
+ ## Version 2.1.0 (2024-01-28)
17
+
18
+ ### Added
19
+
20
+ * Add support for custom tag name to container helpers (#55, #56)
21
+ `cocooned_container` and `cocooned_item` now support an optional tag name as their first argument (as `content_tag` do) when the default `<div/>` is not appropriate.
22
+ **Warning:** This change is not supposed to break anything as helpers prototypes stays the same and no other positional argument where really expected before. However, depending on how you used these helpers with previous releases, you may encounter unexpected side effects.
23
+ * Documentation about nested use cases and event initialization (#59)
24
+
25
+ ### Fixed
26
+
27
+ * Replacements operated on a newly built item in some nested use cases (#52, #57)
28
+ Replacements are now done recursively in nested templates if any exists. This should fix field naming in newly built items when multiple Cocooned instance are nested and correct naming of sub-items depends on generated names for their parent.
29
+
30
+ ### Changed
31
+
32
+ * Update test matrix (#54)
33
+ Add Ruby 3.3. Drop Ruby 2.6 and Ruby 2.7
34
+
9
35
  ## Version 2.0.4 (2023-11-23)
10
36
 
11
37
  ### Fixed
data/README.md CHANGED
@@ -193,7 +193,7 @@ And your sub form as follow:
193
193
  + <% end %>
194
194
  ```
195
195
 
196
- The `cocooned_container` and `cocooned_item` helpers will set for you the HTML attributes the JavaScript part of Cocooned expect to find to hook on. They will forward any option supported by ActionView's `content_tag`.
196
+ The `cocooned_container` and `cocooned_item` helpers will set for you the HTML attributes the JavaScript part of Cocooned expect to find to hook on. They will forward any option supported by ActionView's `content_tag` and accept a tag name as first argument if you don't want to use the default `<div>`.
197
197
 
198
198
  ### 3. Add a way to add a new item to the list
199
199
 
@@ -257,6 +257,10 @@ If you have a `has_one` association, then you (probably) need to set `force_non_
257
257
 
258
258
  See the [original merge request](https://github.com/nathanvda/cocoon/pull/247) for more details.
259
259
 
260
+ #### Complex nested forms
261
+
262
+ If you want to build complex forms with multiple levels of nesting, make sure you [initialize Cocooned event handlers correctly for dynamically added child items](https://github.com/notus-sh/cocooned/blob/main/npm/README.md#complex-nested-forms) or your form won't behave as you might expect.
263
+
260
264
  ## Plugins
261
265
 
262
266
  Cocooned comes with two built-in plugins:
@@ -306,10 +310,10 @@ Remember to add `:position` as a permitted parameter in your controller.
306
310
 
307
311
  Each helper provided by Cocooned with a name ending with `_link` has its `_button` equivalent, to generate a `<button type="button" />` instead of a `<a href="#" />`:
308
312
 
309
- - `cocooned_add_item_link` <=> `cocooned_add_item_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/master/lib/cocooned/helpers/tags/add.rb))
310
- - `cocooned_remove_item_link` <=> `cocooned_remove_item_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/master/lib/cocooned/helpers/tags/remove.rb))
311
- - `cocooned_move_item_up_link` <=> `cocooned_move_item_up_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/master/lib/cocooned/helpers/tags/up.rb))
312
- - `cocooned_move_item_down_link` <=> `cocooned_move_item_down_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/master/lib/cocooned/helpers/tags/down.rb))
313
+ - `cocooned_add_item_link` <=> `cocooned_add_item_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/main/lib/cocooned/helpers/tags/add.rb))
314
+ - `cocooned_remove_item_link` <=> `cocooned_remove_item_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/main/lib/cocooned/helpers/tags/remove.rb))
315
+ - `cocooned_move_item_up_link` <=> `cocooned_move_item_up_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/main/lib/cocooned/helpers/tags/up.rb))
316
+ - `cocooned_move_item_down_link` <=> `cocooned_move_item_down_button` ([Documentation](https://github.com/notus-sh/cocooned/blob/main/lib/cocooned/helpers/tags/down.rb))
313
317
 
314
318
  While all `_link` helpers accept and will politely forward any option supported by ActionView's `link_to`, `_button` helpers will do the same with options supported by ActionView's `button_tag`.
315
319
 
@@ -328,7 +332,7 @@ If no translation is found, the default label will be the humanized action name.
328
332
 
329
333
  ## Javascript
330
334
 
331
- For more documentation about the JavaScript bundled in the companion package, please refer to [its own documentation](https://github.com/notus-sh/cocooned/blob/master/npm/README.md).
335
+ For more documentation about the JavaScript bundled in the companion package, please refer to [its own documentation](https://github.com/notus-sh/cocooned/blob/main/npm/README.md).
332
336
 
333
337
  ## Styling forms
334
338
 
@@ -309,10 +309,7 @@
309
309
 
310
310
  build (id) {
311
311
  const node = this.#documentFragment.cloneNode(true);
312
- this.#replacements.forEach(replacement => {
313
- node.querySelectorAll(`*[${replacement.attribute}]`).forEach(node => replacement.apply(node, id));
314
- });
315
-
312
+ this.#applyReplacements(node, id);
316
313
  return node
317
314
  }
318
315
 
@@ -320,6 +317,16 @@
320
317
  #association
321
318
  #documentFragment
322
319
  #replacements
320
+
321
+ #applyReplacements (node, id) {
322
+ this.#replacements.forEach(replacement => {
323
+ node.querySelectorAll(`*[${replacement.attribute}]`).forEach(node => replacement.apply(node, id));
324
+ });
325
+
326
+ node.querySelectorAll('template').forEach(template => {
327
+ this.#applyReplacements(template.content, id);
328
+ });
329
+ }
323
330
  }
324
331
 
325
332
  class Traverser {
@@ -434,8 +441,9 @@
434
441
  }
435
442
 
436
443
  class Extractor {
437
- constructor (trigger) {
444
+ constructor (trigger, cocooned) {
438
445
  this.#trigger = trigger;
446
+ this.#cocooned = cocooned;
439
447
  }
440
448
 
441
449
  extract () {
@@ -452,6 +460,7 @@
452
460
  }
453
461
 
454
462
  /* Protected and private attributes and methods */
463
+ #cocooned
455
464
  #trigger
456
465
 
457
466
  get #dataset () {
@@ -463,7 +472,8 @@
463
472
  return null
464
473
  }
465
474
 
466
- const template = document.querySelector(`template[data-name="${this.#dataset.template}"]`);
475
+ const find = node => node?.querySelector(`template[data-name="${this.#dataset.template}"]`);
476
+ const template = find(this.#cocooned.toItem(this.#trigger)) || find(document);
467
477
  if (template === null) {
468
478
  return null
469
479
  }
@@ -567,7 +577,7 @@
567
577
 
568
578
  class Add extends Trigger {
569
579
  static create (trigger, cocooned) {
570
- const extractor = new Extractor(trigger);
580
+ const extractor = new Extractor(trigger, cocooned);
571
581
  return new Add(trigger, cocooned, extractor.extract())
572
582
  }
573
583
 
data/cocooned.gemspec CHANGED
@@ -43,9 +43,6 @@ Gem::Specification.new do |spec|
43
43
  spec.add_dependency 'rails', '>= 6.0', '<= 7.2'
44
44
 
45
45
  spec.add_development_dependency 'bundler', '~> 2.1'
46
- spec.add_development_dependency 'formtastic', '~> 4.0'
47
46
  spec.add_development_dependency 'rake', '~> 13.0'
48
47
  spec.add_development_dependency 'rspec', '~> 3.11'
49
- spec.add_development_dependency 'rspec-rails', '>= 5.0'
50
- spec.add_development_dependency 'simple_form', '~> 5.1'
51
48
  end
@@ -39,7 +39,7 @@ module Cocooned
39
39
  model = if reflection.collection?
40
40
  dummy.send(association).build
41
41
  else
42
- dummy.send("build_#{association}")
42
+ dummy.send(:"build_#{association}")
43
43
  end
44
44
  model = model.dup if model.frozen?
45
45
  model
@@ -31,10 +31,11 @@ module Cocooned
31
31
  # will be forwarded.
32
32
  def cocooned_container(*args, &block)
33
33
  options = args.extract_options!.dup
34
+ name = args.shift || :div
34
35
  defaults = cocooned_wrapper_defaults(options, %w[cocooned-container], :'cocooned-container')
35
36
  defaults[:data][:cocooned_options] = options.extract!(:limit, :reorderable).to_json
36
37
 
37
- content_tag(:div, *args, **options.deep_merge(defaults), &block)
38
+ content_tag(name, *args, **options.deep_merge(defaults), &block)
38
39
  end
39
40
 
40
41
  # Wrap content with the expected markup for a Cocooned item.
@@ -54,9 +55,10 @@ module Cocooned
54
55
  # be forwarded.
55
56
  def cocooned_item(*args, &block)
56
57
  options = args.extract_options!.dup
58
+ name = args.shift || :div
57
59
  defaults = cocooned_wrapper_defaults(options, %w[cocooned-item nested-fields], :'cocooned-item')
58
60
 
59
- content_tag(:div, *args, **options.deep_merge(defaults), &block)
61
+ content_tag(name, *args, **options.deep_merge(defaults), &block)
60
62
  end
61
63
 
62
64
  protected
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocooned
4
- VERSION = '2.0.4'
4
+ VERSION = '2.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocooned
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gaël-Ian Havard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-11-23 00:00:00.000000000 Z
12
+ date: 2024-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -45,20 +45,6 @@ dependencies:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2.1'
48
- - !ruby/object:Gem::Dependency
49
- name: formtastic
50
- requirement: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '4.0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '4.0'
62
48
  - !ruby/object:Gem::Dependency
63
49
  name: rake
64
50
  requirement: !ruby/object:Gem::Requirement
@@ -87,34 +73,6 @@ dependencies:
87
73
  - - "~>"
88
74
  - !ruby/object:Gem::Version
89
75
  version: '3.11'
90
- - !ruby/object:Gem::Dependency
91
- name: rspec-rails
92
- requirement: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '5.0'
97
- type: :development
98
- prerelease: false
99
- version_requirements: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '5.0'
104
- - !ruby/object:Gem::Dependency
105
- name: simple_form
106
- requirement: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '5.1'
111
- type: :development
112
- prerelease: false
113
- version_requirements: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '5.1'
118
76
  description: " Easier nested form in Rails with capabilities to add, remove, reorder
119
77
  or limit nested items. Works with standard Rails form builder, Formtastic or SimpleForm,
120
78
  and with or without jQuery. "
@@ -180,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
138
  - !ruby/object:Gem::Version
181
139
  version: '0'
182
140
  requirements: []
183
- rubygems_version: 3.4.1
141
+ rubygems_version: 3.5.3
184
142
  signing_key:
185
143
  specification_version: 4
186
144
  summary: Form builder agnostic handling of Rails nested forms