cocoon 1.0.12 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change History / Release Notes
2
2
 
3
+ ## Version 1.0.13
4
+
5
+ * A while ago we added the option to add a javascript callback on inserting a new associated object, I now made sure we can add a callback on insertion
6
+ and on removal of a new item. One example where this was useful for me is visible in the demo project `cocoon_simple_form_demo` where I implemented a
7
+ `belongs_to` relation, and either select from a list, or add a new element.
8
+ So: the callback-mechanism has changed, and now the callback is bound to the parent container, instead of the link itself. This is because we can also
9
+ bind the removal callback there (as the removal link is inserted in the html dynamically).
10
+
11
+ For more info, see the `README`.
12
+
3
13
  ## Version 1.0.12
4
14
 
5
15
  * using "this" in `association-insertion-node` is now possible
data/README.markdown CHANGED
@@ -135,14 +135,6 @@ It takes four parameters:
135
135
 
136
136
  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).
137
137
 
138
- There is an option to add a callback on insertion. The callback can be added as follows:
139
-
140
- $("#todo_tasks a.add_fields").
141
- data("insertion-callback",
142
- function() {
143
- $(this).find("textarea").autoResize({extraSpace:0}).change();
144
- });
145
-
146
138
 
147
139
  ### link_to_remove_association
148
140
 
@@ -157,9 +149,57 @@ It takes three parameters:
157
149
 
158
150
  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
151
 
152
+ ### Callbacks (upon insert and remove of items)
153
+
154
+ There is an option to add a callback on insertion or removal. If in your view you have the following snippet to select an `onwer`
155
+ (we use slim for demonstration purposes)
156
+
157
+ #owner
158
+ #owner_from_list
159
+ = f.association :owner, :collection => Person.all(:order => 'name'), :prompt => 'Choose an existing owner'
160
+ = link_to_add_association 'add a new person as owner', f, :owner
161
+
162
+ This view part will either let you select an owner from the list of persons, or show the fields to add a new person as owner.
163
+
164
+
165
+ The callbacks can be added as follows:
166
+
167
+ $(document).ready(function() {
168
+ $('#owner').bind('insertion-callback',
169
+ function() {
170
+ $("#owner_from_list").hide();
171
+ $("#owner a.add_fields").hide();
172
+ });
173
+ $('#owner').bind("removal-callback",
174
+ function() {
175
+ $("#owner_from_list").show();
176
+ $("#owner a.add_fields").show();
177
+ });
178
+ });
179
+
180
+ Do note that for the callbacks to work there has to be a surrounding container (div), where you can bind the callbacks to.
181
+
182
+ ### Control the Insertion behaviour
183
+
184
+ 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 -location.
185
+
186
+ For example:
187
+
188
+ $(document).ready(function() {
189
+ $("#owner a.add_fields").
190
+ data("association-insertion-position", 'before').
191
+ data("association-insertion-node", 'this');
192
+ });
193
+
194
+
195
+ The `association-insertion-node` will determine where to add it. You can choose any selector here, or specify this (default it is the parent-container).
196
+
197
+ The `association-insertion-position` will determine where to add it in relation with the node. Only two options: `before` or `after`.
198
+
199
+
160
200
  ### Partial
161
201
 
162
- The partial should be named `_<association-object_singular>_fields`, and should start with a div of class `.nested-fields`.
202
+ The partial should be named `_<association-object_singular>_fields`, and should start with a container (e.g. `div`) of class `.nested-fields`.
163
203
 
164
204
  There is no limit to the amount of nesting, though.
165
205
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.12
1
+ 1.0.13
@@ -1,10 +1,14 @@
1
- $(document).ready(function() {
1
+ (function($) {
2
2
 
3
3
  function replace_in_content(content, regexp_str, with_str) {
4
4
  reg_exp = new RegExp(regexp_str);
5
5
  content.replace(reg_exp, with_str);
6
6
  }
7
7
 
8
+ function trigger_removal_callback(node) {
9
+ node.parent().parent().trigger('removal-callback');
10
+ }
11
+
8
12
  $('.add_fields').live('click', function(e) {
9
13
  e.preventDefault();
10
14
  var assoc = $(this).data('association'),
@@ -13,6 +17,7 @@ $(document).ready(function() {
13
17
  insertionPosition = $(this).data('association-insertion-position'),
14
18
  insertionNode = $(this).data('association-insertion-node'),
15
19
  insertionCallback = $(this).data('insertion-callback'),
20
+ removalCallback = $(this).data('removal-callback'),
16
21
  regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'),
17
22
  regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'),
18
23
  new_id = new Date().getTime(),
@@ -41,21 +46,21 @@ $(document).ready(function() {
41
46
  } else {
42
47
  insertionNode.before(contentNode);
43
48
  }
44
-
45
- if (insertionCallback){
46
- insertionCallback.call(contentNode);
47
- }
49
+
50
+ $(this).parent().trigger('insertion-callback');
48
51
  });
49
52
 
50
53
  $('.remove_fields.dynamic').live('click', function(e) {
54
+ trigger_removal_callback($(this));
51
55
  e.preventDefault();
52
56
  $(this).closest(".nested-fields").remove();
53
57
  });
54
58
 
55
59
  $('.remove_fields.existing').live('click', function(e) {
60
+ trigger_removal_callback($(this));
56
61
  e.preventDefault();
57
62
  $(this).prev("input[type=hidden]").val("1");
58
63
  $(this).closest(".nested-fields").hide();
59
64
  });
60
65
 
61
- });
66
+ })(jQuery);
data/cocoon.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cocoon}
8
- s.version = "1.0.12"
8
+ s.version = "1.0.13"
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 = %q{2011-08-25}
12
+ s.date = %q{2011-10-12}
13
13
  s.description = %q{Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.}
14
14
  s.email = %q{nathan@dixis.com}
15
15
  s.extra_rdoc_files = [
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,17 @@
1
1
  # Configure Rails Envinronment
2
2
  ENV["RAILS_ENV"] = "test"
3
3
 
4
+ # only start SimpleCov on ruby 1.9.x
5
+ if RUBY_VERSION[0..2].to_f >= 1.9
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+ end
9
+
10
+
4
11
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
12
  require "rails/test_help"
6
13
  require "rspec/rails"
7
- require 'simplecov'
14
+
8
15
 
9
16
  ActionMailer::Base.delivery_method = :test
10
17
  ActionMailer::Base.perform_deliveries = true
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: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 12
10
- version: 1.0.12
9
+ - 13
10
+ version: 1.0.13
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: 2011-08-25 00:00:00 +02:00
18
+ date: 2011-10-12 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency