rondo_form 0.2.0 → 0.2.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad2d01fb9c206a3f7942241492eee9ba969f10483a9a3a2f30d53d8fad04186c
|
4
|
+
data.tar.gz: 81567ac3ffe29883945dcf5b34855796570d16be438cf21a39893a2882c4af55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45b7754068730e619cba9d2d4433e3ab876e2c468d8176c324ad744ef1148efb76a723719819660cd0eebe8f03f9db575aa80785dd539805d8e94b590bab6f32
|
7
|
+
data.tar.gz: c2cbb54fa7d4a4d33ab66b78096df1e8b72dc10bea32374471b49abc71adf15b00720d5f04b8500558f5cbc6b49939f161b75e4c7962c1b5abe29af1ad777d50
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RondoForm
|
2
2
|
|
3
|
-
Handle nested forms, same as Cocoon, but using StimulusJS
|
3
|
+
Handle dynamic nested forms, same as Cocoon, but using StimulusJS
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -8,23 +8,64 @@ Install the gem and add to the application's Gemfile by executing:
|
|
8
8
|
|
9
9
|
$ bundle add rondo_form
|
10
10
|
|
11
|
-
|
11
|
+
Or inside the Gemfile add the following
|
12
12
|
|
13
|
-
$ gem
|
13
|
+
$ gem 'rondo_form', '~> 0.2.2'
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
For example, we have `Project` model, which has `has_many` relationship with `Task` model:
|
22
|
+
```
|
23
|
+
rails g scaffold Project name:string description:string
|
24
|
+
rails g model Task description:string done:boolean project:belongs_to
|
25
|
+
```
|
26
|
+
|
27
|
+
### Sample with SimpleForm
|
28
|
+
In your `projects/_form` partial:
|
29
|
+
``` erb
|
30
|
+
<%= simple_form_for(@project) do |f| %>
|
31
|
+
|
32
|
+
<div class="form-inputs">
|
33
|
+
<%= f.input :name %>
|
34
|
+
<%= f.input :description %>
|
35
|
+
</div>
|
36
|
+
|
37
|
+
<h3 class="text-xl mt-4">Tasks</h3>
|
38
|
+
<div class="my-2" data-controller="nested-rondo">
|
39
|
+
<div data-nested-rondo-target="fieldContain">
|
40
|
+
<%= f.simple_fields_for :tasks do |task| %>
|
41
|
+
<%= render "task_fields", f: task %>
|
42
|
+
<% end %>
|
43
|
+
</div>
|
44
|
+
<div class="links">
|
45
|
+
<%= link_to_add_association "Add Task", f, :tasks %>
|
46
|
+
</div>
|
47
|
+
</div>
|
48
|
+
|
49
|
+
<div class="form-actions mt-4">
|
50
|
+
<%= f.button :submit %>
|
51
|
+
</div>
|
52
|
+
<% end %>
|
53
|
+
|
54
|
+
```
|
55
|
+
|
56
|
+
In your `_task_fields` partial:
|
57
|
+
``` erb
|
58
|
+
<div class="nested-fields">
|
59
|
+
<%= f.input :description %>
|
60
|
+
<%= f.input :done, as: :boolean %>
|
61
|
+
<%= link_to_remove_association "Remove Task", f %>
|
62
|
+
</div>
|
63
|
+
|
64
|
+
```
|
65
|
+
|
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 prepended to this element.
|
28
69
|
|
29
70
|
## Contributing
|
30
71
|
|
@@ -5,10 +5,10 @@ module RondoForm
|
|
5
5
|
desc "This generator installs the javascript needed for rondo_form"
|
6
6
|
|
7
7
|
def copy_the_javascript
|
8
|
-
copy_file "
|
8
|
+
copy_file "nested_rondo_controller.js", "app/javascript/controllers/nested_rondo_controller.js", force: true
|
9
9
|
if (Rails.root.join("app/javascript/controllers/index.js")).exist?
|
10
10
|
append_to_file "app/javascript/controllers/index.js",
|
11
|
-
%(import
|
11
|
+
%(import NestedRondoController from "./nested_rondo_controller"\napplication.register("nested-rondo", NestedRondoController)\n)
|
12
12
|
else
|
13
13
|
say %(Couldn't find "app/javascript/controllers/index.js".), :red
|
14
14
|
end
|
data/lib/generators/rondo_form/templates/{cocoon_controller.js → nested_rondo_controller.js}
RENAMED
@@ -1,15 +1,14 @@
|
|
1
1
|
import { Controller } from "@hotwired/stimulus"
|
2
2
|
|
3
3
|
export default class extends Controller {
|
4
|
-
static targets = ["template"]
|
4
|
+
static targets = ["template", "fieldContain"]
|
5
5
|
|
6
6
|
addField(e) {
|
7
7
|
e.preventDefault();
|
8
8
|
|
9
9
|
let assoc = e.target.dataset.association;
|
10
10
|
let newField = this.buildNewAssociation(assoc);
|
11
|
-
|
12
|
-
insertionNode.insertAdjacentHTML("beforebegin", newField);
|
11
|
+
this.fieldContainTarget.insertAdjacentHTML("beforeend", newField);
|
13
12
|
}
|
14
13
|
|
15
14
|
removeField(e) {
|
data/lib/rondo_form/version.rb
CHANGED
@@ -24,7 +24,7 @@ module RondoForm
|
|
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(' ')
|
27
|
-
html_options[:'data-action'] = "click->
|
27
|
+
html_options[:'data-action'] = "click->nested-rondo#removeField"
|
28
28
|
f.hidden_field(:_destroy) + link_to(name, '', html_options)
|
29
29
|
end
|
30
30
|
end
|
@@ -48,11 +48,11 @@ 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-action'] = "click->
|
51
|
+
html_options[:'data-action'] = "click->nested-rondo#addField"
|
52
52
|
|
53
53
|
new_object = f.object.class.reflect_on_association(association).klass.new
|
54
54
|
model_name = new_object.class.name.underscore
|
55
|
-
hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {
|
55
|
+
hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {'nested-rondo_target': 'template'}) do
|
56
56
|
render_association(association, f, new_object)
|
57
57
|
end
|
58
58
|
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
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hungle00
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -78,7 +78,7 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- Rakefile
|
80
80
|
- lib/generators/rondo_form/install_generator.rb
|
81
|
-
- lib/generators/rondo_form/templates/
|
81
|
+
- lib/generators/rondo_form/templates/nested_rondo_controller.js
|
82
82
|
- lib/rondo_form.rb
|
83
83
|
- lib/rondo_form/version.rb
|
84
84
|
- lib/rondo_form/view_helpers.rb
|