rondo_form 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab7c415b1a97a538742b84ffd41f05acb28e60dc6bb5721d059670c4b69e5e91
4
- data.tar.gz: e953952e1955dd4a8194b96ea28aff09aa6e0990ce5113648f4d94f846f55335
3
+ metadata.gz: 6ee9d8f4d7737a47f8e756a02d3dab153a2f8e162b8b5141755976c381470114
4
+ data.tar.gz: 2d1b339d1a473c7336ecf5fe3182b23591ab409f5cffe580fabd62b1f0c3ed1b
5
5
  SHA512:
6
- metadata.gz: 680cb74e9d615f9e1d3444363796a2cc8ec8d25dbc3caf2504bcee3188cb88c4001b911259cb256bb236d6bbaf6e0238edc71570fee7894996fd68ffc08cfe6b
7
- data.tar.gz: 1331fec9838d555ba6742f28aa74259644acbe7a019716a99118413eecb069fd69926f31a21db89e78a9d710e080caa2a0a62f56cd570344ff1e0194c4dbc96c
6
+ metadata.gz: 10b686a87cb7e4de4cd13616d9b85d875d36db630209989c5bb40ae19d23fa66ee0b6153b4220a42c6e4479289b0515f427917d0385ccea5a7f5c44741504d71
7
+ data.tar.gz: 76fcdce9be21925c0f1aba373fd19f996e7c9fbac7d4e3e952df969ebf4b3a6ed3eb232d47a8326161ad0f627f61267afbcc146c465f1e1dbbc2df6906e72e96
@@ -4,32 +4,38 @@ export default class extends Controller {
4
4
  static targets = ["template"]
5
5
 
6
6
  addField(e) {
7
- e.preventDefault()
8
- let assoc = e.target.dataset.association
9
- let newField = this.buildNewAssociation(assoc)
10
- let insertionNode = this.templateTarget.parentElement
11
- insertionNode.insertAdjacentHTML("beforebegin", newField)
7
+ e.preventDefault();
8
+
9
+ let assoc = e.target.dataset.association;
10
+ let newField = this.buildNewAssociation(assoc);
11
+ let insertionNode = this.templateTarget.parentElement;
12
+ insertionNode.insertAdjacentHTML("beforebegin", newField);
12
13
  }
13
14
 
14
15
  removeField(e) {
15
- e.preventDefault()
16
- let closestField = e.target.closest(".nested-fields")
16
+ e.preventDefault();
17
+
18
+ let wrapperClass = this.data.get("wrapperClass") || "nested-fields";
19
+ let wrapperField = e.target.closest("." + wrapperClass);
17
20
  if(e.target.matches('.dynamic')) {
18
- closestField.remove()
21
+ wrapperField.remove();
19
22
  } else {
20
- closestField.style.display = "none"
23
+ wrapperField.querySelector("input[name*='_destroy']").value = 1;
24
+ wrapperField.style.display = "none";
21
25
  }
22
26
  }
23
27
 
24
28
  buildNewAssociation(assoc) {
25
29
  let content = this.templateTarget.innerHTML;
26
- let regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');
27
- let new_id = new Date().getTime();
28
- let new_content = content.replace(regexp_braced, '[' + new_id + ']');
29
- if (new_content == content) {
30
- regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
31
- new_content = content.replace(regexp_braced, '[' + new_id + ']');
30
+ let regexpBraced = new RegExp('\\[new_' + assoc + '\\]', 'g');
31
+ let newId = new Date().getTime();
32
+ let newContent = content.replace(regexpBraced, '[' + newId + ']');
33
+
34
+ if (newContent == content) {
35
+ // assoc can be singular or plural
36
+ regexpBraced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
37
+ newContent = content.replace(regexpBraced, '[' + newId + ']');
32
38
  }
33
- return new_content
39
+ return newContent;
34
40
  }
35
41
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RondoForm
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -1,5 +1,6 @@
1
1
  module RondoForm
2
2
  module ViewHelpers
3
+
3
4
  # this will show a link to remove the current association. This should be placed inside the partial.
4
5
  # either you give
5
6
  # - *name* : the text of the link
@@ -18,9 +19,8 @@ module RondoForm
18
19
  name = capture(&block)
19
20
  link_to_remove_association(name, f, html_options)
20
21
  else
21
- name = args[0]
22
- f = args[1]
23
- html_options = args[2] || {}
22
+ name, f, html_options = *args
23
+ html_options ||= {}
24
24
 
25
25
  is_dynamic = f.object.new_record?
26
26
  html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
@@ -29,32 +29,22 @@ module RondoForm
29
29
  end
30
30
  end
31
31
 
32
- # :nodoc:
33
- def render_association(association, f, new_object)
34
- f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
35
- render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
36
- end
37
- end
38
-
39
32
  # shows a link that will allow to dynamically add a new associated object.
40
33
  #
41
34
  # - *name* : the text to show in the link
42
- # - *f* : the form this should come in (the formtastic form)
35
+ # - *f* : the form this should come in
43
36
  # - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
44
37
  # - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
45
38
  # - *&block*: see <tt>link_to</tt>
46
39
 
47
40
  def link_to_add_association(*args, &block)
48
41
  if block_given?
49
- f = args[0]
50
- association = args[1]
51
- html_options = args[2] || {}
42
+ f, association, html_options = *args
43
+ html_options ||= {}
52
44
  link_to_add_association(capture(&block), f, association, html_options)
53
45
  else
54
- name = args[0]
55
- f = args[1]
56
- association = args[2]
57
- html_options = args[3] || {}
46
+ name, f, association, html_options = *args
47
+ html_options ||= {}
58
48
 
59
49
  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
60
50
  html_options[:'data-association'] = association.to_s.singularize
@@ -68,5 +58,12 @@ module RondoForm
68
58
  end
69
59
  hidden_div.html_safe + link_to(name, '', html_options )
70
60
  end
61
+
62
+ # :nodoc:
63
+ def render_association(association, f, new_object)
64
+ f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
65
+ render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
66
+ end
67
+ end
71
68
  end
72
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rondo_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hungle00
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-13 00:00:00.000000000 Z
11
+ date: 2023-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties