nested_form_fields 0.7.2 → 0.7.3
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 +4 -4
- data/.travis.yml +4 -1
- data/Gemfile +0 -2
- data/README.md +109 -71
- data/lib/assets/javascripts/nested_form_fields.js.coffee +5 -1
- data/lib/nested_form_fields/version.rb +1 -1
- data/lib/nested_form_fields.rb +7 -1
- data/nested_form_fields.gemspec +2 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab74838842c0f157a5efc9ae46b5b99e60ed0c4d
|
4
|
+
data.tar.gz: b5b7663f9f374ad79f6fa41d6ca2106f3767d1e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fb53546a7acbbee53a0a5bbeba881558a46b5443b7db06e8e5bb9e8f70427ed1857674b3d1c3ffb4bbc95d3e0f2e3c21dd5a385d301db29989330150a779ad6
|
7
|
+
data.tar.gz: 65a2643fbcb49811b895ef960a4a9fba0612136ee51cf73c9800cc7adc84384a392109d47db6318afe589b4a9afbc63e0e61c9e3194f0dc12aa9ce70a45847c6
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -5,8 +5,8 @@ This Rails gem helps creating forms for models with nested has_many associations
|
|
5
5
|
It uses jQuery to dynamically add and remove nested associations.
|
6
6
|
|
7
7
|
- Works for arbitrarily deeply nested associations (tested up to 4 levels).
|
8
|
-
- Works with form builders like simple_form.
|
9
|
-
- Requires Ruby 1.9 and the Rails asset pipeline
|
8
|
+
- Works with form builders like [simple_form](https://github.com/plataformatec/simple_form).
|
9
|
+
- Requires Ruby 1.9+ and the Rails asset pipeline.
|
10
10
|
|
11
11
|
|
12
12
|
|
@@ -28,45 +28,55 @@ In your application.js file add:
|
|
28
28
|
|
29
29
|
Assume you have a user model with nested videos:
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
```ruby
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
has_many :videos
|
34
|
+
accepts_nested_attributes_for :videos, allow_destroy: true
|
35
|
+
end
|
36
|
+
```
|
35
37
|
|
36
|
-
Use the
|
38
|
+
Use the `nested_fields_for` helper inside your user form to add the video fields:
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
```haml
|
41
|
+
= form_for @user do |f|
|
42
|
+
= f.nested_fields_for :videos do |ff|
|
43
|
+
= ff.text_field :video_title
|
44
|
+
..
|
45
|
+
```
|
42
46
|
|
43
|
-
Links to add and remove fields can be added using the
|
47
|
+
Links to add and remove fields can be added using the `add_nested_fields_link` and `remove_nested_fields_link` helpers:
|
44
48
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
```haml
|
50
|
+
= form_for @user do |f|
|
51
|
+
= f.nested_fields_for :videos do |ff|
|
52
|
+
= ff.remove_nested_fields_link
|
53
|
+
= ff.text_field :video_title
|
54
|
+
..
|
55
|
+
= f.add_nested_fields_link :videos
|
56
|
+
```
|
51
57
|
|
52
|
-
Note that
|
58
|
+
Note that `remove_nested_fields_link` needs to be called within the `nested_fields_for` call and `add_nested_fields_link` outside of it via the parent builder.
|
53
59
|
|
54
60
|
## Link Customization
|
55
61
|
|
56
|
-
You can change the link text of
|
62
|
+
You can change the link text of `remove_nested_fields_link` and `add_nested_fields_link` like this:
|
57
63
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
```haml
|
65
|
+
...
|
66
|
+
ff.remove_nested_fields_link 'Remove me'
|
67
|
+
...
|
68
|
+
f.add_nested_fields_link :videos, 'Add another funtastic video'
|
69
|
+
```
|
70
|
+
|
71
|
+
You can add classes/attributes to the `remove_nested_fields_link` and `add_nested_fields_link` like this:
|
62
72
|
|
63
|
-
|
73
|
+
```haml
|
74
|
+
...
|
75
|
+
ff.remove_nested_fields_link 'Remove me', class: 'btn btn-danger', role: 'button'
|
76
|
+
...
|
77
|
+
f.add_nested_fields_link :videos, 'Add another funtastic video', class: 'btn btn-primary', role: 'button'
|
78
|
+
```
|
64
79
|
|
65
|
-
...
|
66
|
-
ff.remove_nested_fields_link 'Remove me', class: 'btn btn-danger', role: 'button'
|
67
|
-
...
|
68
|
-
f.add_nested_fields_link :videos, 'Add another funtastic video', class: 'btn btn-primary', role: 'button'
|
69
|
-
|
70
80
|
You can supply a block to the `remove_nested_fields_link` and the `add_nested_fields_link` helpers, as you can with `link_to`:
|
71
81
|
|
72
82
|
```haml
|
@@ -74,7 +84,7 @@ You can supply a block to the `remove_nested_fields_link` and the `add_nested_fi
|
|
74
84
|
Remove me %span.icon-trash
|
75
85
|
```
|
76
86
|
|
77
|
-
You can add a `data-confirm` attribute to the `remove_nested_fields_link` if you want the user to confirm whenever they remove a nested field:
|
87
|
+
You can add a `data-confirm` attribute to the `remove_nested_fields_link` if you want the user to confirm whenever they remove a nested field:
|
78
88
|
|
79
89
|
```haml
|
80
90
|
= ff.remove_nested_fields_link 'Remove me', data: { confirm: 'Are you sure?' }
|
@@ -84,71 +94,99 @@ You can add a `data-confirm` attribute to the `remove_nested_fields_link` if you
|
|
84
94
|
|
85
95
|
You can specify a custom container to add nested forms into, by supplying an id via the `data-insert-into` attribute of the `add_nested_fields_link`:
|
86
96
|
|
87
|
-
|
97
|
+
```haml
|
98
|
+
f.add_nested_fields_link :videos, 'Add another funtastic video', data: { insert_into: '<container_id>' }
|
99
|
+
```
|
88
100
|
|
89
101
|
## Custom Fields Wrapper
|
90
102
|
|
91
|
-
You can change the type of the element wrapping the nested fields using the
|
103
|
+
You can change the type of the element wrapping the nested fields using the `wrapper_tag` option:
|
92
104
|
|
93
|
-
|
105
|
+
```haml
|
106
|
+
= f.nested_fields_for :videos, wrapper_tag: :div do |ff|
|
107
|
+
```
|
94
108
|
|
95
109
|
The default wrapper element is a fieldset. To add legend element to the fieldset use:
|
96
110
|
|
97
|
-
|
111
|
+
```haml
|
112
|
+
= f.nested_fields_for :videos, legend: "Video" do |ff|
|
113
|
+
```
|
98
114
|
|
99
115
|
You can pass options like you would to the `content_tag` method by nesting them in a `:wrapper_options` hash:
|
100
116
|
|
101
|
-
|
117
|
+
```haml
|
118
|
+
= f.nested_fields_for :videos, wrapper_options: { class: 'row' } do |ff|
|
119
|
+
```
|
102
120
|
|
103
121
|
## Rails 4 Parameter Whitelisting
|
104
122
|
|
105
|
-
If you are using Rails 4 remember to add
|
106
|
-
|
123
|
+
If you are using Rails 4 remember to add {{ NESTED_MODEL }}_attributes and the attributes to the permitted params.
|
124
|
+
If you want to destroy the nested model you should add `:_destroy` and `:id`.
|
107
125
|
For example:
|
108
126
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
= f.add_nested_fields_link :videos
|
116
|
-
|
117
|
-
# app/controllers/users_controller
|
127
|
+
```haml
|
128
|
+
# app/views/users/_form.haml.erb
|
129
|
+
= form_for @user do |f|
|
130
|
+
= f.nested_fields_for :videos do |ff|
|
131
|
+
= ff.remove_nested_fields_link
|
132
|
+
= ff.text_field :video_title
|
118
133
|
..
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
134
|
+
= f.add_nested_fields_link :videos
|
135
|
+
```
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
# app/controllers/users_controller
|
139
|
+
..
|
140
|
+
def user_params
|
141
|
+
params.require(:user)
|
142
|
+
.permit(:name,:email,videos_attributes:[:video_title,:_destroy,:id])
|
143
|
+
# ^^^ ^^^ ^^^
|
144
|
+
# nested model attrs
|
145
|
+
# they will let you delete the nested model
|
146
|
+
end
|
147
|
+
```
|
126
148
|
|
127
149
|
## Events
|
128
150
|
|
129
|
-
There are
|
130
|
-
|
151
|
+
There are four JavaScript events firing before and after addition/removal of the fields in the `nested_form_fields` namespace:
|
152
|
+
|
153
|
+
- `fields_adding`
|
154
|
+
- `fields_added`
|
155
|
+
- `fields_removing`
|
156
|
+
- `fields_removed`
|
131
157
|
|
132
158
|
The events `fields_added` and `fields_removed` are triggered on the element being added or removed. The events bubble up so you can listen for them on any parent element.
|
133
|
-
This makes it easy to add listeners when you have multiple nested_form_fields on the same page.
|
159
|
+
This makes it easy to add listeners when you have multiple `nested_form_fields` on the same page.
|
134
160
|
|
135
161
|
CoffeeScript samples:
|
136
162
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
163
|
+
```coffeescript
|
164
|
+
# Listen on an element
|
165
|
+
initializeSortable -> ($el)
|
166
|
+
$el.sortable(...)
|
167
|
+
$el.on 'fields_added.nested_form_fields', (event, param) ->
|
168
|
+
console.log event.target # The added field
|
169
|
+
console.log $(this) # $el
|
170
|
+
|
171
|
+
# Listen on document
|
172
|
+
$(document).on "fields_added.nested_form_fields", (event, param) ->
|
173
|
+
switch param.object_class
|
174
|
+
when "video"
|
175
|
+
console.log "Video object added"
|
176
|
+
else
|
177
|
+
console.log "INFO: Fields were successfully added, callback not handled."
|
178
|
+
```
|
179
|
+
|
180
|
+
## Namespaced Associations
|
151
181
|
|
182
|
+
In case your has_many association is namespaced, you need to add the `class_name` parameter to `nested_fields_for`.
|
183
|
+
Example:
|
184
|
+
|
185
|
+
```haml
|
186
|
+
= form_for @user do |f|
|
187
|
+
= f.nested_fields_for :videos, class_name: "Medium::Video" do |ff|
|
188
|
+
...
|
189
|
+
```
|
152
190
|
|
153
191
|
## Contributing
|
154
192
|
|
@@ -43,7 +43,11 @@ nested_form_fields.bind_nested_forms_links = () ->
|
|
43
43
|
removed_index = parseInt(delete_association_field_name.match('(\\d+\\]\\[_destroy])')[0].match('\\d+')[0])
|
44
44
|
$.event.trigger("fields_removing.nested_form_fields",{object_class: object_class, delete_association_field_name: delete_association_field_name, removed_index: removed_index });
|
45
45
|
$nested_fields_container = $link.parents(".nested_fields").first()
|
46
|
-
$nested_fields_container.
|
46
|
+
delete_field = $nested_fields_container.find("input[type='hidden'][name='#{delete_association_field_name}']")
|
47
|
+
if delete_field.length > 0
|
48
|
+
delete_field.val('1')
|
49
|
+
else
|
50
|
+
$nested_fields_container.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
|
47
51
|
$nested_fields_container.hide()
|
48
52
|
$nested_fields_container.find('input[required]:hidden').removeAttr('required')
|
49
53
|
$nested_fields_container.trigger("fields_removed.nested_form_fields",{object_class: object_class, delete_association_field_name: delete_association_field_name, removed_index: removed_index});
|
data/lib/nested_form_fields.rb
CHANGED
@@ -68,7 +68,13 @@ module ActionView::Helpers
|
|
68
68
|
|
69
69
|
output = ActiveSupport::SafeBuffer.new
|
70
70
|
association.each do |child|
|
71
|
-
|
71
|
+
wrapper_options = options[:wrapper_options].clone || {}
|
72
|
+
if child._destroy == true
|
73
|
+
wrapper_options[:style] = wrapper_options[:style] ?
|
74
|
+
wrapper_options[:style] + ';' + 'display:none' :
|
75
|
+
'display:none'
|
76
|
+
end
|
77
|
+
output << nested_fields_wrapper(association_name, options[:wrapper_tag], options[:legend], wrapper_options) do
|
72
78
|
fields_for_nested_model("#{name}[#{options[:child_index] || nested_child_index(name)}]", child, options, block)
|
73
79
|
end
|
74
80
|
end
|
data/nested_form_fields.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.authors = ["Nico Ritsche"]
|
6
6
|
gem.email = ["ncrdevmail@gmail.com"]
|
7
7
|
gem.description = %q{Rails gem for dynamically adding and removing nested has_many association fields in a form.
|
8
|
-
Uses jQuery and supports multiple nesting levels. Requires Ruby 1.9 and the asset pipeline.}
|
8
|
+
Uses jQuery and supports multiple nesting levels. Requires Ruby 1.9+ and the asset pipeline.}
|
9
9
|
gem.summary = %q{Rails gem for dynamically adding and removing nested has_many association fields in a form.}
|
10
10
|
gem.homepage = ""
|
11
11
|
|
@@ -29,4 +29,5 @@ Gem::Specification.new do |gem|
|
|
29
29
|
gem.add_development_dependency 'haml', '>= 3.1.5'
|
30
30
|
gem.add_development_dependency 'haml-rails'
|
31
31
|
gem.add_development_dependency 'sass-rails', '~> 3.2.3'
|
32
|
+
gem.add_development_dependency 'test-unit', '1.2.3'
|
32
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_form_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nico Ritsche
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -164,9 +164,23 @@ dependencies:
|
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: 3.2.3
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: test-unit
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - '='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 1.2.3
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - '='
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: 1.2.3
|
167
181
|
description: |-
|
168
182
|
Rails gem for dynamically adding and removing nested has_many association fields in a form.
|
169
|
-
Uses jQuery and supports multiple nesting levels. Requires Ruby 1.9 and the asset pipeline.
|
183
|
+
Uses jQuery and supports multiple nesting levels. Requires Ruby 1.9+ and the asset pipeline.
|
170
184
|
email:
|
171
185
|
- ncrdevmail@gmail.com
|
172
186
|
executables: []
|