cocoon 1.0.15 → 1.0.16

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.
data/README.markdown CHANGED
@@ -121,7 +121,7 @@ and inside the `_task_fields` partial we write:
121
121
 
122
122
  That is all there is to it!
123
123
 
124
- There is an example project on github implementing it called [formtastic-cocoon-demo](https://github.com/nathanvda/formtastic_cocoon_demo).
124
+ There is an example project on github implementing it called [cocoon_formtastic_demo](https://github.com/nathanvda/cocoon_formtastic_demo).
125
125
 
126
126
  ### Using simple_form
127
127
 
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  require 'rake'
10
- require 'rake/rdoctask'
10
+ require 'rdoc/task'
11
11
 
12
12
  require 'rspec/core'
13
13
  require 'rspec/core/rake_task'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.15
1
+ 1.0.16
data/cocoon.gemspec CHANGED
@@ -4,21 +4,20 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{cocoon}
8
- s.version = "1.0.15"
7
+ s.name = "cocoon"
8
+ s.version = "1.0.16"
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-12-08}
13
- s.description = %q{Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.}
14
- s.email = %q{nathan@dixis.com}
12
+ s.date = "2012-03-12"
13
+ s.description = "Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven."
14
+ s.email = "nathan@dixis.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.markdown"
17
17
  ]
18
18
  s.files = [
19
19
  ".travis.yml",
20
20
  "Gemfile",
21
- "Gemfile.lock",
22
21
  "History.md",
23
22
  "MIT-LICENSE",
24
23
  "README.markdown",
@@ -72,10 +71,10 @@ Gem::Specification.new do |s|
72
71
  "spec/integration/navigation_spec.rb",
73
72
  "spec/spec_helper.rb"
74
73
  ]
75
- s.homepage = %q{http://github.com/nathanvda/cocoon}
74
+ s.homepage = "http://github.com/nathanvda/cocoon"
76
75
  s.require_paths = ["lib"]
77
- s.rubygems_version = %q{1.6.2}
78
- s.summary = %q{gem that enables easier nested forms with standard forms, formtastic and simple-form}
76
+ s.rubygems_version = "1.8.15"
77
+ s.summary = "gem that enables easier nested forms with standard forms, formtastic and simple-form"
79
78
 
80
79
  if s.respond_to? :specification_version then
81
80
  s.specification_version = 3
@@ -31,10 +31,11 @@ module Cocoon
31
31
  end
32
32
 
33
33
  # :nodoc:
34
- def render_association(association, f, new_object, render_options={})
34
+ def render_association(association, f, new_object, render_options={}, custom_partial=nil)
35
+ partial = setup_partial(custom_partial, association)
35
36
  method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for)
36
37
  f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
37
- render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
38
+ render(partial, :f => builder, :dynamic => true)
38
39
  end
39
40
  end
40
41
 
@@ -51,26 +52,46 @@ module Cocoon
51
52
  f = args[0]
52
53
  association = args[1]
53
54
  html_options = args[2] || {}
55
+ options = args[3] || {}
54
56
  link_to_add_association(capture(&block), f, association, html_options)
55
57
  else
56
58
  name = args[0]
57
59
  f = args[1]
58
60
  association = args[2]
59
61
  html_options = args[3] || {}
62
+ options = args[4] || {}
60
63
 
61
- render_options = html_options.delete(:render_options)
62
- render_options ||={}
64
+ render_options = html_options.delete(:render_options)
65
+ render_options ||= {}
63
66
 
64
67
  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
65
68
  html_options[:'data-association'] = association.to_s.singularize
66
69
  html_options[:'data-associations'] = association.to_s.pluralize
67
70
 
68
- new_object = f.object.class.reflect_on_association(association).klass.new
69
- html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe
71
+ new_object = create_object(f, association)
72
+ html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).html_safe
70
73
 
71
74
  link_to(name, '#', html_options )
72
75
  end
73
76
  end
74
77
 
78
+ # creates new association object with its conditions, like
79
+ # `` has_many :admin_comments, class_name: "Comment", conditions: { author: "Admin" }
80
+ # will create new Comment with author "Admin"
81
+
82
+ def create_object(f, association)
83
+ assoc = f.object.class.reflect_on_association(association)
84
+ conditions = assoc.conditions.flatten
85
+ new_object = assoc.klass.new(*conditions)
86
+ end
87
+
88
+ def setup_partial(partial, association)
89
+ if partial
90
+ partial
91
+ else
92
+ association.to_s.singularize + "_fields"
93
+ end
94
+ end
95
+
75
96
  end
76
97
  end
data/spec/cocoon_spec.rb CHANGED
@@ -19,12 +19,12 @@ describe Cocoon do
19
19
  end
20
20
 
21
21
  context "without a block" do
22
- it "should accept a name" do
22
+ it "accepts a name" do
23
23
  result = @tester.link_to_add_association('add something', @form_obj, :comments)
24
24
  result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>'
25
25
  end
26
26
 
27
- it "should accept html options and pass them to link_to" do
27
+ it "accepts html options and pass them to link_to" do
28
28
  result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
29
29
  result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>'
30
30
  end
@@ -38,7 +38,7 @@ describe Cocoon do
38
38
  result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">some long name</a>'
39
39
  end
40
40
 
41
- it "should accept html options and pass them to link_to" do
41
+ it "accepts html options and pass them to link_to" do
42
42
  result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do
43
43
  "some long name"
44
44
  end
@@ -48,7 +48,7 @@ describe Cocoon do
48
48
  end
49
49
 
50
50
  context "with an irregular plural" do
51
- it "should use the correct plural" do
51
+ it "uses the correct plural" 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
@@ -59,8 +59,8 @@ describe Cocoon do
59
59
  end
60
60
 
61
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'})
62
+ it "uses the correct plural" do
63
+ @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'}, nil)
64
64
  result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'})
65
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
66
  end
@@ -105,13 +105,13 @@ describe Cocoon do
105
105
  @form_obj = stub(:object => @post, :object_name => @post.class.name)
106
106
  end
107
107
 
108
- context "without a block" do
109
- it "should accept a name" do
108
+ context "without a block" do
109
+ it "accepts a name" do
110
110
  result = @tester.link_to_remove_association('remove something', @form_obj)
111
111
  result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"remove_fields dynamic\">remove something</a>"
112
112
  end
113
113
 
114
- it "should accept html options and pass them to link_to" do
114
+ it "accepts html options and pass them to link_to" do
115
115
  result = @tester.link_to_remove_association('remove something', @form_obj, {:class => 'add_some_class', :'data-something' => 'bla'})
116
116
  result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"add_some_class remove_fields dynamic\" data-something=\"bla\">remove something</a>"
117
117
  end
@@ -126,13 +126,31 @@ describe Cocoon do
126
126
  result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"remove_fields dynamic\">remove some long name</a>"
127
127
  end
128
128
 
129
- it "should accept html options and pass them to link_to" do
129
+ it "accepts html options and pass them to link_to" do
130
130
  result = @tester.link_to_remove_association(@form_obj, {:class => 'add_some_class', :'data-something' => 'bla'}) do
131
131
  "remove some long name"
132
132
  end
133
133
  result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"add_some_class remove_fields dynamic\" data-something=\"bla\">remove some long name</a>"
134
134
  end
135
135
  end
136
+
137
+ context "association with conditions" do
138
+ it "should create correct association" do
139
+ result = @tester.create_object(@form_obj, :admin_comments)
140
+ result.author.should == "Admin"
141
+ end
142
+ end
143
+
144
+ context "setup_partial" do
145
+ it "generates the default partial name if no partial given" do
146
+ result = @tester.setup_partial(nil, :admin_comments)
147
+ result.should == "admin_comment_fields"
148
+ end
149
+ it "uses the given partial name" do
150
+ result = @tester.setup_partial("comment_fields", :admin_comments)
151
+ result.should == "comment_fields"
152
+ end
153
+ end
136
154
  end
137
155
 
138
156
  end
@@ -1,4 +1,5 @@
1
1
  class Post < ActiveRecord::Base
2
2
  has_many :comments
3
+ has_many :admin_comments, :class_name => "Comment", :conditions => { :author => "Admin" }
3
4
  has_many :people
4
5
  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: 9
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 15
10
- version: 1.0.15
9
+ - 16
10
+ version: 1.0.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nathan Van der Auwera
@@ -15,11 +15,9 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-08 00:00:00 +01:00
19
- default_executable:
18
+ date: 2012-03-12 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- type: :development
23
21
  requirement: &id001 !ruby/object:Gem::Requirement
24
22
  none: false
25
23
  requirements:
@@ -31,11 +29,11 @@ dependencies:
31
29
  - 0
32
30
  - 0
33
31
  version: 3.0.0
34
- name: rails
35
32
  version_requirements: *id001
33
+ name: rails
36
34
  prerelease: false
37
- - !ruby/object:Gem::Dependency
38
35
  type: :development
36
+ - !ruby/object:Gem::Dependency
39
37
  requirement: &id002 !ruby/object:Gem::Requirement
40
38
  none: false
41
39
  requirements:
@@ -45,11 +43,11 @@ dependencies:
45
43
  segments:
46
44
  - 0
47
45
  version: "0"
48
- name: sqlite3-ruby
49
46
  version_requirements: *id002
47
+ name: sqlite3-ruby
50
48
  prerelease: false
51
- - !ruby/object:Gem::Dependency
52
49
  type: :development
50
+ - !ruby/object:Gem::Dependency
53
51
  requirement: &id003 !ruby/object:Gem::Requirement
54
52
  none: false
55
53
  requirements:
@@ -59,11 +57,11 @@ dependencies:
59
57
  segments:
60
58
  - 0
61
59
  version: "0"
62
- name: json_pure
63
60
  version_requirements: *id003
61
+ name: json_pure
64
62
  prerelease: false
65
- - !ruby/object:Gem::Dependency
66
63
  type: :development
64
+ - !ruby/object:Gem::Dependency
67
65
  requirement: &id004 !ruby/object:Gem::Requirement
68
66
  none: false
69
67
  requirements:
@@ -73,11 +71,11 @@ dependencies:
73
71
  segments:
74
72
  - 0
75
73
  version: "0"
76
- name: jeweler
77
74
  version_requirements: *id004
75
+ name: jeweler
78
76
  prerelease: false
79
- - !ruby/object:Gem::Dependency
80
77
  type: :development
78
+ - !ruby/object:Gem::Dependency
81
79
  requirement: &id005 !ruby/object:Gem::Requirement
82
80
  none: false
83
81
  requirements:
@@ -89,11 +87,11 @@ dependencies:
89
87
  - 6
90
88
  - 0
91
89
  version: 2.6.0
92
- name: rspec-rails
93
90
  version_requirements: *id005
91
+ name: rspec-rails
94
92
  prerelease: false
95
- - !ruby/object:Gem::Dependency
96
93
  type: :development
94
+ - !ruby/object:Gem::Dependency
97
95
  requirement: &id006 !ruby/object:Gem::Requirement
98
96
  none: false
99
97
  requirements:
@@ -105,11 +103,11 @@ dependencies:
105
103
  - 6
106
104
  - 0
107
105
  version: 2.6.0
108
- name: rspec
109
106
  version_requirements: *id006
107
+ name: rspec
110
108
  prerelease: false
111
- - !ruby/object:Gem::Dependency
112
109
  type: :development
110
+ - !ruby/object:Gem::Dependency
113
111
  requirement: &id007 !ruby/object:Gem::Requirement
114
112
  none: false
115
113
  requirements:
@@ -121,11 +119,11 @@ dependencies:
121
119
  - 0
122
120
  - 0
123
121
  version: 3.0.0
124
- name: actionpack
125
122
  version_requirements: *id007
123
+ name: actionpack
126
124
  prerelease: false
127
- - !ruby/object:Gem::Dependency
128
125
  type: :development
126
+ - !ruby/object:Gem::Dependency
129
127
  requirement: &id008 !ruby/object:Gem::Requirement
130
128
  none: false
131
129
  requirements:
@@ -135,11 +133,11 @@ dependencies:
135
133
  segments:
136
134
  - 0
137
135
  version: "0"
138
- name: simplecov
139
136
  version_requirements: *id008
137
+ name: simplecov
140
138
  prerelease: false
141
- - !ruby/object:Gem::Dependency
142
139
  type: :development
140
+ - !ruby/object:Gem::Dependency
143
141
  requirement: &id009 !ruby/object:Gem::Requirement
144
142
  none: false
145
143
  requirements:
@@ -149,11 +147,11 @@ dependencies:
149
147
  segments:
150
148
  - 0
151
149
  version: "0"
152
- name: generator_spec
153
150
  version_requirements: *id009
151
+ name: generator_spec
154
152
  prerelease: false
155
- - !ruby/object:Gem::Dependency
156
153
  type: :development
154
+ - !ruby/object:Gem::Dependency
157
155
  requirement: &id010 !ruby/object:Gem::Requirement
158
156
  none: false
159
157
  requirements:
@@ -165,9 +163,10 @@ dependencies:
165
163
  - 0
166
164
  - 0
167
165
  version: 2.0.0
168
- name: rspec
169
166
  version_requirements: *id010
167
+ name: rspec
170
168
  prerelease: false
169
+ type: :development
171
170
  description: Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.
172
171
  email: nathan@dixis.com
173
172
  executables: []
@@ -179,7 +178,6 @@ extra_rdoc_files:
179
178
  files:
180
179
  - .travis.yml
181
180
  - Gemfile
182
- - Gemfile.lock
183
181
  - History.md
184
182
  - MIT-LICENSE
185
183
  - README.markdown
@@ -232,7 +230,6 @@ files:
232
230
  - spec/generators/install_generator_spec.rb
233
231
  - spec/integration/navigation_spec.rb
234
232
  - spec/spec_helper.rb
235
- has_rdoc: true
236
233
  homepage: http://github.com/nathanvda/cocoon
237
234
  licenses: []
238
235
 
@@ -262,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
259
  requirements: []
263
260
 
264
261
  rubyforge_project:
265
- rubygems_version: 1.6.2
262
+ rubygems_version: 1.8.15
266
263
  signing_key:
267
264
  specification_version: 3
