super_resources 1.0.0.rc1

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.
Files changed (78) hide show
  1. data/.DS_Store +0 -0
  2. data/.gitignore +20 -0
  3. data/Gemfile +8 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +215 -0
  6. data/Rakefile +1 -0
  7. data/lib/super_resources/actions.rb +49 -0
  8. data/lib/super_resources/cancan.rb +15 -0
  9. data/lib/super_resources/controller.rb +12 -0
  10. data/lib/super_resources/has_scope.rb +9 -0
  11. data/lib/super_resources/nesting.rb +110 -0
  12. data/lib/super_resources/resources.rb +88 -0
  13. data/lib/super_resources/url_helpers.rb +61 -0
  14. data/lib/super_resources/version.rb +3 -0
  15. data/lib/super_resources.rb +11 -0
  16. data/spec/controllers/actions_nested_spec.rb +115 -0
  17. data/spec/controllers/actions_spec.rb +142 -0
  18. data/spec/controllers/actions_with_blocks_spec.rb +173 -0
  19. data/spec/controllers/paths_spec.rb +114 -0
  20. data/spec/controllers/redirects_spec.rb +39 -0
  21. data/spec/controllers/resources_adapted_spec.rb +192 -0
  22. data/spec/controllers/resources_nested_spec.rb +37 -0
  23. data/spec/controllers/resources_spec.rb +35 -0
  24. data/spec/dummy/.DS_Store +0 -0
  25. data/spec/dummy/README.rdoc +261 -0
  26. data/spec/dummy/Rakefile +7 -0
  27. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  28. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  29. data/spec/dummy/app/controllers/adapted_resources_controller.rb +24 -0
  30. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  31. data/spec/dummy/app/controllers/child_resources_controller.rb +2 -0
  32. data/spec/dummy/app/controllers/grandparent_resources_controller.rb +13 -0
  33. data/spec/dummy/app/controllers/great_grandparent_resources_controller.rb +46 -0
  34. data/spec/dummy/app/controllers/my_adapted_resources_controller.rb +8 -0
  35. data/spec/dummy/app/controllers/parent_resources_controller.rb +2 -0
  36. data/spec/dummy/app/controllers/simple_resources_controller.rb +2 -0
  37. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  38. data/spec/dummy/app/mailers/.gitkeep +0 -0
  39. data/spec/dummy/app/models/.gitkeep +0 -0
  40. data/spec/dummy/app/models/adapted_resource.rb +3 -0
  41. data/spec/dummy/app/models/child_resource.rb +5 -0
  42. data/spec/dummy/app/models/grandparent_resource.rb +6 -0
  43. data/spec/dummy/app/models/great_grandparent_resource.rb +3 -0
  44. data/spec/dummy/app/models/my_adapted_resource.rb +2 -0
  45. data/spec/dummy/app/models/parent_resource.rb +6 -0
  46. data/spec/dummy/app/models/simple_resource.rb +2 -0
  47. data/spec/dummy/app/views/application/edit.html.erb +1 -0
  48. data/spec/dummy/app/views/application/index.html.erb +1 -0
  49. data/spec/dummy/app/views/application/new.html.erb +1 -0
  50. data/spec/dummy/app/views/application/show.html.erb +1 -0
  51. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  52. data/spec/dummy/config/application.rb +64 -0
  53. data/spec/dummy/config/boot.rb +10 -0
  54. data/spec/dummy/config/database.yml +25 -0
  55. data/spec/dummy/config/environment.rb +5 -0
  56. data/spec/dummy/config/environments/development.rb +37 -0
  57. data/spec/dummy/config/environments/production.rb +67 -0
  58. data/spec/dummy/config/environments/test.rb +37 -0
  59. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  60. data/spec/dummy/config/initializers/inflections.rb +15 -0
  61. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  62. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  63. data/spec/dummy/config/initializers/session_store.rb +8 -0
  64. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  65. data/spec/dummy/config/locales/en.yml +5 -0
  66. data/spec/dummy/config/routes.rb +17 -0
  67. data/spec/dummy/config.ru +4 -0
  68. data/spec/dummy/db/migrate/20130117225232_create_resources.rb +27 -0
  69. data/spec/dummy/db/schema.rb +40 -0
  70. data/spec/dummy/lib/assets/.gitkeep +0 -0
  71. data/spec/dummy/log/.gitkeep +0 -0
  72. data/spec/dummy/public/404.html +26 -0
  73. data/spec/dummy/public/422.html +26 -0
  74. data/spec/dummy/public/500.html +25 -0
  75. data/spec/dummy/public/favicon.ico +0 -0
  76. data/spec/spec_helper.rb +39 -0
  77. data/super_resources.gemspec +21 -0
  78. metadata +201 -0
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Nested resources' do
4
+ let(:ggp) { GreatGrandparentResource.create! }
5
+ let(:gp) { GrandparentResource.create!(:great_grandparent_resource => ggp) }
6
+ let(:p) { ParentResource.create!(:grandparent_resource => gp) }
7
+ let(:c) { ChildResource.create!(:parent_resource => p) }
8
+ let(:orphan_c) { ChildResource.create! }
9
+
10
+ describe ChildResourcesController do
11
+ describe "RESTful resource actions" do
12
+ it "scopes the resource collection qualified by a parent" do
13
+ get :index, {:parent_resource_id => p.id}
14
+ subject.send(:collection).should eq([c])
15
+ end
16
+
17
+ it "shows a resource qualified by a parent" do
18
+ get :show, {:id => c.to_param, :parent_resource_id => p.id}
19
+ subject.send(:resource).should eq(c)
20
+ end
21
+
22
+ it "qualifies a new resource qualified by a parent" do
23
+ get :new, {:parent_resource_id => p.id}
24
+ subject.send(:resource).should be_a_new(ChildResource)
25
+ subject.send(:resource).parent_resource.should eq(p)
26
+ end
27
+
28
+ it "provide editing for a resource qualified by a parent" do
29
+ get :edit, {:id => c.to_param, :parent_resource_id => p.id}
30
+ subject.send(:resource).should eq(c)
31
+ end
32
+
33
+ it "creates a resource qualified by a parent" do
34
+ expect {
35
+ post :create, {:parent_resource_id => p.id}
36
+ }.to change(ChildResource, :count).by(1)
37
+
38
+ subject.send(:resource).should be_a(ChildResource)
39
+ subject.send(:resource).should be_persisted
40
+
41
+ response.should redirect_to(subject.send(:resource_url, ChildResource.last))
42
+ end
43
+
44
+ it "update a resource qualified by a parent" do
45
+ ChildResource.any_instance.should_receive(:update_attributes).with({'these' => 'params'}).and_return(true)
46
+ put :update, {:id => c.to_param, :child_resource => {'these' => 'params'}, :parent_resource_id => p.id}
47
+
48
+ subject.send(:resource).should eq(c)
49
+ response.should redirect_to(subject.send(:resource_url, c))
50
+ end
51
+
52
+ it "destroys a resource qualified by a parent" do
53
+ d = ChildResource.create!(:parent_resource => p)
54
+ expect {
55
+ delete :destroy, {:id => d.to_param, :parent_resource_id => p.id}
56
+ }.to change(ChildResource, :count).by(-1)
57
+
58
+ response.should redirect_to(subject.send(:collection_url))
59
+ end
60
+ end
61
+ end
62
+
63
+ describe ParentResourcesController do
64
+ describe "nested RESTful resource actions" do
65
+ it "scopes the resource collection qualified by a grandparent" do
66
+ get :index, {:great_grandparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
67
+ subject.send(:collection).should eq([p])
68
+ end
69
+
70
+ it "shows a resource qualified by a grandparent" do
71
+ get :show, {:id => p.to_param, :great_granbparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
72
+ subject.send(:resource).should eq(p)
73
+ end
74
+
75
+ it "qualifies a new resource qualified by a grandparent" do
76
+ get :new, {:great_grandparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
77
+ subject.send(:resource).should be_a_new(ParentResource)
78
+ subject.send(:resource).grandparent_resource.great_grandparent_resource.should eq(ggp)
79
+ end
80
+
81
+ it "provide editing for a resource qualified by a grandparent" do
82
+ get :edit, {:id => p.to_param, :great_granbparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
83
+ subject.send(:resource).should eq(p)
84
+ end
85
+
86
+ it "creates a resource qualified by a grandparent" do
87
+ expect {
88
+ post :create, {:great_grandparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
89
+ }.to change(ParentResource, :count).by(1)
90
+
91
+ subject.send(:resource).should be_a(ParentResource)
92
+ subject.send(:resource).should be_persisted
93
+
94
+ response.should redirect_to(subject.send(:resource_url, ParentResource.last))
95
+ end
96
+
97
+ it "update a resource qualified by a grandparent" do
98
+ ParentResource.any_instance.should_receive(:update_attributes).with({'these' => 'params'}).and_return(true)
99
+ put :update, {:id => p.to_param, :parent_resource => {'these' => 'params'}, :great_granbparent_resource_id => ggp.id, :grandparent_resource_id => gp.id}
100
+
101
+ subject.send(:resource).should eq(p)
102
+ response.should redirect_to(subject.send(:resource_url, p))
103
+ end
104
+
105
+ it "destroys a resource qualified by a grandparent" do
106
+ d = ParentResource.create!(:grandparent_resource => gp)
107
+ expect {
108
+ delete :destroy, {:id => d.to_param, :grandparent_resource_id => gp.id}
109
+ }.to change(ParentResource, :count).by(-1)
110
+
111
+ response.should redirect_to(subject.send(:collection_url))
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleResourcesController do
4
+ describe 'DRYs up RESTful scaffolded actions' do
5
+ def valid_attributes
6
+ {}
7
+ end
8
+
9
+ def valid_session
10
+ {}
11
+ end
12
+
13
+ describe "GET index" do
14
+ it "assigns all simple resources as collection" do
15
+ r = SimpleResource.create! valid_attributes
16
+ get :index, {}, valid_session
17
+ assigns(:collection).should eq([r])
18
+ end
19
+ end
20
+
21
+ describe "GET show" do
22
+ it "assigns the requested resource as resource" do
23
+ r = SimpleResource.create! valid_attributes
24
+ get :show, {:id => r.to_param}, valid_session
25
+ assigns(:resource).should eq(r)
26
+ end
27
+ end
28
+
29
+ describe "GET new" do
30
+ it "assigns a new resource as resource" do
31
+ get :new, {}, valid_session
32
+ assigns(:resource).should be_a_new(SimpleResource)
33
+ end
34
+ end
35
+
36
+ describe "GET edit" do
37
+ it "assigns the requested resource as resource" do
38
+ r = SimpleResource.create! valid_attributes
39
+ get :edit, {:id => r.to_param}, valid_session
40
+ assigns(:resource).should eq(r)
41
+ end
42
+ end
43
+
44
+ describe "POST create" do
45
+ describe "with valid params" do
46
+ it "creates a new resource" do
47
+ expect {
48
+ post :create, {:simple_resource => valid_attributes}, valid_session
49
+ }.to change(SimpleResource, :count).by(1)
50
+ end
51
+
52
+ it "assigns a newly created resource as resource" do
53
+ post :create, {:simple_resource => valid_attributes}, valid_session
54
+ assigns(:resource).should be_a(SimpleResource)
55
+ assigns(:resource).should be_persisted
56
+ end
57
+
58
+ it "redirects to the created resource" do
59
+ post :create, {:simple_resource => valid_attributes}, valid_session
60
+ response.should redirect_to(SimpleResource.last)
61
+ end
62
+ end
63
+
64
+ describe "with invalid params" do
65
+ it "assigns a newly created but unsaved resource as resource" do
66
+ # Trigger the behavior that occurs when invalid params are submitted
67
+ SimpleResource.any_instance.stub(:save).and_return(false)
68
+ post :create, {:simple_resource => {}}, valid_session
69
+ assigns(:resource).should be_a_new(SimpleResource)
70
+ end
71
+
72
+ it "re-renders the 'new' template" do
73
+ # Trigger the behavior that occurs when invalid params are submitted
74
+ SimpleResource.any_instance.stub(:save).and_return(false)
75
+ SimpleResource.any_instance.stub(:errors).and_return(['error'])
76
+ post :create, {:simple_resource => {}}, valid_session
77
+ response.should render_template("new")
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "PUT update" do
83
+ describe "with valid params" do
84
+ it "updates the requested resource" do
85
+ r = SimpleResource.create! valid_attributes
86
+ # Assuming there are no other resources in the database, this
87
+ # specifies that the resource created on the previous line
88
+ # receives the :update_attributes message with whatever params are
89
+ # submitted in the request.
90
+ SimpleResource.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
91
+ put :update, {:id => r.to_param, :simple_resource => {'these' => 'params'}}, valid_session
92
+ end
93
+
94
+ it "assigns the requested resource as resource" do
95
+ r = SimpleResource.create! valid_attributes
96
+ put :update, {:id => r.to_param, :simple_resource => valid_attributes}, valid_session
97
+ assigns(:resource).should eq(r)
98
+ end
99
+
100
+ it "redirects to the resource" do
101
+ r = SimpleResource.create! valid_attributes
102
+ put :update, {:id => r.to_param, :simple_resource => valid_attributes}, valid_session
103
+ response.should redirect_to(r)
104
+ end
105
+ end
106
+
107
+ describe "with invalid params" do
108
+ it "assigns the resource as resource" do
109
+ r = SimpleResource.create! valid_attributes
110
+ # Trigger the behavior that occurs when invalid params are submitted
111
+ SimpleResource.any_instance.stub(:save).and_return(false)
112
+ put :update, {:id => r.to_param, :simple_resource => {}}, valid_session
113
+ assigns(:resource).should eq(r)
114
+ end
115
+
116
+ it "re-renders the 'edit' template" do
117
+ r = SimpleResource.create! valid_attributes
118
+ # Trigger the behavior that occurs when invalid params are submitted
119
+ SimpleResource.any_instance.stub(:save).and_return(false)
120
+ SimpleResource.any_instance.stub(:errors).and_return(['error'])
121
+ put :update, {:id => r.to_param, :simple_resource => {}}, valid_session
122
+ response.should render_template("edit")
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "DELETE destroy" do
128
+ it "destroys the requested resource" do
129
+ r = SimpleResource.create! valid_attributes
130
+ expect {
131
+ delete :destroy, {:id => r.to_param}, valid_session
132
+ }.to change(SimpleResource, :count).by(-1)
133
+ end
134
+
135
+ it "redirects to the resources list" do
136
+ r = SimpleResource.create! valid_attributes
137
+ delete :destroy, {:id => r.to_param}, valid_session
138
+ response.should redirect_to(subject.send :collection_url)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+
3
+ describe GreatGrandparentResourcesController do
4
+ describe "GET index" do
5
+ it "executes a passed block" do
6
+ ggp = GreatGrandparentResource.create!
7
+ get :index
8
+ assigns(:collection).should eq([ggp])
9
+ subject.send(:collection).first.description.should eq('We are all great grandparents')
10
+ end
11
+ end
12
+
13
+ describe "GET show" do
14
+ it "executes a passed block" do
15
+ ggp = GreatGrandparentResource.create!
16
+ get :show, {:id => ggp.to_param}
17
+ assigns(:resource).should eq(ggp)
18
+ subject.send(:resource).description.should eq('I am a great grandparent')
19
+ end
20
+ end
21
+
22
+ describe "GET new" do
23
+ it "executes a passed block" do
24
+ get :new
25
+ assigns(:resource).should be_a_new(GreatGrandparentResource)
26
+ subject.send(:resource).description.should eq('I am becoming a new great grandparent')
27
+ end
28
+ end
29
+
30
+ describe "GET edit" do
31
+ it "executes a passed block" do
32
+ ggp = GreatGrandparentResource.create!
33
+ get :edit, {:id => ggp.to_param}
34
+ assigns(:resource).should eq(ggp)
35
+
36
+ subject.send(:resource).description.should eq('I am becoming a great grandparent again')
37
+ end
38
+ end
39
+
40
+ describe "POST create" do
41
+ it "executes a passed block" do
42
+ post :create
43
+ subject.send(:resource).description.should eq('I am a new great grandparent')
44
+ end
45
+ end
46
+
47
+ describe "PUT update" do
48
+ it "executes a passed block" do
49
+ ggp = GreatGrandparentResource.create!
50
+ GreatGrandparentResource.any_instance.stub(:save).and_return(false)
51
+ put :update, {:id => ggp.to_param}
52
+ subject.send(:resource).description.should eq('I am a great grandparent again')
53
+ end
54
+ end
55
+
56
+ describe "DELETE destroy" do
57
+ it "executes a passed block" do
58
+ ggp = GreatGrandparentResource.create!
59
+ delete :destroy, {:id => ggp.to_param}
60
+ subject.instance_variable_get(:@obituary).should eq('Here lies a dead great grandparent')
61
+ end
62
+ end
63
+
64
+ # check we haven't fuzled any of the rest of the standard REstful action expectations
65
+
66
+ def valid_attributes
67
+ {}
68
+ end
69
+
70
+ def valid_session
71
+ {}
72
+ end
73
+
74
+ describe "POST create" do
75
+ describe "with valid params" do
76
+ it "creates a new resource" do
77
+ expect {
78
+ post :create, {:great_grandparent_resource => valid_attributes}, valid_session
79
+ }.to change(GreatGrandparentResource, :count).by(1)
80
+ end
81
+
82
+ it "assigns a newly created resource as resource" do
83
+ post :create, {:great_grandparent_resource => valid_attributes}, valid_session
84
+ assigns(:resource).should be_a(GreatGrandparentResource)
85
+ assigns(:resource).should be_persisted
86
+ end
87
+
88
+ it "redirects to the created resource" do
89
+ post :create, {:great_grandparent_resource => valid_attributes}, valid_session
90
+ response.should redirect_to(GreatGrandparentResource.last)
91
+ end
92
+ end
93
+
94
+ describe "with invalid params" do
95
+ it "assigns a newly created but unsaved resource as resource" do
96
+ # Trigger the behavior that occurs when invalid params are submitted
97
+ GreatGrandparentResource.any_instance.stub(:save).and_return(false)
98
+ post :create, {:great_grandparent_resource => {}}, valid_session
99
+ assigns(:resource).should be_a_new(GreatGrandparentResource)
100
+ end
101
+
102
+ it "re-renders the 'new' template" do
103
+ # Trigger the behavior that occurs when invalid params are submitted
104
+ GreatGrandparentResource.any_instance.stub(:save).and_return(false)
105
+ GreatGrandparentResource.any_instance.stub(:errors).and_return(['error'])
106
+ post :create, {:great_grandparent_resource => {}}, valid_session
107
+ response.should render_template("new")
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "PUT update" do
113
+ describe "with valid params" do
114
+ it "updates the requested resource" do
115
+ r = GreatGrandparentResource.create! valid_attributes
116
+ # Assuming there are no other resources in the database, this
117
+ # specifies that the resource created on the previous line
118
+ # receives the :update_attributes message with whatever params are
119
+ # submitted in the request.
120
+ GreatGrandparentResource.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
121
+ put :update, {:id => r.to_param, :great_grandparent_resource => {'these' => 'params'}}, valid_session
122
+ end
123
+
124
+ it "assigns the requested resource as resource" do
125
+ r = GreatGrandparentResource.create! valid_attributes
126
+ put :update, {:id => r.to_param, :great_grandparent_resource => valid_attributes}, valid_session
127
+ assigns(:resource).should eq(r)
128
+ end
129
+
130
+ it "redirects to the resource" do
131
+ r = GreatGrandparentResource.create! valid_attributes
132
+ put :update, {:id => r.to_param, :great_grandparent_resource => valid_attributes}, valid_session
133
+ response.should redirect_to(r)
134
+ end
135
+ end
136
+
137
+ describe "with invalid params" do
138
+ it "assigns the resource as resource" do
139
+ r = GreatGrandparentResource.create! valid_attributes
140
+ # Trigger the behavior that occurs when invalid params are submitted
141
+ GreatGrandparentResource.any_instance.stub(:save).and_return(false)
142
+ put :update, {:id => r.to_param, :great_grandparent_resource => {}}, valid_session
143
+ assigns(:resource).should eq(r)
144
+ end
145
+
146
+ it "re-renders the 'edit' template" do
147
+ r = GreatGrandparentResource.create! valid_attributes
148
+ # Trigger the behavior that occurs when invalid params are submitted
149
+ GreatGrandparentResource.any_instance.stub(:save).and_return(false)
150
+ GreatGrandparentResource.any_instance.stub(:errors).and_return(['error'])
151
+ put :update, {:id => r.to_param, :great_grandparent_resource => {}}, valid_session
152
+ response.should render_template("edit")
153
+ end
154
+ end
155
+ end
156
+
157
+ describe "DELETE destroy" do
158
+ it "destroys the requested resource" do
159
+ r = GreatGrandparentResource.create! valid_attributes
160
+ expect {
161
+ delete :destroy, {:id => r.to_param}, valid_session
162
+ }.to change(GreatGrandparentResource, :count).by(-1)
163
+ end
164
+
165
+ it "redirects to the resources list" do
166
+ r = GreatGrandparentResource.create! valid_attributes
167
+ delete :destroy, {:id => r.to_param}, valid_session
168
+ response.should redirect_to(subject.send :collection_url)
169
+ end
170
+ end
171
+
172
+
173
+ end
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleResourcesController do
4
+ describe 'url helpers' do
5
+ it "collection_url derives for you" do
6
+ get :index
7
+ subject.send(:collection_url).should eq('http://test.host/simple_resources')
8
+ end
9
+
10
+ it "resource_url derives for you" do
11
+ r = SimpleResource.create!
12
+ get :index
13
+ subject.send(:resource_url, r).should eq("http://test.host/simple_resources/#{r.id}")
14
+ end
15
+
16
+ it "resource_url assumes the current resource and derives for you" do
17
+ r = SimpleResource.create!
18
+ get :edit, {:id => r.to_param}
19
+ subject.send(:resource_url).should eq("http://test.host/simple_resources/#{r.id}")
20
+ end
21
+
22
+ it "new_resource_url derives for you" do
23
+ get :index
24
+ subject.send(:new_resource_url).should eq('http://test.host/simple_resources/new')
25
+ end
26
+
27
+ it "edit_resource_url derives for you" do
28
+ r = SimpleResource.create!
29
+ get :index
30
+ subject.send(:edit_resource_url, r).should eq("http://test.host/simple_resources/#{r.id}/edit")
31
+ end
32
+
33
+ it "edit_resource_url assumes the current resource and derives path for you" do
34
+ r = SimpleResource.create!
35
+ get :show, {:id => r.to_param}
36
+ subject.send(:edit_resource_url).should eq("http://test.host/simple_resources/#{r.id}/edit")
37
+ end
38
+ end
39
+ end
40
+
41
+ describe ChildResourcesController do
42
+ describe 'url helpers' do
43
+ let(:p) { ParentResource.create! }
44
+ let(:c) { ChildResource.create!(:parent_resource => p) }
45
+
46
+ it "collection_url derives nesting for you" do
47
+ get :index, {:parent_resource_id => p.id}
48
+ subject.send(:collection_url).should eq("http://test.host/parent_resources/#{p.id}/child_resources")
49
+ end
50
+
51
+ it "resource_url derives nesting for you" do
52
+ get :index, {:parent_resource_id => p.id}
53
+ subject.send(:resource_url, c).should eq("http://test.host/parent_resources/#{p.id}/child_resources/#{c.id}")
54
+ end
55
+
56
+ it "resource_url assumes the current resource and derives nesting for you" do
57
+ get :edit, {:id => c.to_param, :parent_resource_id => p.id}
58
+ subject.send(:resource_url).should eq("http://test.host/parent_resources/#{p.id}/child_resources/#{c.id}")
59
+ end
60
+
61
+ it "new_resource_url derives nesting for you" do
62
+ get :index, {:parent_resource_id => p.id}
63
+ subject.send(:new_resource_url).should eq("http://test.host/parent_resources/#{p.id}/child_resources/new")
64
+ end
65
+
66
+ it "edit_resource_url derives nesting for you" do
67
+ get :index, {:parent_resource_id => p.id}
68
+ subject.send(:edit_resource_url, c).should eq("http://test.host/parent_resources/#{p.id}/child_resources/#{c.id}/edit")
69
+ end
70
+
71
+ it "edit_resource_url assumes the current resource and derives nesting for you" do
72
+ get :show, {:id => c.to_param, :parent_resource_id => p.id}
73
+ subject.send(:edit_resource_url).should eq("http://test.host/parent_resources/#{p.id}/child_resources/#{c.id}/edit")
74
+ end
75
+ end
76
+ end
77
+
78
+ describe ParentResourcesController do
79
+ describe 'url helpers' do
80
+ let(:ggp) { GreatGrandparentResource.create! }
81
+ let(:gp) { GrandparentResource.create!(:great_grandparent_resource => ggp) }
82
+ let(:p) { ParentResource.create!(:grandparent_resource => gp) }
83
+
84
+ it "collection_url derives nesting for you" do
85
+ get :index, {:grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
86
+ subject.send(:collection_url).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources")
87
+ end
88
+
89
+ it "resource_url derives nesting for you" do
90
+ get :index, {:grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
91
+ subject.send(:resource_url, p).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources/#{p.id}")
92
+ end
93
+
94
+ it "resource_url assumes the current resource and derives nesting for you" do
95
+ get :edit, {:id => p.id, :grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
96
+ subject.send(:resource_url).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources/#{p.id}")
97
+ end
98
+
99
+ it "new_resource_url derives nesting for you" do
100
+ get :index, {:grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
101
+ subject.send(:new_resource_url).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources/new")
102
+ end
103
+
104
+ it "edit_resource_url derives nesting for you" do
105
+ get :index, {:grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
106
+ subject.send(:edit_resource_url, p).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources/#{p.id}/edit")
107
+ end
108
+
109
+ it "edit_resource_url assumes the current resource and derives nesting for you" do
110
+ get :show, {:id => p.id, :grandparent_resource_id => gp.id, :great_grandparent_resource_id => ggp.id}
111
+ subject.send(:edit_resource_url).should eq("http://test.host/great_grandparent_resources/#{ggp.id}/grandparent_resources/#{gp.id}/parent_resources/#{p.id}/edit")
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'In the context of generic RESTful-ness,' do
4
+ let(:ggp) { GreatGrandparentResource.create! }
5
+ let(:gp) { GrandparentResource.create!(:great_grandparent_resource => ggp) }
6
+
7
+ describe GrandparentResourcesController do
8
+ describe 'overriding redirects' do
9
+ it "redirects from create as adapted" do
10
+ expect {
11
+ post :create, {:great_grandparent_resource_id => ggp.id}
12
+ }.to change(GreatGrandparentResource, :count).by(1)
13
+
14
+ subject.send(:resource).should be_a(GrandparentResource)
15
+ subject.send(:resource).should be_persisted
16
+
17
+ response.should redirect_to(subject.send(:great_grandparent_resource_url, ggp))
18
+ end
19
+
20
+ it "redirects from update as adapted" do
21
+ GrandparentResource.any_instance.should_receive(:update_attributes).with({'these' => 'params'}).and_return(true)
22
+ put :update, {:id => gp.to_param, :grandparent_resource => {'these' => 'params'}, :great_grandparent_resource_id => ggp.id}
23
+
24
+ subject.send(:resource).should eq(gp)
25
+
26
+ response.should redirect_to(subject.send(:great_grandparent_resource_url, ggp))
27
+ end
28
+
29
+ it "redirects from destroy as adapted" do
30
+ d = GrandparentResource.create!(:great_grandparent_resource => ggp)
31
+ expect {
32
+ delete :destroy, {:id => d.to_param, :great_grandparent_resource_id => ggp.id}
33
+ }.to change(GrandparentResource, :count).by(-1)
34
+
35
+ response.should redirect_to(subject.send(:great_grandparent_resource_url, ggp))
36
+ end
37
+ end
38
+ end
39
+ end