giga-safe-tool 0.0.1
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 +7 -0
- data/cocoon-1.2.15/Gemfile +33 -0
- data/cocoon-1.2.15/History.md +197 -0
- data/cocoon-1.2.15/LICENSE +20 -0
- data/cocoon-1.2.15/README.markdown +612 -0
- data/cocoon-1.2.15/Rakefile +89 -0
- data/cocoon-1.2.15/VERSION +1 -0
- data/cocoon-1.2.15/app/assets/javascripts/cocoon.js +146 -0
- data/cocoon-1.2.15/cocoon.gemspec +149 -0
- data/cocoon-1.2.15/gemfiles/Gemfile.default +31 -0
- data/cocoon-1.2.15/gemfiles/Gemfile.rails-3.2.13 +30 -0
- data/cocoon-1.2.15/gemfiles/Gemfile.rails-4-r2 +27 -0
- data/cocoon-1.2.15/lib/cocoon/view_helpers.rb +160 -0
- data/cocoon-1.2.15/lib/cocoon.rb +20 -0
- data/cocoon-1.2.15/lib/generators/cocoon/install/install_generator.rb +17 -0
- data/cocoon-1.2.15/npm/README.md +8 -0
- data/cocoon-1.2.15/npm/package.json.erb +21 -0
- data/cocoon-1.2.15/spec/cocoon_spec.rb +446 -0
- data/cocoon-1.2.15/spec/dummy/Rakefile +7 -0
- data/cocoon-1.2.15/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/cocoon-1.2.15/spec/dummy/app/decorators/comment_decorator.rb +17 -0
- data/cocoon-1.2.15/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/cocoon-1.2.15/spec/dummy/app/models/comment.rb +7 -0
- data/cocoon-1.2.15/spec/dummy/app/models/person.rb +3 -0
- data/cocoon-1.2.15/spec/dummy/app/models/post.rb +9 -0
- data/cocoon-1.2.15/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/cocoon-1.2.15/spec/dummy/config/application.rb +45 -0
- data/cocoon-1.2.15/spec/dummy/config/boot.rb +10 -0
- data/cocoon-1.2.15/spec/dummy/config/database.yml +22 -0
- data/cocoon-1.2.15/spec/dummy/config/environment.rb +5 -0
- data/cocoon-1.2.15/spec/dummy/config/environments/development.rb +25 -0
- data/cocoon-1.2.15/spec/dummy/config/environments/production.rb +50 -0
- data/cocoon-1.2.15/spec/dummy/config/environments/test.rb +33 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/inflections.rb +10 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/rails_version_helper.rb +6 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/cocoon-1.2.15/spec/dummy/config/initializers/session_store.rb +8 -0
- data/cocoon-1.2.15/spec/dummy/config/locales/en.yml +5 -0
- data/cocoon-1.2.15/spec/dummy/config/routes.rb +58 -0
- data/cocoon-1.2.15/spec/dummy/config.ru +4 -0
- data/cocoon-1.2.15/spec/dummy/db/migrate/20110306212208_create_posts.rb +14 -0
- data/cocoon-1.2.15/spec/dummy/db/migrate/20110306212250_create_comments.rb +15 -0
- data/cocoon-1.2.15/spec/dummy/db/migrate/20110420222224_create_people.rb +15 -0
- data/cocoon-1.2.15/spec/dummy/db/schema.rb +39 -0
- data/cocoon-1.2.15/spec/dummy/public/404.html +26 -0
- data/cocoon-1.2.15/spec/dummy/public/422.html +26 -0
- data/cocoon-1.2.15/spec/dummy/public/500.html +26 -0
- data/cocoon-1.2.15/spec/dummy/public/favicon.ico +0 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/application.js +2 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/controls.js +965 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/effects.js +1123 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/cocoon-1.2.15/spec/dummy/public/javascripts/rails.js +175 -0
- data/cocoon-1.2.15/spec/dummy/script/rails +6 -0
- data/cocoon-1.2.15/spec/generators/install_generator_spec.rb +48 -0
- data/cocoon-1.2.15/spec/integration/navigation_spec.rb +9 -0
- data/cocoon-1.2.15/spec/spec_helper.rb +40 -0
- data/cocoon-1.2.15/spec/support/i18n.rb +3 -0
- data/cocoon-1.2.15/spec/support/rails_version_helper.rb +3 -0
- data/cocoon-1.2.15/spec/support/shared_examples.rb +73 -0
- data/giga-safe-tool.gemspec +11 -0
- metadata +103 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
# cocoon
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/nathanvda/cocoon)
|
|
4
|
+
|
|
5
|
+
Cocoon makes it easier to handle nested forms.
|
|
6
|
+
|
|
7
|
+
Nested forms are forms that handle nested models and attributes in one form;
|
|
8
|
+
e.g. a project with its tasks or an invoice with its line items.
|
|
9
|
+
|
|
10
|
+
Cocoon is form builder-agnostic, so it works with standard Rails, [Formtastic](https://github.com/justinfrench/formtastic), or [SimpleForm](https://github.com/plataformatec/simple_form).
|
|
11
|
+
It is compatible with rails 3, 4 and 5.
|
|
12
|
+
|
|
13
|
+
This project is not related to [Apache Cocoon](http://cocoon.apache.org/).
|
|
14
|
+
|
|
15
|
+
## Prerequisites
|
|
16
|
+
|
|
17
|
+
This gem depends on jQuery, so it's most useful in a Rails project where you are already using jQuery.
|
|
18
|
+
Furthermore, I would advise you to use either [Formtastic](https://github.com/justinfrench/formtastic) or [SimpleForm](https://github.com/plataformatec/simple_form).
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Inside your `Gemfile` add the following:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem "cocoon"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> Please note that for rails 4 you will need at least v1.2.0 or later.
|
|
29
|
+
|
|
30
|
+
### Rails 6/Webpacker
|
|
31
|
+
|
|
32
|
+
Add the componanion package
|
|
33
|
+
|
|
34
|
+
yarn add @nathanvda/cocoon
|
|
35
|
+
|
|
36
|
+
and then in your `app/javascripts/packs/application.js` you should add
|
|
37
|
+
|
|
38
|
+
require("jquery")
|
|
39
|
+
require("@nathanvda/cocoon")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
> Note: there are alternative npm packages, which might better suit your needs.
|
|
43
|
+
E.g. some offer the cocoon functionality without using jquery (search for `cocoon + vanilla` --I found three packages on npm already).
|
|
44
|
+
Obviously you are free to use those, however the code samples in this README will (still) rely on jquery.
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
### Rails 3.1+/Rails 4/Rails 5
|
|
48
|
+
|
|
49
|
+
Add the following to `application.js` so it compiles to the asset pipeline:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
//= require cocoon
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Rails 3.0.x
|
|
56
|
+
|
|
57
|
+
If you are using Rails 3.0.x, you need to run the installation task (since rails 3.1 this is no longer needed):
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
rails g cocoon:install
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This will install the Cocoon JavaScript file. In your application layout, add the following below the default javascripts:
|
|
64
|
+
|
|
65
|
+
```haml
|
|
66
|
+
= javascript_include_tag :cocoon
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Basic Usage
|
|
70
|
+
|
|
71
|
+
Suppose you have a `Project` model:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
rails g scaffold Project name:string description:string
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
And a project has many `tasks`:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
rails g model Task description:string done:boolean project:belongs_to
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Your models are associated like this:
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
class Project < ActiveRecord::Base
|
|
87
|
+
has_many :tasks, inverse_of: :project
|
|
88
|
+
accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class Task < ActiveRecord::Base
|
|
92
|
+
belongs_to :project
|
|
93
|
+
end
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
> *Rails 5 Note*: since rails 5 a `belongs_to` relation is by default required. While this absolutely makes sense, this also means
|
|
97
|
+
> associations have to be declared more explicitly.
|
|
98
|
+
> When saving nested items, theoretically the parent is not yet saved on validation, so rails needs help to know
|
|
99
|
+
> the link between relations. There are two ways: either declare the `belongs_to` as `optional: false`, but the
|
|
100
|
+
> cleanest way is to specify the `inverse_of:` on the `has_many`. That is why we write : `has_many :tasks, inverse_of: :project`
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
Now we want a project form where we can add and remove tasks dynamically.
|
|
105
|
+
To do this, we need the fields for a new or existing `task` to be defined in a partial
|
|
106
|
+
named `_task_fields.html`.
|
|
107
|
+
|
|
108
|
+
### Strong Parameters Gotcha
|
|
109
|
+
|
|
110
|
+
To destroy nested models, rails uses a virtual attribute called `_destroy`.
|
|
111
|
+
When `_destroy` is set, the nested model will be deleted. If the record is persisted, rails performs `id` field lookup to destroy the real record, so if `id` wasn't specified, it will treat current set of parameters like a parameters for a new record.
|
|
112
|
+
|
|
113
|
+
When using strong parameters (default in rails 4), you need to explicitly
|
|
114
|
+
add both `:id` and `:_destroy` to the list of permitted parameters.
|
|
115
|
+
|
|
116
|
+
E.g. in your `ProjectsController`:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
def project_params
|
|
120
|
+
params.require(:project).permit(:name, :description, tasks_attributes: [:id, :description, :done, :_destroy])
|
|
121
|
+
end
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Examples
|
|
125
|
+
|
|
126
|
+
Cocoon's default configuration requires `link_to_add_association` and associated partials to
|
|
127
|
+
be properly wrapped with elements. The examples below illustrate simple layouts.
|
|
128
|
+
|
|
129
|
+
Please note these examples rely on the `haml` gem ([click here](https://github.com/nathanvda/cocoon/wiki/ERB-examples) for the default `erb` views).
|
|
130
|
+
|
|
131
|
+
### Formtastic
|
|
132
|
+
|
|
133
|
+
In our `projects/_form` partial we'd write:
|
|
134
|
+
|
|
135
|
+
```haml
|
|
136
|
+
= semantic_form_for @project do |f|
|
|
137
|
+
= f.inputs do
|
|
138
|
+
= f.input :name
|
|
139
|
+
= f.input :description
|
|
140
|
+
%h3 Tasks
|
|
141
|
+
#tasks
|
|
142
|
+
= f.semantic_fields_for :tasks do |task|
|
|
143
|
+
= render 'task_fields', f: task
|
|
144
|
+
.links
|
|
145
|
+
= link_to_add_association 'add task', f, :tasks
|
|
146
|
+
= f.actions do
|
|
147
|
+
= f.action :submit
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
And in our `_task_fields` partial we'd write:
|
|
151
|
+
|
|
152
|
+
```haml
|
|
153
|
+
.nested-fields
|
|
154
|
+
= f.inputs do
|
|
155
|
+
= f.input :description
|
|
156
|
+
= f.input :done, as: :boolean
|
|
157
|
+
= link_to_remove_association "remove task", f
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The example project [cocoon_formtastic_demo](https://github.com/nathanvda/cocoon_formtastic_demo) demonstrates this.
|
|
161
|
+
|
|
162
|
+
### SimpleForm
|
|
163
|
+
|
|
164
|
+
In our `projects/_form` partial we'd write:
|
|
165
|
+
|
|
166
|
+
```haml
|
|
167
|
+
= simple_form_for @project do |f|
|
|
168
|
+
= f.input :name
|
|
169
|
+
= f.input :description
|
|
170
|
+
%h3 Tasks
|
|
171
|
+
#tasks
|
|
172
|
+
= f.simple_fields_for :tasks do |task|
|
|
173
|
+
= render 'task_fields', f: task
|
|
174
|
+
.links
|
|
175
|
+
= link_to_add_association 'add task', f, :tasks
|
|
176
|
+
= f.submit
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
In our `_task_fields` partial we write:
|
|
180
|
+
|
|
181
|
+
```haml
|
|
182
|
+
.nested-fields
|
|
183
|
+
= f.input :description
|
|
184
|
+
= f.input :done, as: :boolean
|
|
185
|
+
= link_to_remove_association "remove task", f
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The example project [cocoon_simple_form_demo](https://github.com/nathanvda/cocoon_simple_form_demo) demonstrates this.
|
|
189
|
+
|
|
190
|
+
### Standard Rails forms
|
|
191
|
+
|
|
192
|
+
In our `projects/_form` partial we'd write:
|
|
193
|
+
|
|
194
|
+
```haml
|
|
195
|
+
= form_for @project do |f|
|
|
196
|
+
.field
|
|
197
|
+
= f.label :name
|
|
198
|
+
%br
|
|
199
|
+
= f.text_field :name
|
|
200
|
+
.field
|
|
201
|
+
= f.label :description
|
|
202
|
+
%br
|
|
203
|
+
= f.text_field :description
|
|
204
|
+
%h3 Tasks
|
|
205
|
+
#tasks
|
|
206
|
+
= f.fields_for :tasks do |task|
|
|
207
|
+
= render 'task_fields', f: task
|
|
208
|
+
.links
|
|
209
|
+
= link_to_add_association 'add task', f, :tasks
|
|
210
|
+
= f.submit
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
In our `_task_fields` partial we'd write:
|
|
214
|
+
|
|
215
|
+
```haml
|
|
216
|
+
.nested-fields
|
|
217
|
+
.field
|
|
218
|
+
= f.label :description
|
|
219
|
+
%br
|
|
220
|
+
= f.text_field :description
|
|
221
|
+
.field
|
|
222
|
+
= f.check_box :done
|
|
223
|
+
= f.label :done
|
|
224
|
+
= link_to_remove_association "remove task", f
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## How it works
|
|
228
|
+
|
|
229
|
+
Cocoon defines two helper functions:
|
|
230
|
+
|
|
231
|
+
### link_to_add_association
|
|
232
|
+
|
|
233
|
+
This function adds a link to your markup that, when clicked, dynamically adds a new partial form for the given association.
|
|
234
|
+
This should be called within the form builder.
|
|
235
|
+
|
|
236
|
+
`link_to_add_association` takes four parameters:
|
|
237
|
+
|
|
238
|
+
- **name**: the text to show in the link
|
|
239
|
+
- **f**: the form builder
|
|
240
|
+
- **association**: the name of the association (plural) of which a new instance needs to be added (symbol or string).
|
|
241
|
+
- **html_options**: extra html-options (see [`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
|
|
242
|
+
There are some special options, the first three allow to control the placement of the new link-data:
|
|
243
|
+
- `data-association-insertion-traversal` : the jquery traversal method to allow node selection relative to the link. `closest`, `next`, `children`, etc. Default: absolute selection
|
|
244
|
+
- `data-association-insertion-node` : the jquery selector of the node as string, or a function that takes the `link_to_add_association` node as the parameter and returns a node. Default: parent node
|
|
245
|
+
- `data-association-insertion-method` : jquery method that inserts the new data. `before`, `after`, `append`, `prepend`, etc. Default: `before`
|
|
246
|
+
- `data-association-insertion-position` : old method specifying where to insert new data.
|
|
247
|
+
- this setting still works but `data-association-insertion-method` takes precedence. may be removed in a future version.
|
|
248
|
+
- `partial`: explicitly declare the name of the partial that will be used
|
|
249
|
+
- `render_options` : options passed through to the form-builder function (e.g. `simple_fields_for`, `semantic_fields_for` or `fields_for`).
|
|
250
|
+
If it contains a `:locals` option containing a hash, that is handed to the partial.
|
|
251
|
+
- `wrap_object` : a proc that will allow to wrap your object, especially useful if you are using decorators (e.g. draper). See example lower.
|
|
252
|
+
- `force_non_association_create`: if true, it will _not_ create the new object using the association (see lower)
|
|
253
|
+
- `form_name` : the name of the form parameter in your nested partial. By default this is `f`.
|
|
254
|
+
- `count` : the number of nested items to insert at once. Default `1`.
|
|
255
|
+
|
|
256
|
+
Optionally, you can omit the name and supply a block that is captured to render the link body (if you want to do something more complicated).
|
|
257
|
+
|
|
258
|
+
#### :render_options
|
|
259
|
+
Inside the `html_options` you can add an option `:render_options`, and the containing hash will be handed down to the form builder for the inserted
|
|
260
|
+
form.
|
|
261
|
+
|
|
262
|
+
When using Twitter Bootstrap and SimpleForm together, `simple_fields_for` needs the option `wrapper: 'inline'` which can
|
|
263
|
+
be handed down as follows:
|
|
264
|
+
|
|
265
|
+
(Note: In certain newer versions of simple_form, the option to use is `wrapper: 'bootstrap'`.)
|
|
266
|
+
|
|
267
|
+
```haml
|
|
268
|
+
= link_to_add_association 'add something', f, :something,
|
|
269
|
+
render_options: { wrapper: 'inline' }
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
To specify locals that needed to handed down to the partial:
|
|
273
|
+
|
|
274
|
+
```haml
|
|
275
|
+
= link_to_add_association 'add something', f, :something,
|
|
276
|
+
render_options: {locals: { sherlock: 'Holmes' }}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### :partial
|
|
280
|
+
|
|
281
|
+
To override the default partial name, e.g. because it shared between multiple views:
|
|
282
|
+
|
|
283
|
+
```haml
|
|
284
|
+
= link_to_add_association 'add something', f, :something,
|
|
285
|
+
partial: 'shared/something_fields'
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
#### :wrap_object
|
|
289
|
+
|
|
290
|
+
If you are using decorators, the normal instantiation of the associated object will not be enough. You actually want to generate the decorated object.
|
|
291
|
+
|
|
292
|
+
A simple decorator would look like:
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
class CommentDecorator
|
|
296
|
+
def initialize(comment)
|
|
297
|
+
@comment = comment
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def formatted_created_at
|
|
301
|
+
@comment.created_at.to_formatted_s(:short)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def method_missing(method_sym, *args)
|
|
305
|
+
if @comment.respond_to?(method_sym)
|
|
306
|
+
@comment.send(method_sym, *args)
|
|
307
|
+
else
|
|
308
|
+
super
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
To use this:
|
|
315
|
+
|
|
316
|
+
```haml
|
|
317
|
+
= link_to_add_association('add something', @form_obj, :comments,
|
|
318
|
+
wrap_object: Proc.new {|comment| CommentDecorator.new(comment) })
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Note that the `:wrap_object` expects an object that is _callable_, so any `Proc` will do. So you could as well use it to do some fancy extra initialisation (if needed).
|
|
322
|
+
But note you will have to return the (nested) object you want used.
|
|
323
|
+
E.g.
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
```haml
|
|
327
|
+
= link_to_add_association('add something', @form_obj, :comments,
|
|
328
|
+
wrap_object: Proc.new { |comment| comment.name = current_user.name; comment })
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
#### :force_non_association_create
|
|
332
|
+
|
|
333
|
+
In normal cases we create a new nested object using the association relation itself. This is the cleanest way to create
|
|
334
|
+
a new nested object.
|
|
335
|
+
|
|
336
|
+
This used to have a side-effect: for each call of `link_to_add_association` a new element was added to the association.
|
|
337
|
+
This is no longer the case.
|
|
338
|
+
|
|
339
|
+
For backward compatibility we keep this option for now. Or if for some specific reason you would really need an object
|
|
340
|
+
to be _not_ created on the association, for example if you did not want `after_add` callback to be triggered on
|
|
341
|
+
the association.
|
|
342
|
+
|
|
343
|
+
Example use:
|
|
344
|
+
|
|
345
|
+
```haml
|
|
346
|
+
= link_to_add_association('add something', @form_obj, :comments,
|
|
347
|
+
force_non_association_create: true)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
By default `:force_non_association_create` is `false`.
|
|
351
|
+
|
|
352
|
+
### link_to_remove_association
|
|
353
|
+
|
|
354
|
+
This function will add a link to your markup that, when clicked, dynamically removes the surrounding partial form.
|
|
355
|
+
This should be placed inside the partial `_<association-object-singular>_fields`.
|
|
356
|
+
|
|
357
|
+
It takes three parameters:
|
|
358
|
+
|
|
359
|
+
- **name**: the text to show in the link
|
|
360
|
+
- **f**: referring to the containing form-object
|
|
361
|
+
- **html_options**: extra html-options (see `link_to`)
|
|
362
|
+
|
|
363
|
+
Optionally you could also leave out the name and supply a block that is captured to give the name (if you want to do something more complicated).
|
|
364
|
+
|
|
365
|
+
Optionally, you can add an html option called `wrapper_class` to use a different wrapper div instead of `.nested-fields`.
|
|
366
|
+
The class should be added without a preceding dot (`.`).
|
|
367
|
+
|
|
368
|
+
> Note: the javascript behind the generated link relies on the presence of a wrapper class (default `.nested-fields`) to function correctly.
|
|
369
|
+
|
|
370
|
+
Example:
|
|
371
|
+
```haml
|
|
372
|
+
= link_to_remove_association('remove this', @form_obj,
|
|
373
|
+
{ wrapper_class: 'my-wrapper-class' })
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Callbacks (upon insert and remove of items)
|
|
377
|
+
|
|
378
|
+
On insertion or removal the following events are triggered:
|
|
379
|
+
|
|
380
|
+
* `cocoon:before-insert`: called before inserting a new nested child, can be [canceled](#canceling-a-callback)
|
|
381
|
+
* `cocoon:after-insert`: called after inserting
|
|
382
|
+
* `cocoon:before-remove`: called before removing the nested child, can be [canceled](#canceling-a-callback)
|
|
383
|
+
* `cocoon:after-remove`: called after removal
|
|
384
|
+
|
|
385
|
+
To listen to the events in your JavaScript:
|
|
386
|
+
|
|
387
|
+
```javascript
|
|
388
|
+
$('#container').on('cocoon:before-insert', function(e, insertedItem, originalEvent) {
|
|
389
|
+
// ... do something
|
|
390
|
+
});
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
...where `e` is the event and the second parameter is the inserted or removed item. This allows you to change markup, or
|
|
394
|
+
add effects/animations (see example below).
|
|
395
|
+
`originalEvent` is also passed and references the event that triggered an insertion or removal (e.g. the `click` event on the link to add an association)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
If in your view you have the following snippet to select an `owner`:
|
|
399
|
+
|
|
400
|
+
```haml
|
|
401
|
+
#owner
|
|
402
|
+
#owner_from_list
|
|
403
|
+
= f.association :owner, collection: Person.all(order: 'name'), prompt: 'Choose an existing owner'
|
|
404
|
+
= link_to_add_association 'add a new person as owner', f, :owner
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
This will either let you select an owner from the list of persons, or show the fields to add a new person as owner.
|
|
408
|
+
|
|
409
|
+
The callbacks can be added as follows:
|
|
410
|
+
|
|
411
|
+
```javascript
|
|
412
|
+
$(document).ready(function() {
|
|
413
|
+
$('#owner')
|
|
414
|
+
.on('cocoon:before-insert', function() {
|
|
415
|
+
$("#owner_from_list").hide();
|
|
416
|
+
$("#owner a.add_fields").hide();
|
|
417
|
+
})
|
|
418
|
+
.on('cocoon:after-insert', function() {
|
|
419
|
+
/* ... do something ... */
|
|
420
|
+
})
|
|
421
|
+
.on("cocoon:before-remove", function() {
|
|
422
|
+
$("#owner_from_list").show();
|
|
423
|
+
$("#owner a.add_fields").show();
|
|
424
|
+
})
|
|
425
|
+
.on("cocoon:after-remove", function() {
|
|
426
|
+
/* e.g. recalculate order of child items */
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// example showing manipulating the inserted/removed item
|
|
430
|
+
|
|
431
|
+
$('#tasks')
|
|
432
|
+
.on('cocoon:before-insert', function(e,task_to_be_added) {
|
|
433
|
+
task_to_be_added.fadeIn('slow');
|
|
434
|
+
})
|
|
435
|
+
.on('cocoon:after-insert', function(e, added_task) {
|
|
436
|
+
// e.g. set the background of inserted task
|
|
437
|
+
added_task.css("background","red");
|
|
438
|
+
})
|
|
439
|
+
.on('cocoon:before-remove', function(e, task) {
|
|
440
|
+
// allow some time for the animation to complete
|
|
441
|
+
$(this).data('remove-timeout', 1000);
|
|
442
|
+
task.fadeOut('slow');
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Note that for the callbacks to work there has to be a surrounding container to which you can bind the callbacks.
|
|
448
|
+
|
|
449
|
+
When adding animations and effects to make the removal of items more interesting, you will also have to provide a timeout.
|
|
450
|
+
This is accomplished by the following line:
|
|
451
|
+
|
|
452
|
+
```javascript
|
|
453
|
+
$(this).data('remove-timeout', 1000);
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
You could also immediately add this to your view, on the `.nested-fields` container (or the `wrapper_class` element you are using).
|
|
457
|
+
|
|
458
|
+
#### Canceling a Callback
|
|
459
|
+
|
|
460
|
+
You can cancel an action from occurring, either an insertion or removal, within the `before-<action>` callback by calling `event.preventDefault()` anywhere within the callback.
|
|
461
|
+
|
|
462
|
+
For example:
|
|
463
|
+
|
|
464
|
+
```javascript
|
|
465
|
+
$('#container').on('cocoon:before-insert', function(event, insertedItem) {
|
|
466
|
+
var confirmation = confirm("Are you sure?");
|
|
467
|
+
if( confirmation === false ){
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### Control the Insertion Behaviour
|
|
474
|
+
|
|
475
|
+
The default insertion location is at the back of the current container. But we have added two `data-` attributes that are read to determine the insertion-node and -method.
|
|
476
|
+
|
|
477
|
+
For example:
|
|
478
|
+
|
|
479
|
+
```javascript
|
|
480
|
+
$(document).ready(function() {
|
|
481
|
+
$("#owner a.add_fields").
|
|
482
|
+
data("association-insertion-method", 'before').
|
|
483
|
+
data("association-insertion-node", 'this');
|
|
484
|
+
});
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
The `association-insertion-node` will determine where to add it. You can choose any selector here, or specify this. Also, you can pass a function that returns an arbitrary node. The default is the parent-container, if you don't specify anything.
|
|
488
|
+
|
|
489
|
+
The `association-insertion-method` will determine where to add it in relation with the node. Any jQuery DOM Manipulation method can be set but we recommend sticking to any of the following: `before`, `after`, `append`, `prepend`. It is unknown at this time what others would do.
|
|
490
|
+
|
|
491
|
+
The `association-insertion-traversal` will allow node selection to be relative to the link.
|
|
492
|
+
|
|
493
|
+
For example:
|
|
494
|
+
|
|
495
|
+
```javascript
|
|
496
|
+
$(document).ready(function() {
|
|
497
|
+
$("#owner a.add_fields").
|
|
498
|
+
data("association-insertion-method", 'append').
|
|
499
|
+
data("association-insertion-traversal", 'closest').
|
|
500
|
+
data("association-insertion-node", '#parent_table');
|
|
501
|
+
});
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
(if you pass `association-insertion-node` as a function, this value will be ignored)
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
Note, if you want to add templates to the specific location which is:
|
|
508
|
+
|
|
509
|
+
- not a direct parent or sibling of the link
|
|
510
|
+
- the link appears multiple times - for instance, inside a deeply nested form
|
|
511
|
+
|
|
512
|
+
you need to specify `association-insertion-node` as a function.
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
For example, suppose Task has many SubTasks in the [Example](#examples), and have subtask forms like the following.
|
|
516
|
+
|
|
517
|
+
```haml
|
|
518
|
+
.row
|
|
519
|
+
.col-lg-12
|
|
520
|
+
.add_sub_task= link_to_add_association 'add a new sub task', f, :sub_tasks
|
|
521
|
+
.row
|
|
522
|
+
.col-lg-12
|
|
523
|
+
.sub_tasks_form
|
|
524
|
+
fields_for :sub_tasks do |sub_task_form|
|
|
525
|
+
= render 'sub_task_fields', f: sub_task_form
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
Then this will do the thing.
|
|
529
|
+
|
|
530
|
+
```javascript
|
|
531
|
+
$(document).ready(function() {
|
|
532
|
+
$(".add_sub_task a").
|
|
533
|
+
data("association-insertion-method", 'append').
|
|
534
|
+
data("association-insertion-node", function(link){
|
|
535
|
+
return link.closest('.row').next('.row').find('.sub_tasks_form')
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
### Partial
|
|
542
|
+
|
|
543
|
+
If no explicit partial name is given, `cocoon` looks for a file named `_<association-object_singular>_fields`.
|
|
544
|
+
To override the default partial use the `:partial` option.
|
|
545
|
+
|
|
546
|
+
For the JavaScript to behave correctly, the partial should start with a container (e.g. `div`) of class `.nested-fields`, or a class of your choice which you can define in the `link_to_remove_association` method.
|
|
547
|
+
|
|
548
|
+
There is no limit to the amount of nesting, though.
|
|
549
|
+
|
|
550
|
+
## I18n
|
|
551
|
+
|
|
552
|
+
As you seen in previous sections, the helper method `link_to_add_association` treats the first parameter as a name. Additionally, if it's skipped and the `form` object is passed as the first one, then **Cocoon** names it using **I18n**.
|
|
553
|
+
|
|
554
|
+
It allows to invoke helper methods like this:
|
|
555
|
+
|
|
556
|
+
```haml
|
|
557
|
+
= link_to_add_association form_object, :tasks
|
|
558
|
+
= link_to_remove_association form_object
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
instead of:
|
|
562
|
+
|
|
563
|
+
```haml
|
|
564
|
+
= link_to_add_association "Add task", form_object, :tasks
|
|
565
|
+
= link_to_remove_association "remove task", form_object
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
**Cocoon** uses the name of `association` as a translations scope key. If custom translations for association is not present it fallbacks to default name. Example of translations tree:
|
|
569
|
+
|
|
570
|
+
```yaml
|
|
571
|
+
en:
|
|
572
|
+
cocoon:
|
|
573
|
+
defaults:
|
|
574
|
+
add: "Add record"
|
|
575
|
+
remove: "Remove record"
|
|
576
|
+
tasks:
|
|
577
|
+
add: "Add new task"
|
|
578
|
+
remove: "Remove old task"
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
Note that `link_to_remove_association` does not require `association` name as an argument. In order to get correct translation key, **Cocoon** tableizes `class` name of the target object of form builder (`form_object.object` from previous example).
|
|
582
|
+
|
|
583
|
+
## Note on Patches/Pull Requests
|
|
584
|
+
|
|
585
|
+
* Fork the project.
|
|
586
|
+
* Make your feature addition or bug fix.
|
|
587
|
+
* Add tests for it. This is important so I don't break it in a
|
|
588
|
+
future version unintentionally.
|
|
589
|
+
* Commit, do not mess with rakefile, version, or history.
|
|
590
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
|
591
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
## Contributors
|
|
595
|
+
|
|
596
|
+
The list of contributors just keeps on growing. [Check it out!](https://github.com/nathanvda/cocoon/graphs/contributors)
|
|
597
|
+
I would really really like to thank all of them. They make cocoon more awesome every day. Thanks.
|
|
598
|
+
|
|
599
|
+
## Todo
|
|
600
|
+
|
|
601
|
+
* add more sample relations: `has_many :through`, `belongs_to`, ...
|
|
602
|
+
* improve the tests (test the javascript too)(if anybody wants to lend a hand ...?)
|
|
603
|
+
|
|
604
|
+
## Copyright
|
|
605
|
+
|
|
606
|
+
Copyright (c) 2010 Nathan Van der Auwera. See LICENSE for details.
|
|
607
|
+
|
|
608
|
+
## Not Related To Apache Cocoon
|
|
609
|
+
|
|
610
|
+
Please note that this project is not related to the Apache Cocoon web framework project.
|
|
611
|
+
|
|
612
|
+
[Apache Cocoon](http://cocoon.apache.org/), Cocoon, and Apache are either registered trademarks or trademarks of the [Apache Software Foundation](http://www.apache.org/) in the United States and/or other countries.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
begin
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
rescue LoadError
|
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
require 'rake'
|
|
10
|
+
require 'rdoc/task'
|
|
11
|
+
|
|
12
|
+
require 'rspec/core'
|
|
13
|
+
require 'rspec/core/rake_task'
|
|
14
|
+
require 'erb'
|
|
15
|
+
require 'JSON'
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
19
|
+
|
|
20
|
+
task :default => :spec
|
|
21
|
+
|
|
22
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
24
|
+
rdoc.title = 'Cocoon'
|
|
25
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
26
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
begin
|
|
31
|
+
require 'jeweler'
|
|
32
|
+
Jeweler::Tasks.new do |gem|
|
|
33
|
+
gem.name = "cocoon"
|
|
34
|
+
gem.summary = %Q{gem that enables easier nested forms with standard forms, formtastic and simple-form}
|
|
35
|
+
gem.description = %Q{Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.}
|
|
36
|
+
gem.email = "nathan@dixis.com"
|
|
37
|
+
gem.homepage = "http://github.com/nathanvda/cocoon"
|
|
38
|
+
gem.authors = ["Nathan Van der Auwera"]
|
|
39
|
+
gem.licenses = ["MIT"]
|
|
40
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
41
|
+
end
|
|
42
|
+
Jeweler::GemcutterTasks.new
|
|
43
|
+
rescue LoadError
|
|
44
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
require 'bundler/gem_tasks'
|
|
49
|
+
|
|
50
|
+
spec = Bundler.load_gemspec('./cocoon.gemspec')
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
npm_src_dir = './npm'
|
|
54
|
+
npm_dest_dir = './dist'
|
|
55
|
+
CLOBBER.include 'dist'
|
|
56
|
+
|
|
57
|
+
assets_dir = './app/assets/'
|
|
58
|
+
|
|
59
|
+
npm_files = {
|
|
60
|
+
File.join(npm_dest_dir, 'cocoon.js') => File.join(assets_dir, 'javascripts', 'cocoon.js'),
|
|
61
|
+
File.join(npm_dest_dir, 'README.md') => File.join(npm_src_dir, 'README.md'),
|
|
62
|
+
File.join(npm_dest_dir, 'LICENSE') => './LICENSE'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
namespace :npm do
|
|
66
|
+
npm_files.each do |dest, src|
|
|
67
|
+
file dest => src do
|
|
68
|
+
cp src, dest
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
task :'package-json' do
|
|
73
|
+
template = ERB.new(File.read(File.join(npm_src_dir, 'package.json.erb')))
|
|
74
|
+
content = template.result_with_hash(spec: spec)
|
|
75
|
+
File.write(File.join(npm_dest_dir, 'package.json'),
|
|
76
|
+
JSON.pretty_generate(JSON.parse(content)))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
desc "Build nathanvda-cocoon-#{spec.version}.tgz into the pkg directory"
|
|
80
|
+
task build: (%i[package-json] + npm_files.keys) do
|
|
81
|
+
system("cd #{npm_dest_dir} && npm pack && mv ./nathanvda-cocoon-#{spec.version}.tgz ../pkg/")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
desc "Build and push nathanvda-cocoon-#{spec.version}.tgz to https://npmjs.org"
|
|
85
|
+
task release: %i[build] do
|
|
86
|
+
system("npm publish ./pkg/nathanvda-cocoon-#{spec.version}.tgz --access public")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.2.15
|