cocoon 1.0.13 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Change History / Release Notes
2
2
 
3
+ ## Version 1.0.14
4
+
5
+ * When playing with `simple_form` and `twitter-bootstrap`, I noticed it is crucial that I call the correct nested-fields function.
6
+ That is: `fields_for` for standard forms, `semantic_fields_for` in formtastic and `simple_fields_for` for simple_form.
7
+ Secondly, this was not enough, I needed to be able to hand down options to that method. So in the `link_to_add_association` method you
8
+ can now an extra option `:render_options` and that hash will be handed to the association-builder.
9
+
10
+ This allows the nested fields to be built correctly with `simple_form` for `twitter-bootstrap`.
11
+
3
12
  ## Version 1.0.13
4
13
 
5
14
  * 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
@@ -149,6 +149,13 @@ It takes three parameters:
149
149
 
150
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).
151
151
 
152
+ 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
153
+ form. E.g. especially when using `twitter-bootstrap` and `simple_form` together, the `simple_fields_for` needs the option `:wrapper => 'inline'` which can
154
+ be handed down as follows:
155
+
156
+ = link_to_add_association 'add something', f, :something, :render_options => {:wrapper => 'inline' }
157
+
158
+
152
159
  ### Callbacks (upon insert and remove of items)
153
160
 
154
161
  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`
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.13
1
+ 1.0.14
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cocoon}
8
- s.version = "1.0.13"
8
+ s.version = "1.0.14"
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-10-12}
12
+ s.date = %q{2011-10-22}
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 = [
@@ -31,8 +31,9 @@ module Cocoon
31
31
  end
32
32
 
33
33
  # :nodoc:
34
- def render_association(association, f, new_object)
35
- f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
34
+ def render_association(association, f, new_object, render_options={})
35
+ method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for)
36
+ f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
36
37
  render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
37
38
  end
38
39
  end
@@ -57,12 +58,15 @@ module Cocoon
57
58
  association = args[2]
58
59
  html_options = args[3] || {}
59
60
 
61
+ render_options = html_options.delete(:render_options)
62
+ render_options ||={}
63
+
60
64
  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
61
65
  html_options[:'data-association'] = association.to_s.singularize
62
66
  html_options[:'data-associations'] = association.to_s.pluralize
63
67
 
64
68
  new_object = f.object.class.reflect_on_association(association).klass.new
65
- html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object)).html_safe
69
+ html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe
66
70
 
67
71
  link_to(name, '#', html_options )
68
72
  end
@@ -52,7 +52,48 @@ describe Cocoon do
52
52
  result = @tester.link_to_add_association('add something', @form_obj, :people)
53
53
  result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
54
54
  end
55
+ end
56
+
57
+ it "tttt" do
58
+ @post.class.reflect_on_association(:people).klass.new.should be_a(Person)
59
+ end
60
+
61
+ context "with extra render-options for rendering the child relation" do
62
+ it "should use the correct plural" do
63
+ @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'})
64
+ result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'})
65
+ result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
66
+ end
67
+ end
68
+
69
+ context "when using formtastic" do
70
+ before(:each) do
71
+ @tester.unstub(:render_association)
72
+ @form_obj.stub(:semantic_fields_for).and_return('form<tagzzz>')
73
+ end
74
+ it "calls semantic_fields_for and not fields_for" do
75
+ @form_obj.should_receive(:semantic_fields_for)
76
+ @form_obj.should_receive(:fields_for).never
77
+ result = @tester.link_to_add_association('add something', @form_obj, :people)
78
+ result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tagzzz&gt;">add something</a>'
55
79
 
80
+ end
81
+ end
82
+ context "when using simple_form" do
83
+ before(:each) do
84
+ @tester.unstub(:render_association)
85
+ @form_obj.stub(:simple_fields_for).and_return('form<tagxxx>')
86
+ end
87
+ it "responds_to :simple_fields_for" do
88
+ @form_obj.should respond_to(:simple_fields_for)
89
+ end
90
+ it "calls simple_fields_for and not fields_for" do
91
+ @form_obj.should_receive(:simple_fields_for)
92
+ @form_obj.should_receive(:fields_for).never
93
+ result = @tester.link_to_add_association('add something', @form_obj, :people)
94
+ result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tagxxx&gt;">add something</a>'
95
+
96
+ end
56
97
  end
57
98
 
58
99
  end
@@ -1,2 +1,3 @@
1
1
  class Person < ActiveRecord::Base
2
+ belongs_to :post
2
3
  end
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 13
10
- version: 1.0.13
9
+ - 14
10
+ version: 1.0.14
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-10-12 00:00:00 +02:00
18
+ date: 2011-10-22 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency