rondo_form 0.2.4 → 0.2.6

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: 7c5d3d9c7c5df1095b20d8473c0e6e6307161cbf4c8b7a27aa7e43db9cdd6050
4
- data.tar.gz: 9aab5d1fff292c0e4c558aa3c37ef6954f52c82aeab2e8989e9751f744b29c7f
3
+ metadata.gz: 423cebe9e27a08956f9271b683f8c0088a41f4948ad4bca55173f1b332b3cae4
4
+ data.tar.gz: 7d070bc07b55acc9f776931248f470e168a61e37d4c166eeb56fee0ef219c160
5
5
  SHA512:
6
- metadata.gz: 9494cd91916ad3ff074712e7d2b0c2acfa34ee425da35979f40a9fb5252269837589ae9eab8f802d68f7cb60fc2b9dc2e87001a9d16de6fde5152e59d0a5f39a
7
- data.tar.gz: 768eca8ff0a7ba31f9661ea51b53551d8a79cd2a13bc29c960ade8827e638be36e8675ad98502d0f772f7f71962d911aa6cc35be895dbfaa983e59d14dd0c864
6
+ metadata.gz: 14e2bdf013942175ee60caa28aef367b3895f2955a155c486d21bf1e9efc59ccc41bc91b7e9f0d5d50fd3c521fb5cb1ff421283e1bb414284613be8fa9e7bae1
7
+ data.tar.gz: da491eafe4dacfb0e2d57350ed5f1d8030b4db9aeba61df1cf20849c2bb04256e141e7a99466330b2512cee9b121aa9e941cd7629b69f4c14b840b61d0f50499
data/README.md CHANGED
@@ -10,22 +10,37 @@ Install the gem and add to the application's Gemfile by executing:
10
10
 
11
11
  Or inside the Gemfile add the following
12
12
 
13
- $ gem 'rondo_form', '~> 0.2.4'
13
+ $ gem 'rondo_form', '~> 0.2.6'
14
14
 
15
15
  Run the installation task:
16
16
 
17
- $ rails g rondo_form:install
17
+ $ rails g rondo_form:install
18
18
 
19
19
  ## Usage
20
20
 
21
- For example, we have `Project` model, which has `has_many` relationship with `Task` model:
21
+ For example, you have `Project` model, which has `has_many` relationship with `Task` model:
22
+
22
23
  ```
23
24
  rails g scaffold Project name:string description:string
24
25
  rails g model Task description:string done:boolean project:belongs_to
25
26
  ```
26
27
 
28
+ You need to add `accepts_nested_attributes_for` to `Project` model:
29
+
30
+ ```
31
+ class Project < ApplicationRecord
32
+ has_many :tasks, dependent: :destroy
33
+ accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true
34
+ end
35
+ ```
36
+
27
37
  ### Sample with SimpleForm
38
+
39
+ The RondoForm gem adds two helper functions: `link_to_add_association` and `link_to_remove_association`.
40
+ The example below illustrates the way to use it.
41
+
28
42
  In your `projects/_form` partial:
43
+
29
44
  ``` erb
30
45
  <%= simple_form_for(@project) do |f| %>
31
46
 
@@ -50,23 +65,26 @@ In your `projects/_form` partial:
50
65
  <%= f.button :submit %>
51
66
  </div>
52
67
  <% end %>
53
-
54
68
  ```
55
69
 
56
70
  In your `_task_fields` partial:
71
+
57
72
  ``` erb
58
73
  <div class="task-field">
59
74
  <%= f.input :description %>
60
75
  <%= f.input :done, as: :boolean %>
61
76
  <%= link_to_remove_association "Remove Task", f %>
62
77
  </div>
63
-
64
78
  ```
65
79
 
