cocoon 1.0.17 → 1.0.18
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +39 -9
- data/VERSION +1 -1
- data/app/assets/javascripts/cocoon.js +11 -5
- data/cocoon.gemspec +2 -2
- data/lib/cocoon/view_helpers.rb +6 -1
- data/spec/cocoon_spec.rb +19 -0
- metadata +4 -4
data/README.markdown
CHANGED
@@ -149,14 +149,41 @@ It takes four parameters:
|
|
149
149
|
- f: referring to the containing form-object
|
150
150
|
- association: the name of the association (plural) of which a new instance needs to be added (symbol or string).
|
151
151
|
- html_options: extra html-options (see `link_to`)
|
152
|
-
There are
|
152
|
+
There are some special options, the first three allow to control the placement of the new link-data:
|
153
153
|
- `data-association-insertion-node` : the jquery selector of the node
|
154
154
|
- `data-association-insertion-method` : jquery method that inserts the new data. `before`, `after`, `append`, `prepend`, etc. Default: `before`
|
155
155
|
- `data-association-insertion-position` : old method specifying where to insert new data.
|
156
156
|
- this setting still works but `data-association-insertion-method` takes precedence. may be removed in a future version.
|
157
|
+
- `partial`: explicitly declare the name of the partial that will be used
|
158
|
+
- `render_options` : options passed through to the form-builder function (e.g. `simple_fields_for`, `semantic_fields_for` or `fields_for`).
|
159
|
+
If it contains a `:locals` option containing a hash, that is handed to the partial.
|
157
160
|
|
158
161
|
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).
|
159
162
|
|
163
|
+
#### :render_options
|
164
|
+
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
|
165
|
+
form. E.g. especially when using `twitter-bootstrap` and `simple_form` together, the `simple_fields_for` needs the option `:wrapper => 'inline'` which can
|
166
|
+
be handed down as follows:
|
167
|
+
|
168
|
+
````haml
|
169
|
+
= link_to_add_association 'add something', f, :something, :render_options => {:wrapper => 'inline' }
|
170
|
+
````
|
171
|
+
|
172
|
+
If you want to specify locals that needed to handed down to the partial, write
|
173
|
+
|
174
|
+
````haml
|
175
|
+
= link_to_add_association 'add something', f, :something, :render_options => {:locals => {:sherlock => 'Holmes' }}
|
176
|
+
````
|
177
|
+
|
178
|
+
|
179
|
+
#### :partial
|
180
|
+
|
181
|
+
To overrule the default partial name, e.g. because it shared between multiple views, write
|
182
|
+
|
183
|
+
````haml
|
184
|
+
= link_to_add_association 'add something', f, :something, :partial => 'shared/something_fields'
|
185
|
+
````
|
186
|
+
|
160
187
|
|
161
188
|
### link_to_remove_association
|
162
189
|
|
@@ -171,13 +198,6 @@ It takes three parameters:
|
|
171
198
|
|
172
199
|
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).
|
173
200
|
|
174
|
-
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
|
175
|
-
form. E.g. especially when using `twitter-bootstrap` and `simple_form` together, the `simple_fields_for` needs the option `:wrapper => 'inline'` which can
|
176
|
-
be handed down as follows:
|
177
|
-
|
178
|
-
````haml
|
179
|
-
= link_to_add_association 'add something', f, :something, :render_options => {:wrapper => 'inline' }
|
180
|
-
````
|
181
201
|
|
182
202
|
### Callbacks (upon insert and remove of items)
|
183
203
|
|
@@ -208,10 +228,15 @@ $(document).ready(function() {
|
|
208
228
|
$("#owner_from_list").show();
|
209
229
|
$("#owner a.add_fields").show();
|
210
230
|
});
|
231
|
+
$('#owner').bind("after-removal-callback",
|
232
|
+
function() {
|
233
|
+
/* e.g. recalculate order of child items */
|
234
|
+
});
|
211
235
|
});
|
212
236
|
````
|
213
237
|
|
214
238
|
Do note that for the callbacks to work there has to be a surrounding container (div), where you can bind the callbacks to.
|
239
|
+
Note that the default `removal-callback` is called _before_ removing the nested item.
|
215
240
|
|
216
241
|
### Control the Insertion behaviour
|
217
242
|
|
@@ -234,7 +259,12 @@ The `association-insertion-method` will determine where to add it in relation wi
|
|
234
259
|
|
235
260
|
### Partial
|
236
261
|
|
237
|
-
|
262
|
+
If no explicit partial-name is given, `cocoon` looks for a file named `_<association-object_singular>_fields`.
|
263
|
+
To override the default partial-name use the option `:partial`.
|
264
|
+
|
265
|
+
For the javascript to behave correctly, the partial should start with a container (e.g. `div`) of class `.nested-fields`.
|
266
|
+
|
267
|
+
|
238
268
|
|
239
269
|
There is no limit to the amount of nesting, though.
|
240
270
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.18
|
@@ -6,7 +6,11 @@
|
|
6
6
|
}
|
7
7
|
|
8
8
|
function trigger_removal_callback(node) {
|
9
|
-
node.
|
9
|
+
node.trigger('removal-callback');
|
10
|
+
}
|
11
|
+
|
12
|
+
function trigger_after_removal_callback(node) {
|
13
|
+
node.trigger('after-removal-callback');
|
10
14
|
}
|
11
15
|
|
12
16
|
$('.add_fields').live('click', function(e) {
|
@@ -17,8 +21,6 @@
|
|
17
21
|
content = $this.data('template'),
|
18
22
|
insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before';
|
19
23
|
insertionNode = $this.data('association-insertion-node'),
|
20
|
-
insertionCallback = $this.data('insertion-callback'),
|
21
|
-
removalCallback = $this.data('removal-callback'),
|
22
24
|
regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'),
|
23
25
|
regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'),
|
24
26
|
new_id = new Date().getTime(),
|
@@ -52,17 +54,21 @@
|
|
52
54
|
|
53
55
|
$('.remove_fields.dynamic').live('click', function(e) {
|
54
56
|
var $this = $(this);
|
55
|
-
|
57
|
+
var trigger_node = $this.closest(".nested-fields").parent();
|
58
|
+
trigger_removal_callback(trigger_node);
|
56
59
|
e.preventDefault();
|
57
60
|
$this.closest(".nested-fields").remove();
|
61
|
+
trigger_after_removal_callback(trigger_node);
|
58
62
|
});
|
59
63
|
|
60
64
|
$('.remove_fields.existing').live('click', function(e) {
|
61
65
|
var $this = $(this);
|
62
|
-
|
66
|
+
var trigger_node = $this.closest(".nested-fields").parent().parent();
|
67
|
+
trigger_removal_callback(trigger_node);
|
63
68
|
e.preventDefault();
|
64
69
|
$this.prev("input[type=hidden]").val("1");
|
65
70
|
$this.closest(".nested-fields").hide();
|
71
|
+
trigger_after_removal_callback(trigger_node);
|
66
72
|
});
|
67
73
|
|
68
74
|
})(jQuery);
|
data/cocoon.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "cocoon"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.18"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nathan Van der Auwera"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-04-07"
|
13
13
|
s.description = "Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven."
|
14
14
|
s.email = "nathan@dixis.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/cocoon/view_helpers.rb
CHANGED
@@ -33,9 +33,11 @@ module Cocoon
|
|
33
33
|
# :nodoc:
|
34
34
|
def render_association(association, f, new_object, render_options={}, custom_partial=nil)
|
35
35
|
partial = setup_partial(custom_partial, association)
|
36
|
+
locals = render_options.delete(:locals)
|
36
37
|
method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for)
|
37
38
|
f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
|
38
|
-
|
39
|
+
partial_options = {:f => builder, :dynamic => true}.merge(locals)
|
40
|
+
render(partial, partial_options)
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -45,6 +47,9 @@ module Cocoon
|
|
45
47
|
# - *f* : the form this should come in (the formtastic form)
|
46
48
|
# - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
|
47
49
|
# - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
|
50
|
+
# - *:render_options* : options passed to `simple_fields_for, semantic_fields_for or fields_for`
|
51
|
+
# - *:locals* : the locals hash in the :render_options is handed to the partial
|
52
|
+
# - *:partial* : explicitly override the default partial name
|
48
53
|
# - *&block*: see <tt>link_to</tt>
|
49
54
|
|
50
55
|
def link_to_add_association(*args, &block)
|
data/spec/cocoon_spec.rb
CHANGED
@@ -54,6 +54,13 @@ describe Cocoon do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
context "when using aliased association and class-name" do
|
58
|
+
it "uses the correct name" do
|
59
|
+
result = @tester.link_to_add_association('add something', @form_obj, :admin_comments)
|
60
|
+
result.to_s.should == '<a href="#" class="add_fields" data-association="admin_comment" data-associations="admin_comments" data-template="form<tag>">add something</a>'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
57
64
|
it "tttt" do
|
58
65
|
@post.class.reflect_on_association(:people).klass.new.should be_a(Person)
|
59
66
|
end
|
@@ -66,6 +73,18 @@ describe Cocoon do
|
|
66
73
|
end
|
67
74
|
end
|
68
75
|
|
76
|
+
context "with extra render-options to pass locals to the partial" do
|
77
|
+
it "uses the correct plural" do
|
78
|
+
@tester.unstub(:render_association)
|
79
|
+
@form_obj.should_receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
|
80
|
+
@tester.should_receive(:render).with("person_fields", {:f=>nil, :dynamic=>true, :alfred=>"Judoka"}).and_return ("partiallll")
|
81
|
+
result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline', :locals => {:alfred => 'Judoka'}})
|
82
|
+
result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="partiallll">add something</a>'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
|
69
88
|
context "when using formtastic" do
|
70
89
|
before(:each) do
|
71
90
|
@tester.unstub(:render_association)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cocoon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 51
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 18
|
10
|
+
version: 1.0.18
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nathan Van der Auwera
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-04-07 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|