268
265
  summary: gem that enables easier nested forms with standard forms, formtastic and simple-form
data/Gemfile.lock DELETED
@@ -1,125 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- actionmailer (3.1.3)
5
- actionpack (= 3.1.3)
6
- mail (~> 2.3.0)
7
- actionpack (3.1.3)
8
- activemodel (= 3.1.3)
9
- activesupport (= 3.1.3)
10
- builder (~> 3.0.0)
11
- erubis (~> 2.7.0)
12
- i18n (~> 0.6)
13
- rack (~> 1.3.5)
14
- rack-cache (~> 1.1)
15
- rack-mount (~> 0.8.2)
16
- rack-test (~> 0.6.1)
17
- sprockets (~> 2.0.3)
18
- activemodel (3.1.3)
19
- activesupport (= 3.1.3)
20
- builder (~> 3.0.0)
21
- i18n (~> 0.6)
22
- activerecord (3.1.3)
23
- activemodel (= 3.1.3)
24
- activesupport (= 3.1.3)
25
- arel (~> 2.2.1)
26
- tzinfo (~> 0.3.29)
27
- activeresource (3.1.3)
28
- activemodel (= 3.1.3)
29
- activesupport (= 3.1.3)
30
- activesupport (3.1.3)
31
- multi_json (~> 1.0)
32
- arel (2.2.1)
33
- builder (3.0.0)
34
- diff-lcs (1.1.3)
35
- erubis (2.7.0)
36
- generator_spec (0.8.4)
37
- rails (>= 3.0, < 4.0)
38
- rspec-rails
39
- git (1.2.5)
40
- hike (1.2.1)
41
- i18n (0.6.0)
42
- jeweler (1.6.4)
43
- bundler (~> 1.0)
44
- git (>= 1.2.5)
45
- rake
46
- json (1.6.3)
47
- json_pure (1.6.3)
48
- mail (2.3.0)
49
- i18n (>= 0.4.0)
50
- mime-types (~> 1.16)
51
- treetop (~> 1.4.8)
52
- mime-types (1.17.2)
53
- multi_json (1.0.4)
54
- polyglot (0.3.3)
55
- rack (1.3.5)
56
- rack-cache (1.1)
57
- rack (>= 0.4)
58
- rack-mount (0.8.3)
59
- rack (>= 1.0.0)
60
- rack-ssl (1.3.2)
61
- rack
62
- rack-test (0.6.1)
63
- rack (>= 1.0)
64
- rails (3.1.3)
65
- actionmailer (= 3.1.3)
66
- actionpack (= 3.1.3)
67
- activerecord (= 3.1.3)
68
- activeresource (= 3.1.3)
69
- activesupport (= 3.1.3)
70
- bundler (~> 1.0)
71
- railties (= 3.1.3)
72
- railties (3.1.3)
73
- actionpack (= 3.1.3)
74
- activesupport (= 3.1.3)
75
- rack-ssl (~> 1.3.2)
76
- rake (>= 0.8.7)
77
- rdoc (~> 3.4)
78
- thor (~> 0.14.6)
79
- rake (0.9.2.2)
80
- rdoc (3.11)
81
- json (~> 1.4)
82
- rspec (2.7.0)
83
- rspec-core (~> 2.7.0)
84
- rspec-expectations (~> 2.7.0)
85
- rspec-mocks (~> 2.7.0)
86
- rspec-core (2.7.1)
87
- rspec-expectations (2.7.0)
88
- diff-lcs (~> 1.1.2)
89
- rspec-mocks (2.7.0)
90
- rspec-rails (2.7.0)
91
- actionpack (~> 3.0)
92
- activesupport (~> 3.0)
93
- railties (~> 3.0)
94
- rspec (~> 2.7.0)
95
- simplecov (0.5.4)
96
- multi_json (~> 1.0.3)
97
- simplecov-html (~> 0.5.3)
98
- simplecov-html (0.5.3)
99
- sprockets (2.0.3)
100
- hike (~> 1.2)
101
- rack (~> 1.0)
102
- tilt (~> 1.1, != 1.3.0)
103
- sqlite3 (1.3.5)
104
- sqlite3-ruby (1.3.3)
105
- sqlite3 (>= 1.3.3)
106
- thor (0.14.6)
107
- tilt (1.3.3)
108
- treetop (1.4.10)
109
- polyglot
110
- polyglot (>= 0.3.1)
111
- tzinfo (0.3.31)
112
-
113
- PLATFORMS
114
- ruby
115
-
116
- DEPENDENCIES
117
- actionpack (>= 3.0.0)
118
- generator_spec
119
- jeweler
120
- json_pure
121
- rails (>= 3.0.0)
122
- rspec (>= 2.6.0)
123
- rspec-rails (>= 2.6.0)
124
- simplecov
125
- sqlite3-ruby