nested_form_fields 0.7 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -8
- data/lib/assets/javascripts/nested_form_fields.js.coffee +6 -3
- data/lib/nested_form_fields.rb +1 -1
- data/lib/nested_form_fields/version.rb +1 -1
- data/nested_form_fields.gemspec +2 -2
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb813a85779b0a95a43aec817a9e676dd616e892
|
4
|
+
data.tar.gz: 7668ca008dc5cd61c6d0b249fbcd86daa8ccf373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d8294f51086f30bc92f626a6e50e317ad719471755eb9644bef72d90f6d1e2f2dd155a9703cc54fc9e34964482359038c129de5e5db85a86f7d59af86281e6b
|
7
|
+
data.tar.gz: 2215f957696aec9a9feb9e440e04a1f70b4382687b1d75e487428d1cd67e2ab11e7fcf479d068f88298bf2c38d0bcf416e0572283a37dddd327f29db14b7f243
|
data/README.md
CHANGED
@@ -51,6 +51,8 @@ Links to add and remove fields can be added using the *add_nested_fields_link* a
|
|
51
51
|
|
52
52
|
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
53
|
|
54
|
+
## Link Customization
|
55
|
+
|
54
56
|
You can change the link text of *remove_nested_fields_link* and *add_nested_fields_link* like this:
|
55
57
|
|
56
58
|
...
|
@@ -64,6 +66,27 @@ You can add classes/attributes to the *remove_nested_fields_link* and *add_nest
|
|
64
66
|
ff.remove_nested_fields_link 'Remove me', class: 'btn btn-danger', role: 'button'
|
65
67
|
...
|
66
68
|
f.add_nested_fields_link :videos, 'Add another funtastic video', class: 'btn btn-primary', role: 'button'
|
69
|
+
|
70
|
+
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
|
+
|
72
|
+
```haml
|
73
|
+
= ff.remove_nested_fields_link
|
74
|
+
Remove me %span.icon-trash
|
75
|
+
```
|
76
|
+
|
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:
|
78
|
+
|
79
|
+
```haml
|
80
|
+
= ff.remove_nested_fields_link 'Remove me', data: { confirm: 'Are you sure?' }
|
81
|
+
```
|
82
|
+
|
83
|
+
## Custom Container
|
84
|
+
|
85
|
+
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
|
+
|
87
|
+
f.add_nested_fields_link :videos, 'Add another funtastic video', data: { insert_into: '<container_id>' }
|
88
|
+
|
89
|
+
## Custom Fields Wrapper
|
67
90
|
|
68
91
|
You can change the type of the element wrapping the nested fields using the *wrapper_tag* option:
|
69
92
|
|
@@ -77,6 +100,8 @@ You can pass options like you would to the `content_tag` method by nesting them
|
|
77
100
|
|
78
101
|
= f.nested_fields_for :videos, wrapper_options: { class: 'row' } do |ff|
|
79
102
|
|
103
|
+
## Rails 4 Parameter Whitelisting
|
104
|
+
|
80
105
|
If you are using Rails 4 remember to add << NESTED_MODEL >>_attributes and the attributes to the permitted params.
|
81
106
|
Also, if you want to destroy the nested model you should add :_destroy and :id.
|
82
107
|
For example:
|
@@ -99,11 +124,12 @@ For example:
|
|
99
124
|
# they will let you delete the nested model
|
100
125
|
end
|
101
126
|
|
127
|
+
## Events
|
102
128
|
|
103
129
|
There are 4 javascipt events firing before and after addition/removal of the fields in the *nested_form_fields* namespace. Namely:
|
104
130
|
fields_adding, fields_added, fields_removing, fields_removed.
|
105
131
|
|
106
|
-
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.
|
132
|
+
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.
|
107
133
|
This makes it easy to add listeners when you have multiple nested_form_fields on the same page.
|
108
134
|
|
109
135
|
CoffeeScript samples:
|
@@ -111,17 +137,17 @@ CoffeeScript samples:
|
|
111
137
|
# Listen on an element
|
112
138
|
initializeSortable -> ($el)
|
113
139
|
$el.sortable(...)
|
114
|
-
$el.on 'fields_added.nested_form_fields',
|
140
|
+
$el.on 'fields_added.nested_form_fields', (event, param) ->
|
115
141
|
console.log event.target # The added field
|
116
142
|
console.log $(this) # $el
|
117
143
|
|
118
144
|
# Listen on document
|
119
|
-
$(document).on "fields_added.nested_form_fields", (event,param) ->
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
145
|
+
$(document).on "fields_added.nested_form_fields", (event, param) ->
|
146
|
+
switch param.object_class
|
147
|
+
when "video"
|
148
|
+
console.log "Video object added"
|
149
|
+
else
|
150
|
+
console.log "INFO: Fields were successfully added, callback not handled."
|
125
151
|
|
126
152
|
|
127
153
|
## Contributing
|
@@ -8,8 +8,11 @@ nested_form_fields.bind_nested_forms_links = () ->
|
|
8
8
|
association_path = $link.data('association-path')
|
9
9
|
added_index = $(".nested_#{association_path}").length
|
10
10
|
$.event.trigger("fields_adding.nested_form_fields",{object_class: object_class, added_index: added_index, association_path: association_path});
|
11
|
-
|
12
|
-
|
11
|
+
if $link.data('scope')
|
12
|
+
$template = $("#{$link.data('scope')} ##{association_path}_template")
|
13
|
+
else
|
14
|
+
$template = $("##{association_path}_template")
|
15
|
+
target = $link.data('insert-into')
|
13
16
|
|
14
17
|
template_html = $template.html()
|
15
18
|
|
@@ -37,7 +40,7 @@ nested_form_fields.bind_nested_forms_links = () ->
|
|
37
40
|
return false unless $.rails.allowAction($link)
|
38
41
|
object_class = $link.data('object-class')
|
39
42
|
delete_association_field_name = $link.data('delete-association-field-name')
|
40
|
-
removed_index = parseInt(delete_association_field_name.match('(\\d+\\]\\[_destroy])')[0][0])
|
43
|
+
removed_index = parseInt(delete_association_field_name.match('(\\d+\\]\\[_destroy])')[0].match('\\d+')[0])
|
41
44
|
$.event.trigger("fields_removing.nested_form_fields",{object_class: object_class, delete_association_field_name: delete_association_field_name, removed_index: removed_index });
|
42
45
|
$nested_fields_container = $link.parents(".nested_fields").first()
|
43
46
|
$nested_fields_container.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
|
data/lib/nested_form_fields.rb
CHANGED
@@ -30,7 +30,7 @@ module ActionView::Helpers
|
|
30
30
|
args = []
|
31
31
|
args << (text || "Add #{association.to_s.singularize.humanize}") unless block_given?
|
32
32
|
args << ''
|
33
|
-
args << { class: "#{html_class} add_nested_fields_link",
|
33
|
+
args << { class: "#{html_class.empty? ? '' : html_class} add_nested_fields_link",
|
34
34
|
data: { association_path: association_path(association.to_s),
|
35
35
|
object_class: association.to_s.singularize }.merge(html_data)
|
36
36
|
}.merge(html_options)
|
data/nested_form_fields.gemspec
CHANGED
@@ -18,8 +18,9 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.license = 'MIT'
|
19
19
|
|
20
20
|
gem.add_dependency 'rails', '>= 3.2.0'
|
21
|
+
gem.add_dependency 'coffee-rails', '>= 3.2.1'
|
22
|
+
gem.add_dependency 'jquery-rails'
|
21
23
|
|
22
|
-
gem.add_development_dependency 'jquery-rails'
|
23
24
|
gem.add_development_dependency 'rspec-rails', '2.9.0'
|
24
25
|
gem.add_development_dependency 'assert_difference'
|
25
26
|
gem.add_development_dependency 'capybara'
|
@@ -28,5 +29,4 @@ Gem::Specification.new do |gem|
|
|
28
29
|
gem.add_development_dependency 'haml', '>= 3.1.5'
|
29
30
|
gem.add_development_dependency 'haml-rails'
|
30
31
|
gem.add_development_dependency 'sass-rails', '~> 3.2.3'
|
31
|
-
gem.add_development_dependency 'coffee-rails', '~> 3.2.1'
|
32
32
|
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:
|
4
|
+
version: 0.7.1
|
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-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coffee-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.1
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: jquery-rails
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -31,7 +45,7 @@ dependencies:
|
|
31
45
|
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: '0'
|
34
|
-
type: :
|
48
|
+
type: :runtime
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
@@ -150,20 +164,6 @@ dependencies:
|
|
150
164
|
- - "~>"
|
151
165
|
- !ruby/object:Gem::Version
|
152
166
|
version: 3.2.3
|
153
|
-
- !ruby/object:Gem::Dependency
|
154
|
-
name: coffee-rails
|
155
|
-
requirement: !ruby/object:Gem::Requirement
|
156
|
-
requirements:
|
157
|
-
- - "~>"
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
version: 3.2.1
|
160
|
-
type: :development
|
161
|
-
prerelease: false
|
162
|
-
version_requirements: !ruby/object:Gem::Requirement
|
163
|
-
requirements:
|
164
|
-
- - "~>"
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version: 3.2.1
|
167
167
|
description: |-
|
168
168
|
Rails gem for dynamically adding and removing nested has_many association fields in a form.
|
169
169
|
Uses jQuery and supports multiple nesting levels. Requires Ruby 1.9 and the asset pipeline.
|