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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 780896d01d77d65dabbf6e49138d75873f36ce38
4
- data.tar.gz: 9f788fb496249e0478d40a7740fabc23f30aafed
3
+ metadata.gz: ab74838842c0f157a5efc9ae46b5b99e60ed0c4d
4
+ data.tar.gz: b5b7663f9f374ad79f6fa41d6ca2106f3767d1e9
5
5
  SHA512:
6
- metadata.gz: 21559a77beb15101d5ffcabb94b70699d761efdd0ded937c9a2bd5b28a7fe5bcd1388fd209a98f521c37416c633532aa1b2a3d78c8a2724cdfc8a8b32b2f0704
7
- data.tar.gz: cede63960729dd02ed7498ac2177919c3af187ed19fdb26c8b44d7b91cdb0a14242ec701a4f24b2fb322c3597d122bc084f7d8ea71e0de80676a8b5133376cfe
6
+ metadata.gz: 3fb53546a7acbbee53a0a5bbeba881558a46b5443b7db06e8e5bb9e8f70427ed1857674b3d1c3ffb4bbc95d3e0f2e3c21dd5a385d301db29989330150a779ad6
7
+ data.tar.gz: 65a2643fbcb49811b895ef960a4a9fba0612136ee51cf73c9800cc7adc84384a392109d47db6318afe589b4a9afbc63e0e61c9e3194f0dc12aa9ce70a45847c6
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  rvm:
2
- - 2.1.3
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.7
5
+ - 2.2.3
3
6
  before_script:
4
7
  - "export DISPLAY=:99.0"
5
8
  - "sh -e /etc/init.d/xvfb start"
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- ruby '2.1.3'
4
-
5
3
  # Specify your gem's dependencies in nested_form_fields.gemspec
6
4
  gemspec
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
- class User < ActiveRecord::Base
32
- has_many :videos
33
- accepts_nested_attributes_for :videos, allow_destroy: true
34
- end
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 *nested_fields_for* helper inside your user form to add the video fields:
38
+ Use the `nested_fields_for` helper inside your user form to add the video fields:
37
39
 
38
- = form_for @user do |f|
39
- = f.nested_fields_for :videos do |ff|
40
- = ff.text_field :video_title
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 *add_nested_fields_link* and *remove_nested_fields_link* helpers:
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
- = form_for @user do |f|
46
- = f.nested_fields_for :videos do |ff|
47
- = ff.remove_nested_fields_link
48
- = ff.text_field :video_title
49
- ..
50
- = f.add_nested_fields_link :videos
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 *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.
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 *remove_nested_fields_link* and *add_nested_fields_link* like this:
62
+ You can change the link text of `remove_nested_fields_link` and `add_nested_fields_link` like this:
57
63
 
58
- ...
59
- ff.remove_nested_fields_link 'Remove me'
60
- ...
61
- f.add_nested_fields_link :videos, 'Add another funtastic video'
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
- You can add classes/attributes to the *remove_nested_fields_link* and *add_nested_fields_link* like this:
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
- f.add_nested_fields_link :videos, 'Add another funtastic video', data: { insert_into: '<container_id>' }
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 *wrapper_tag* option:
103
+ You can change the type of the element wrapping the nested fields using the `wrapper_tag` option:
92
104
 
93
- = f.nested_fields_for :videos, wrapper_tag: :div do |ff|
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
- = f.nested_fields_for :videos, legend: "Video" do |ff|
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
- = f.nested_fields_for :videos, wrapper_options: { class: 'row' } do |ff|
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 << NESTED_MODEL >>_attributes and the attributes to the permitted params.
106
- Also, if you want to destroy the nested model you should add :_destroy and :id.
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
- # app/views/users/_form.haml.erb
110
- = form_for @user do |f|
111
- = f.nested_fields_for :videos do |ff|
112
- = ff.remove_nested_fields_link
113
- = ff.text_field :video_title
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
- def user_params
120
- params.require(:user)
121
- .permit(:name,:email,videos_attributes:[:video_title,:_destroy,:id])
122
- # ^^^ ^^^ ^^^
123
- # nested model attrs
124
- # they will let you delete the nested model
125
- end
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 4 javascipt events firing before and after addition/removal of the fields in the *nested_form_fields* namespace. Namely:
130
- fields_adding, fields_added, fields_removing, fields_removed.
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
- # Listen on an element
138
- initializeSortable -> ($el)
139
- $el.sortable(...)
140
- $el.on 'fields_added.nested_form_fields', (event, param) ->
141
- console.log event.target # The added field
142
- console.log $(this) # $el
143
-
144
- # Listen on document
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."
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.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
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});
@@ -1,3 +1,3 @@
1
1
  module NestedFormFields
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
  end
@@ -68,7 +68,13 @@ module ActionView::Helpers
68
68
 
69
69
  output = ActiveSupport::SafeBuffer.new
70
70
  association.each do |child|
71
- output << nested_fields_wrapper(association_name, options[:wrapper_tag], options[:legend], options[:wrapper_options]) do
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
@@ -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.2
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-10-06 00:00:00.000000000 Z
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: []