rondo_form 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -5
- data/lib/rondo_form/version.rb +1 -1
- data/lib/rondo_form/view_helpers.rb +5 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 423cebe9e27a08956f9271b683f8c0088a41f4948ad4bca55173f1b332b3cae4
|
4
|
+
data.tar.gz: 7d070bc07b55acc9f776931248f470e168a61e37d4c166eeb56fee0ef219c160
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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,
|
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
80
|
_Convention_:
|
81
|
+
|
67
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.
|
68
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.
|
69
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
|
|
data/lib/rondo_form/version.rb
CHANGED
@@ -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
|
@@ -54,16 +54,17 @@ module RondoForm
|
|
54
54
|
new_object = f.object.class.reflect_on_association(association).klass.new
|
55
55
|
model_name = new_object.class.name.underscore
|
56
56
|
hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {'nested-rondo_target': 'template'}) do
|
57
|
-
render_association(association, f, new_object)
|
57
|
+
render_association(association, f, new_object, html_options)
|
58
58
|
end
|
59
59
|
hidden_div.html_safe + link_to(name, '', html_options )
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
# :nodoc:
|
64
|
-
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"
|
65
66
|
f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
|
66
|
-
render(
|
67
|
+
render(partial_name, :f => builder, :dynamic => true)
|
67
68
|
end
|
68
69
|
end
|
69
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
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hungle00
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.1.6
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Cocoon, but with Stimulus JS
|