cocooned 1.3.1 → 1.3.2
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/History.md +9 -0
- data/README.md +119 -91
- data/Rakefile +1 -0
- data/app/assets/javascripts/cocooned.js +2 -0
- data/cocooned.gemspec +1 -0
- data/lib/cocooned/association_builder.rb +2 -1
- data/lib/cocooned/helpers/deprecate.rb +1 -1
- data/lib/cocooned/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4494f16e78b35fe2847ef24c3b8ec95183ce63cf05fc41a81ed9aa6b316ca1a1
|
4
|
+
data.tar.gz: 4f554493b8c3aa8fe6e9e89a1eb762d186336783ad24f948e31d4d97922f405b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c09909ed1c2e7b9db436be0918b19bc28815568c804aeb76e9caba91809baabea55a3a732bcfedeac5226d550820e79b85f12d69ffced86aa2844bfaca16e91
|
7
|
+
data.tar.gz: 0c41cef34455f05fb7bc278f563d063f40ca3e5c74f31dbc2ec642f60b694041657d9f35ca35cdf8579ba685ca0923d8a16994dacbdc34a56c65520e4d44cad3
|
data/History.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Change History / Release Notes
|
2
2
|
|
3
|
+
## Version 1.3.2
|
4
|
+
|
5
|
+
* Compatibility with Mongoid 7+
|
6
|
+
|
7
|
+
## Version 1.3.1
|
8
|
+
|
9
|
+
* Use UMD pattern to load the Cocooned module
|
10
|
+
* Now publish packages on both rubygems.org and npmjs.com
|
11
|
+
|
3
12
|
## Version 1.3.0
|
4
13
|
|
5
14
|
### Breaking changes
|
data/README.md
CHANGED
@@ -33,7 +33,11 @@ Inside your `Gemfile` add the following:
|
|
33
33
|
gem "cocooned"
|
34
34
|
```
|
35
35
|
|
36
|
-
|
36
|
+
### Load Cocooned styles and scripts
|
37
|
+
|
38
|
+
If you use Sprockets, you have to require `cocooned` in your `application.js` and `application.css`, so it compiles with the asset pipeline.
|
39
|
+
|
40
|
+
If you use Yarn to manage your non-Ruby dependencies and/or Webpack to build your assets, you can install the [`@notus.sh/cocooned` companion package](https://www.npmjs.com/package/@notus.sh/cocooned).
|
37
41
|
|
38
42
|
## Usage
|
39
43
|
|
@@ -70,23 +74,23 @@ E.g. in your `ListsController`:
|
|
70
74
|
|
71
75
|
### Basic form
|
72
76
|
|
73
|
-
_Please note examples in this section are written with the [`haml` templating language](http://haml.info/)._
|
74
|
-
|
75
77
|
[Rails natively supports nested forms](https://guides.rubyonrails.org/form_helpers.html#nested-forms) but does not support adding or removing nested items.
|
76
78
|
|
77
|
-
```
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
```erb
|
80
|
+
<% # `app/views/lists/_form.html.erb` %>
|
81
|
+
<%= form_for @list do |f| %>
|
82
|
+
<%= f.input :name %>
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
84
|
+
<h3>Items</h3>
|
85
|
+
<%= f.fields_for :tasks do |item_form| %>
|
86
|
+
<% # This block is repeated for every task in @list.items %>
|
87
|
+
<%= item_form.label :description %>
|
88
|
+
<%= item_form.text_field :description %>
|
89
|
+
<%= item_form.check_box :done %>
|
90
|
+
<% end %>
|
91
|
+
|
92
|
+
<%= f.submit "Save" %>
|
93
|
+
<% end %>
|
90
94
|
```
|
91
95
|
|
92
96
|
To enable Cocooned on this first, we need to:
|
@@ -102,75 +106,88 @@ Let's do it.
|
|
102
106
|
|
103
107
|
We now have two files:
|
104
108
|
|
105
|
-
```
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
+
```erb
|
110
|
+
<% # `app/views/lists/_form.html.erb` %>
|
111
|
+
<%= form_for @list do |form| %>
|
112
|
+
<%= form.input :name %>
|
109
113
|
|
110
|
-
|
111
|
-
|
112
|
-
|
114
|
+
<h3>Items</h3>
|
115
|
+
<%= form.fields_for :items do |item_form|
|
116
|
+
<%= render 'item_fields', f: item_form %>
|
117
|
+
<% end %>
|
113
118
|
|
114
|
-
|
119
|
+
<%= form.submit "Save" %>
|
120
|
+
<% end %>
|
115
121
|
```
|
116
122
|
|
117
|
-
```
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
123
|
+
```erb
|
124
|
+
<% # `app/views/lists/_item_fields.html.erb` %>
|
125
|
+
<%= f.label :description %>
|
126
|
+
<%= f.text_field :description %>
|
127
|
+
<%= f.check_box :done %>
|
122
128
|
```
|
123
129
|
|
124
130
|
#### 2. Add a way to add a new item to the collection
|
125
131
|
|
126
|
-
```
|
127
|
-
|
128
|
-
|
129
|
-
|
132
|
+
```erb
|
133
|
+
<% # `app/views/lists/_form.html.erb` %>
|
134
|
+
<%= form_for @list do |form| %>
|
135
|
+
<%= form.input :name %>
|
130
136
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
+
<h3>Items</h3>
|
138
|
+
<div id="items">
|
139
|
+
<%= form.fields_for :tasks do |item_form| %>
|
140
|
+
<%= render 'item_fields', f: item_form %>
|
141
|
+
<% end %>
|
142
|
+
|
143
|
+
<div class="links">
|
144
|
+
<%= cocooned_add_item_link 'Add an item', form, :items %>
|
145
|
+
</div>
|
146
|
+
</div>
|
137
147
|
|
138
|
-
|
148
|
+
<%= form.submit "Save" %>
|
149
|
+
<% end %>
|
139
150
|
```
|
140
151
|
|
141
152
|
By default, a new item will be inserted just before the immediate parent of the 'Add an item' link. You can have a look at the documentation of `cocooned_add_item_link` for more information about how to change that but we'll keep it simple for now.
|
142
153
|
|
143
154
|
#### 3. Add a way to remove an item from the collection
|
144
155
|
|
145
|
-
```
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
156
|
+
```erb
|
157
|
+
<% # `app/views/lists/_item_fields.html.erb` %>
|
158
|
+
<div class="cocooned-item">
|
159
|
+
<%= f.label :description %>
|
160
|
+
<%= f.text_field :description %>
|
161
|
+
<%= f.check_box :done %>
|
162
|
+
<%= cocooned_remove_item_link 'Remove', f %>
|
163
|
+
</div>
|
152
164
|
```
|
153
165
|
|
154
|
-
The
|
166
|
+
The `cocooned-item` class is required for the `cocooned_remove_item_link` to work correctly.
|
155
167
|
|
156
168
|
#### 4. Initialize Cocooned to handle this form
|
157
169
|
|
158
170
|
Cocooned will detect on page load forms it should handle and initialize itself.
|
159
171
|
This detection is based on the presence of a `data-cocooned-options` attribute on the nested forms container.
|
160
172
|
|
161
|
-
```
|
162
|
-
|
163
|
-
|
164
|
-
|
173
|
+
```erb
|
174
|
+
<% # `app/views/lists/_form.html.erb` %>
|
175
|
+
<%= form_for @list do |form| %>
|
176
|
+
<%= form.input :name %>
|
165
177
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
178
|
+
<h3>Items</h3>
|
179
|
+
<div id="items" data-cocooned-options="<%= {}.to_json %>">
|
180
|
+
<%= form.fields_for :tasks do |item_form| %>
|
181
|
+
<%= render 'item_fields', f: item_form %>
|
182
|
+
<% end %>
|
183
|
+
|
184
|
+
<div class="links">
|
185
|
+
<%= cocooned_add_item_link 'Add an item', form, :items %>
|
186
|
+
</div>
|
187
|
+
</div>
|
172
188
|
|
173
|
-
|
189
|
+
<%= form.submit "Save" %>
|
190
|
+
</div>
|
174
191
|
```
|
175
192
|
|
176
193
|
And we're done!
|
@@ -188,52 +205,63 @@ For now, Cocooned supports two plugins:
|
|
188
205
|
|
189
206
|
The limit plugin is autoloaded when needed and does not require anything more than you specifiying the maximum number of items allowed in the association.
|
190
207
|
|
191
|
-
```
|
192
|
-
|
193
|
-
|
194
|
-
|
208
|
+
```erb
|
209
|
+
<% # `app/views/lists/_form.html.erb` %>
|
210
|
+
<%= form_for @list do |form| %>
|
211
|
+
<%= form.input :name %>
|
195
212
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
213
|
+
<h3>Items</h3>
|
214
|
+
<div id="items" data-cocooned-options="<%= { limit: 12 }.to_json %>">
|
215
|
+
<%= form.fields_for :tasks do |item_form| %>
|
216
|
+
<%= render 'item_fields', f: item_form %>
|
217
|
+
<% end %>
|
218
|
+
|
219
|
+
<div class="links">
|
220
|
+
<%= cocooned_add_item_link 'Add an item', form, :items %>
|
221
|
+
</div>
|
222
|
+
</div>
|
202
223
|
|
203
|
-
|
224
|
+
<%= form.submit "Save" %>
|
225
|
+
<% end %>
|
204
226
|
```
|
205
227
|
|
206
228
|
#### The reorderable plugin
|
207
229
|
|
208
230
|
The reorderable plugin is autoloaded when activated and does not support any particular options.
|
209
231
|
|
210
|
-
```
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
%h3 Items
|
216
|
-
#items{ :data => { cocooned_options: { reorderable: true }.to_json } }
|
217
|
-
= form.fields_for :tasks do |item_form|
|
218
|
-
= render 'item_fields', f: item_form
|
219
|
-
.links
|
220
|
-
= cocooned_add_item_link 'Add an item', form, :items
|
232
|
+
```erb
|
233
|
+
<% # `app/views/lists/_form.html.haml` %>
|
234
|
+
<%= form_for @list do |form| %>
|
235
|
+
<%= form.input :name %>
|
221
236
|
|
222
|
-
|
237
|
+
<h3>Items</h3>
|
238
|
+
<div id="items" data-cocooned-options="<%= { reorderable: true }.to_json %>">
|
239
|
+
<%= form.fields_for :tasks do |item_form| %>
|
240
|
+
<%= render 'item_fields', f: item_form %>
|
241
|
+
<% end %>
|
242
|
+
|
243
|
+
<div class="links">
|
244
|
+
<%= cocooned_add_item_link 'Add an item', form, :items %>
|
245
|
+
</div>
|
246
|
+
</div>
|
247
|
+
|
248
|
+
<%= form.submit "Save" %>
|
249
|
+
<% end %>
|
223
250
|
```
|
224
251
|
|
225
252
|
However, you need to edit your nested partial to add the links that allow your users to move an item up or down in the collection and to add a `position` field.
|
226
253
|
|
227
|
-
```
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
254
|
+
```erb
|
255
|
+
<% # `app/views/lists/_item_fields.html.erb` %>
|
256
|
+
<div class="cocooned-item">
|
257
|
+
<%= f.label :description %>
|
258
|
+
<%= f.text_field :description %>
|
259
|
+
<%= f.check_box :done %>
|
260
|
+
<%= f.hidden_field :position %>
|
261
|
+
<%= cocooned_move_item_up_link 'Up', f %>
|
262
|
+
<%= cocooned_move_item_down_link 'Down', f %>
|
263
|
+
<%= cocooned_remove_item_link 'Remove', f %>
|
264
|
+
</div>
|
237
265
|
```
|
238
266
|
|
239
267
|
Also, remember the strong parameters gotcha we mentioned earlier.
|
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
/* globals define */
|
2
2
|
|
3
|
+
// Use Universal Module Definition pattern to load Cocooned
|
4
|
+
// See https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
|
3
5
|
(function (root, factory) {
|
4
6
|
if (typeof define === 'function' && define.amd) {
|
5
7
|
// AMD. Register as an anonymous module.
|
data/cocooned.gemspec
CHANGED
@@ -54,11 +54,12 @@ module Cocooned
|
|
54
54
|
def build_without_reflection
|
55
55
|
methods = %W[build_#{plural_name} build_#{singular_name}].select { |m| form.object.respond_to?(m) }
|
56
56
|
raise "Association #{association} doesn't exist on #{form.object.class}" unless methods.any?
|
57
|
+
|
57
58
|
form.object.send(methods.first)
|
58
59
|
end
|
59
60
|
|
60
61
|
def should_use_conditions?
|
61
|
-
reflection.class.name
|
62
|
+
reflection.class.name.starts_with?('Mongoid::') || @options[:force_non_association_create]
|
62
63
|
end
|
63
64
|
|
64
65
|
def build_with_conditions
|
@@ -14,7 +14,7 @@ module Cocooned
|
|
14
14
|
[
|
15
15
|
"NOTE: #{target_and_name} is deprecated",
|
16
16
|
replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
|
17
|
-
format('. It will dissapear in
|
17
|
+
format('. It will dissapear in %<release>s.', release: release),
|
18
18
|
location.nil? ? '' : "\n#{target_and_name} called from #{location}"
|
19
19
|
].join.strip
|
20
20
|
end
|
data/lib/cocooned/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cocooned
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gaël-Ian Havard
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-04-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -115,6 +115,20 @@ dependencies:
|
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
|
+
- !ruby/object:Gem::Dependency
|
119
|
+
name: rubocop-performance
|
120
|
+
requirement: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
type: :development
|
126
|
+
prerelease: false
|
127
|
+
version_requirements: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
118
132
|
description: Easier nested form. Supports standard Rails forms, Formtastic and SimpleForm.
|
119
133
|
email:
|
120
134
|
- gael-ian@notus.sh
|