66
- _Note_:
67
- - Stimulus controller is `nested-rondo`, so you need to declare the element with `data-controller="nested-rondo"`.
68
- - You must add `data-nested-rondo-target="fieldContain"` to an element, that wraps all nested fields, the new field will be appended to this element.
69
- - `data-nested-rondo-field-class-value` is used to detect the element that needs to be removed. Its value must match the class name that wraps the partial. If you do not declare it, it will default remove the closest parent element.
80
+ _Convention_:
81
+
82
+ - For convention, I named Stimulus controller `nested-rondo`. But you can change the name of Javascript file and the value of `data-controller` to match your purpose.
83
+ - `data-nested-rondo-target="fieldContain"` must be added to an element that wraps all nested fields, the new field will be appended to this element.
84
+ - `data-nested-rondo-field-class-value` is used to detect the element that needs to be removed. Its value must match the class name of an element that wraps the partial. If you do not declare it, it will default remove the closest parent element.
85
+ - The partial added when clicking `link_to_add_association` is named for the association name with the `_fields` suffix. In this example, the partial is named `task_fields`. You can change the partial name by passing the `partial_name` option to `link_to_add_association`.
86
+
87
+ View details implement for this sample at [rondo_form_demo_turbo_8](https://github.com/hungle00/turbo_8_demos/tree/rondo_form)
70
88
 
71
89
  ## Contributing
72
90
 
@@ -7,8 +7,7 @@ export default class extends Controller {
7
7
  addField(e) {
8
8
  e.preventDefault();
9
9
 
10
- let assoc = e.target.dataset.association;
11
- let newField = this.buildNewAssociation(assoc);
10
+ const newField = this.buildNewAssociation(e);
12
11
  this.fieldContainTarget.insertAdjacentHTML("beforeend", newField);
13
12
  }
14
13
 
@@ -24,16 +23,19 @@ export default class extends Controller {
24
23
  }
25
24
  }
26
25
 
27
- buildNewAssociation(assoc) {
28
- let content = this.templateTarget.innerHTML;
29
- let regexpBraced = new RegExp('\\[new_' + assoc + '\\]', 'g');
26
+ buildNewAssociation(element) {
27
+ const assoc = element.target.dataset.association;
28
+ const assocs = element.target.dataset.associations;
29
+ const content = this.templateTarget.innerHTML;
30
+
31
+ let regexpBraced = new RegExp('\\[new_' + assoc + '\\](.*?\\s)', 'g');
30
32
  let newId = new Date().getTime();
31
- let newContent = content.replace(regexpBraced, '[' + newId + ']');
33
+ let newContent = content.replace(regexpBraced, '[' + newId + ']$1');
32
34
 
33
35
  if (newContent == content) {
34
36
  // assoc can be singular or plural
35
- regexpBraced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
36
- newContent = content.replace(regexpBraced, '[' + newId + ']');
37
+ regexpBraced = new RegExp('\\[new_' + assocs + '\\](.*?\\s)', 'g');
38
+ newContent = content.replace(regexpBraced, '[' + newId + ']$1');
37
39
  }
38
40
  return newContent;
39
41
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RondoForm
4
- VERSION = "0.2.4"
4
+ VERSION = "0.2.6"
5
5
  end
@@ -11,7 +11,7 @@ module RondoForm
11
11
  # - *f* : the form this link should be placed in
12
12
  # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
13
13
  # - *&block*: the output of the block will be show in the link, see <tt>link_to</tt>
14
-
14
+
15
15
  def link_to_remove_association(*args, &block)
16
16
  if block_given?
17
17
  f = args.first
@@ -48,21 +48,23 @@ module RondoForm
48
48
 
49
49
  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
50
50
  html_options[:'data-association'] = association.to_s.singularize
51
+ html_options[:'data-associations'] = association.to_s.pluralize
51
52
  html_options[:'data-action'] = "click->nested-rondo#addField"
52
53
 
53
54
  new_object = f.object.class.reflect_on_association(association).klass.new
54
55
  model_name = new_object.class.name.underscore
55
56
  hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {'nested-rondo_target': 'template'}) do
56
- render_association(association, f, new_object)
57
+ render_association(association, f, new_object, html_options)
57
58
  end
58
59
  hidden_div.html_safe + link_to(name, '', html_options )
59
60
  end
60
61
  end
61
62
 
62
63
  # :nodoc:
63
- def render_association(association, f, new_object)
64
+ def render_association(association, f, new_object, html_options)
65
+ partial_name = html_options[:partial_name] || association.to_s.singularize + "_fields"
64
66
  f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
65
- render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
67
+ render(partial_name, :f => builder, :dynamic => true)
66
68
  end
67
69
  end
68
70
  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.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - hungle00
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-30 00:00:00.000000000 Z
11
+ date: 2024-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -89,7 +89,7 @@ licenses:
89
89
  metadata:
90
90
  homepage_uri: https://github.com/hungle00/rondo_form
91
91
  source_code_uri: https://github.com/hungle00/rondo_form
92
- post_install_message:
92
+ post_install_message:
93
93
  rdoc_options: []
94
94
  require_paths:
95
95
  - lib
@@ -104,8 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
- rubygems_version: 3.2.3
108
- signing_key:
107
+ rubygems_version: 3.1.6
108
+ signing_key:
109
109
  specification_version: 4
110
110
  summary: Cocoon, but with Stimulus JS
111
111
  test_files: []