karsthammer-inherited_resources 1.1.2

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 (36) hide show
  1. data/CHANGELOG +119 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +516 -0
  4. data/Rakefile +43 -0
  5. data/lib/generators/rails/USAGE +10 -0
  6. data/lib/generators/rails/inherited_resources_controller_generator.rb +11 -0
  7. data/lib/generators/rails/templates/controller.rb +5 -0
  8. data/lib/inherited_resources.rb +37 -0
  9. data/lib/inherited_resources/actions.rb +67 -0
  10. data/lib/inherited_resources/base.rb +44 -0
  11. data/lib/inherited_resources/base_helpers.rb +270 -0
  12. data/lib/inherited_resources/belongs_to_helpers.rb +97 -0
  13. data/lib/inherited_resources/blank_slate.rb +12 -0
  14. data/lib/inherited_resources/class_methods.rb +267 -0
  15. data/lib/inherited_resources/dsl.rb +26 -0
  16. data/lib/inherited_resources/polymorphic_helpers.rb +155 -0
  17. data/lib/inherited_resources/responder.rb +6 -0
  18. data/lib/inherited_resources/singleton_helpers.rb +95 -0
  19. data/lib/inherited_resources/url_helpers.rb +188 -0
  20. data/lib/inherited_resources/version.rb +3 -0
  21. data/test/aliases_test.rb +144 -0
  22. data/test/association_chain_test.rb +125 -0
  23. data/test/base_test.rb +278 -0
  24. data/test/belongs_to_test.rb +105 -0
  25. data/test/class_methods_test.rb +132 -0
  26. data/test/customized_base_test.rb +168 -0
  27. data/test/customized_belongs_to_test.rb +76 -0
  28. data/test/defaults_test.rb +70 -0
  29. data/test/nested_belongs_to_test.rb +108 -0
  30. data/test/optional_belongs_to_test.rb +164 -0
  31. data/test/polymorphic_test.rb +186 -0
  32. data/test/redirect_to_test.rb +51 -0
  33. data/test/singleton_test.rb +83 -0
  34. data/test/test_helper.rb +40 -0
  35. data/test/url_helpers_test.rb +665 -0
  36. metadata +142 -0
@@ -0,0 +1,186 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Factory; end
4
+ class Company; end
5
+
6
+ class Employee
7
+ def self.human_name; 'Employee'; end
8
+ end
9
+
10
+ class EmployeesController < InheritedResources::Base
11
+ belongs_to :factory, :company, :polymorphic => true
12
+ end
13
+
14
+ class PolymorphicFactoriesTest < ActionController::TestCase
15
+ tests EmployeesController
16
+
17
+ def setup
18
+ Factory.expects(:find).with('37').returns(mock_factory)
19
+ mock_factory.expects(:employees).returns(Employee)
20
+
21
+ @controller.stubs(:resource_url).returns('/')
22
+ @controller.stubs(:collection_url).returns('/')
23
+ end
24
+
25
+ def test_expose_all_employees_as_instance_variable_on_index
26
+ Employee.expects(:all).returns([mock_employee])
27
+ get :index, :factory_id => '37'
28
+ assert_equal mock_factory, assigns(:factory)
29
+ assert_equal [mock_employee], assigns(:employees)
30
+ end
31
+
32
+ def test_expose_the_requested_employee_on_show
33
+ Employee.expects(:find).with('42').returns(mock_employee)
34
+ get :show, :id => '42', :factory_id => '37'
35
+ assert_equal mock_factory, assigns(:factory)
36
+ assert_equal mock_employee, assigns(:employee)
37
+ end
38
+
39
+ def test_expose_a_new_employee_on_new
40
+ Employee.expects(:build).returns(mock_employee)
41
+ get :new, :factory_id => '37'
42
+ assert_equal mock_factory, assigns(:factory)
43
+ assert_equal mock_employee, assigns(:employee)
44
+ end
45
+
46
+ def test_expose_the_requested_employee_on_edit
47
+ Employee.expects(:find).with('42').returns(mock_employee)
48
+ get :edit, :id => '42', :factory_id => '37'
49
+ assert_equal mock_factory, assigns(:factory)
50
+ assert_equal mock_employee, assigns(:employee)
51
+ assert_response :success
52
+ end
53
+
54
+ def test_expose_a_newly_create_employee_on_create
55
+ Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
56
+ post :create, :factory_id => '37', :employee => {:these => 'params'}
57
+ assert_equal mock_factory, assigns(:factory)
58
+ assert_equal mock_employee, assigns(:employee)
59
+ end
60
+
61
+ def test_update_the_requested_object_on_update
62
+ Employee.expects(:find).with('42').returns(mock_employee)
63
+ mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
64
+ put :update, :id => '42', :factory_id => '37', :employee => {:these => 'params'}
65
+ assert_equal mock_factory, assigns(:factory)
66
+ assert_equal mock_employee, assigns(:employee)
67
+ end
68
+
69
+ def test_the_requested_employee_is_destroyed_on_destroy
70
+ Employee.expects(:find).with('42').returns(mock_employee)
71
+ mock_employee.expects(:destroy)
72
+ delete :destroy, :id => '42', :factory_id => '37'
73
+ assert_equal mock_factory, assigns(:factory)
74
+ assert_equal mock_employee, assigns(:employee)
75
+ end
76
+
77
+ def test_polymorphic_helpers
78
+ mock_factory.stubs(:class).returns(Factory)
79
+
80
+ Employee.expects(:all).returns([mock_employee])
81
+ get :index, :factory_id => '37'
82
+
83
+ assert @controller.send(:parent?)
84
+ assert_equal :factory, assigns(:parent_type)
85
+ assert_equal :factory, @controller.send(:parent_type)
86
+ assert_equal Factory, @controller.send(:parent_class)
87
+ assert_equal mock_factory, assigns(:factory)
88
+ assert_equal mock_factory, @controller.send(:parent)
89
+ end
90
+
91
+ protected
92
+ def mock_factory(stubs={})
93
+ @mock_factory ||= mock(stubs)
94
+ end
95
+
96
+ def mock_employee(stubs={})
97
+ @mock_employee ||= mock(stubs)
98
+ end
99
+ end
100
+
101
+ class PolymorphicCompanyTest < ActionController::TestCase
102
+ tests EmployeesController
103
+
104
+ def setup
105
+ Company.expects(:find).with('37').returns(mock_company)
106
+ mock_company.expects(:employees).returns(Employee)
107
+
108
+ @controller.stubs(:resource_url).returns('/')
109
+ @controller.stubs(:collection_url).returns('/')
110
+ end
111
+
112
+ def test_expose_all_employees_as_instance_variable_on_index
113
+ Employee.expects(:all).returns([mock_employee])
114
+ get :index, :company_id => '37'
115
+ assert_equal mock_company, assigns(:company)
116
+ assert_equal [mock_employee], assigns(:employees)
117
+ end
118
+
119
+ def test_expose_the_requested_employee_on_show
120
+ Employee.expects(:find).with('42').returns(mock_employee)
121
+ get :show, :id => '42', :company_id => '37'
122
+ assert_equal mock_company, assigns(:company)
123
+ assert_equal mock_employee, assigns(:employee)
124
+ end
125
+
126
+ def test_expose_a_new_employee_on_new
127
+ Employee.expects(:build).returns(mock_employee)
128
+ get :new, :company_id => '37'
129
+ assert_equal mock_company, assigns(:company)
130
+ assert_equal mock_employee, assigns(:employee)
131
+ end
132
+
133
+ def test_expose_the_requested_employee_on_edit
134
+ Employee.expects(:find).with('42').returns(mock_employee)
135
+ get :edit, :id => '42', :company_id => '37'
136
+ assert_equal mock_company, assigns(:company)
137
+ assert_equal mock_employee, assigns(:employee)
138
+ assert_response :success
139
+ end
140
+
141
+ def test_expose_a_newly_create_employee_on_create
142
+ Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
143
+ post :create, :company_id => '37', :employee => {:these => 'params'}
144
+ assert_equal mock_company, assigns(:company)
145
+ assert_equal mock_employee, assigns(:employee)
146
+ end
147
+
148
+ def test_update_the_requested_object_on_update
149
+ Employee.expects(:find).with('42').returns(mock_employee)
150
+ mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
151
+ put :update, :id => '42', :company_id => '37', :employee => {:these => 'params'}
152
+ assert_equal mock_company, assigns(:company)
153
+ assert_equal mock_employee, assigns(:employee)
154
+ end
155
+
156
+ def test_the_requested_employee_is_destroyed_on_destroy
157
+ Employee.expects(:find).with('42').returns(mock_employee)
158
+ mock_employee.expects(:destroy)
159
+ delete :destroy, :id => '42', :company_id => '37'
160
+ assert_equal mock_company, assigns(:company)
161
+ assert_equal mock_employee, assigns(:employee)
162
+ end
163
+
164
+ def test_polymorphic_helpers
165
+ mock_company.stubs(:class).returns(Company)
166
+
167
+ Employee.expects(:all).returns([mock_employee])
168
+ get :index, :company_id => '37'
169
+
170
+ assert @controller.send(:parent?)
171
+ assert_equal :company, assigns(:parent_type)
172
+ assert_equal :company, @controller.send(:parent_type)
173
+ assert_equal Company, @controller.send(:parent_class)
174
+ assert_equal mock_company, assigns(:company)
175
+ assert_equal mock_company, @controller.send(:parent)
176
+ end
177
+
178
+ protected
179
+ def mock_company(stubs={})
180
+ @mock_company ||= mock(stubs)
181
+ end
182
+
183
+ def mock_employee(stubs={})
184
+ @mock_employee ||= mock(stubs)
185
+ end
186
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Machine;
4
+ def self.human_name; 'Machine'; end
5
+ end
6
+
7
+ class MachinesController < InheritedResources::Base
8
+ def create
9
+ create!{ complex_url(:create, true, true) }
10
+ end
11
+
12
+ def update
13
+ update!{ complex_url(:update, false, false) }
14
+ end
15
+
16
+ def destroy
17
+ destroy!{ complex_url(:destroy, true, false) }
18
+ end
19
+
20
+ protected
21
+ def complex_url(name, arg2, arg3)
22
+ 'http://test.host/' + name.to_s
23
+ end
24
+ end
25
+
26
+ class RedirectToWithBlockTest < ActionController::TestCase
27
+ tests MachinesController
28
+
29
+ def test_redirect_to_the_given_url_on_create
30
+ Machine.stubs(:new).returns(mock_machine(:save => true))
31
+ post :create
32
+ assert_redirected_to 'http://test.host/create'
33
+ end
34
+
35
+ def test_redirect_to_the_given_url_on_update
36
+ Machine.stubs(:find).returns(mock_machine(:update_attributes => true))
37
+ put :update
38
+ assert_redirected_to 'http://test.host/update'
39
+ end
40
+
41
+ def test_redirect_to_the_given_url_on_destroy
42
+ Machine.stubs(:find).returns(mock_machine(:destroy => true))
43
+ delete :destroy
44
+ assert_redirected_to 'http://test.host/destroy'
45
+ end
46
+
47
+ protected
48
+ def mock_machine(stubs={})
49
+ @mock_machine ||= mock(stubs)
50
+ end
51
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ # This test file is instead to test the how controller flow and actions
4
+ # using a belongs_to association. This is done using mocks a la rspec.
5
+ #
6
+ class Store
7
+ end
8
+
9
+ class Manager
10
+ def self.human_name; 'Manager'; end
11
+ end
12
+
13
+ class ManagersController < InheritedResources::Base
14
+ belongs_to :store, :singleton => true
15
+ end
16
+
17
+ class SingletonTest < ActionController::TestCase
18
+ tests ManagersController
19
+
20
+ def setup
21
+ @controller.stubs(:resource_url).returns('/')
22
+ @controller.stubs(:collection_url).returns('/')
23
+ end
24
+
25
+ def test_expose_the_requested_manager_on_show
26
+ Store.expects(:find).with('37').returns(mock_store)
27
+ mock_store.expects(:manager).returns(mock_manager)
28
+ get :show, :store_id => '37'
29
+ assert_equal mock_store, assigns(:store)
30
+ assert_equal mock_manager, assigns(:manager)
31
+ end
32
+
33
+ def test_expose_a_new_manager_on_new
34
+ Store.expects(:find).with('37').returns(mock_store)
35
+ mock_store.expects(:build_manager).returns(mock_manager)
36
+ get :new, :store_id => '37'
37
+ assert_equal mock_store, assigns(:store)
38
+ assert_equal mock_manager, assigns(:manager)
39
+ end
40
+
41
+ def test_expose_the_requested_manager_on_edit
42
+ Store.expects(:find).with('37').returns(mock_store)
43
+ mock_store.expects(:manager).returns(mock_manager)
44
+ get :edit, :store_id => '37'
45
+ assert_equal mock_store, assigns(:store)
46
+ assert_equal mock_manager, assigns(:manager)
47
+ assert_response :success
48
+ end
49
+
50
+ def test_expose_a_newly_create_manager_on_create
51
+ Store.expects(:find).with('37').returns(mock_store)
52
+ mock_store.expects(:build_manager).with({'these' => 'params'}).returns(mock_manager(:save => true))
53
+ post :create, :store_id => '37', :manager => {:these => 'params'}
54
+ assert_equal mock_store, assigns(:store)
55
+ assert_equal mock_manager, assigns(:manager)
56
+ end
57
+
58
+ def test_update_the_requested_object_on_update
59
+ Store.expects(:find).with('37').returns(mock_store(:manager => mock_manager))
60
+ mock_manager.expects(:update_attributes).with({'these' => 'params'}).returns(true)
61
+ put :update, :store_id => '37', :manager => {:these => 'params'}
62
+ assert_equal mock_store, assigns(:store)
63
+ assert_equal mock_manager, assigns(:manager)
64
+ end
65
+
66
+ def test_the_requested_manager_is_destroyed_on_destroy
67
+ Store.expects(:find).with('37').returns(mock_store)
68
+ mock_store.expects(:manager).returns(mock_manager)
69
+ mock_manager.expects(:destroy)
70
+ delete :destroy, :store_id => '37'
71
+ assert_equal mock_store, assigns(:store)
72
+ assert_equal mock_manager, assigns(:manager)
73
+ end
74
+
75
+ protected
76
+ def mock_store(stubs={})
77
+ @mock_store ||= mock(stubs)
78
+ end
79
+
80
+ def mock_manager(stubs={})
81
+ @mock_manager ||= mock(stubs)
82
+ end
83
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+
6
+ require 'test/unit'
7
+ require 'mocha'
8
+
9
+ ENV["RAILS_ENV"] = "test"
10
+ RAILS_ROOT = "anywhere"
11
+
12
+ require "active_support"
13
+ require "active_model"
14
+ require "action_controller"
15
+ require "rails/railtie"
16
+
17
+ I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
18
+ I18n.reload!
19
+
20
+ class ApplicationController < ActionController::Base; end
21
+
22
+ # Add IR to load path and load the main file
23
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
24
+ require 'inherited_resources'
25
+
26
+ ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
27
+
28
+ InheritedResources::Routes = ActionDispatch::Routing::RouteSet.new
29
+ InheritedResources::Routes.draw do |map|
30
+ map.connect ':controller/:action/:id'
31
+ map.connect ':controller/:action'
32
+ end
33
+
34
+ ActionController::Base.send :include, InheritedResources::Routes.url_helpers
35
+
36
+ class ActiveSupport::TestCase
37
+ setup do
38
+ @routes = InheritedResources::Routes
39
+ end
40
+ end
@@ -0,0 +1,665 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Universe
4
+ extend ActiveModel::Naming
5
+ end
6
+ class UniversesController < InheritedResources::Base
7
+ defaults :singleton => true, :route_instance_name => 'universum'
8
+ end
9
+
10
+ class House
11
+ extend ActiveModel::Naming
12
+ end
13
+ class HousesController < InheritedResources::Base
14
+ end
15
+
16
+ class News
17
+ extend ActiveModel::Naming
18
+ end
19
+ class NewsController < InheritedResources::Base
20
+ end
21
+
22
+ class Backpack
23
+ extend ActiveModel::Naming
24
+ end
25
+ module Admin; end
26
+ class Admin::BackpacksController < InheritedResources::Base
27
+ defaults :route_collection_name => 'tour_backpacks'
28
+ end
29
+
30
+ class Table
31
+ extend ActiveModel::Naming
32
+ end
33
+ class TablesController < InheritedResources::Base
34
+ belongs_to :house
35
+ end
36
+
37
+ class RoomsController < InheritedResources::Base
38
+ belongs_to :house, :route_name => 'big_house'
39
+ end
40
+
41
+ class ChairsController < InheritedResources::Base
42
+ belongs_to :house do
43
+ belongs_to :table
44
+ end
45
+ end
46
+
47
+ class OwnersController < InheritedResources::Base
48
+ singleton_belongs_to :house
49
+ end
50
+
51
+ class Bed
52
+ extend ActiveModel::Naming
53
+ end
54
+ class BedsController < InheritedResources::Base
55
+ optional_belongs_to :house, :building
56
+ end
57
+
58
+ class Sheep
59
+ extend ActiveModel::Naming
60
+ end
61
+ class SheepController < InheritedResources::Base
62
+ belongs_to :news, :table, :polymorphic => true
63
+ end
64
+
65
+ class Desk
66
+ extend ActiveModel::Naming
67
+ end
68
+ module Admin
69
+ class DesksController < InheritedResources::Base
70
+ optional_belongs_to :house
71
+ end
72
+ end
73
+
74
+ class Dish
75
+ extend ActiveModel::Naming
76
+ end
77
+ class DishesController < InheritedResources::Base
78
+ belongs_to :house do
79
+ polymorphic_belongs_to :table, :kitchen
80
+ end
81
+ end
82
+
83
+ class Center
84
+ extend ActiveModel::Naming
85
+ end
86
+ class CentersController < InheritedResources::Base
87
+ acts_as_singleton!
88
+
89
+ belongs_to :house do
90
+ belongs_to :table, :kitchen, :polymorphic => true
91
+ end
92
+ end
93
+
94
+ # Create a TestHelper module with some helpers
95
+ class UrlHelpersTest < ActiveSupport::TestCase
96
+
97
+ def test_url_helpers_on_simple_inherited_resource
98
+ controller = HousesController.new
99
+ controller.instance_variable_set('@house', :house)
100
+
101
+ [:url, :path].each do |path_or_url|
102
+ controller.expects("houses_#{path_or_url}").with({}).once
103
+ controller.send("collection_#{path_or_url}")
104
+
105
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
106
+ controller.send("resource_#{path_or_url}")
107
+
108
+ controller.expects("new_house_#{path_or_url}").with({}).once
109
+ controller.send("new_resource_#{path_or_url}")
110
+
111
+ controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
112
+ controller.send("edit_resource_#{path_or_url}")
113
+
114
+ # With arg
115
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
116
+ controller.send("resource_#{path_or_url}", :arg)
117
+
118
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
119
+ controller.send("resource_#{path_or_url}", :arg)
120
+
121
+ # With options
122
+ controller.expects("house_#{path_or_url}").with(:arg, :page => 1).once
123
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
124
+ end
125
+ end
126
+
127
+ def test_url_helpers_on_simple_inherited_resource_using_uncountable
128
+ controller = NewsController.new
129
+ controller.instance_variable_set('@news', :news)
130
+
131
+ [:url, :path].each do |path_or_url|
132
+ controller.expects("news_index_#{path_or_url}").with({}).once
133
+ controller.send("collection_#{path_or_url}")
134
+
135
+ controller.expects("news_#{path_or_url}").with(:news, {}).once
136
+ controller.send("resource_#{path_or_url}")
137
+
138
+ controller.expects("new_news_#{path_or_url}").with({}).once
139
+ controller.send("new_resource_#{path_or_url}")
140
+
141
+ controller.expects("edit_news_#{path_or_url}").with(:news, {}).once
142
+ controller.send("edit_resource_#{path_or_url}")
143
+
144
+ # With arg
145
+ controller.expects("news_#{path_or_url}").with(:arg, {}).once
146
+ controller.send("resource_#{path_or_url}", :arg)
147
+
148
+ controller.expects("news_#{path_or_url}").with(:arg, {}).once
149
+ controller.send("resource_#{path_or_url}", :arg)
150
+
151
+ # With options
152
+ controller.expects("news_#{path_or_url}").with(:arg, :page => 1).once
153
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
154
+ end
155
+ end
156
+
157
+ def test_url_helpers_on_simple_inherited_namespaced_resource
158
+ controller = Admin::BackpacksController.new
159
+ controller.instance_variable_set('@backpack', :backpack)
160
+
161
+ assert_equal 'admin', controller.class.resources_configuration[:self][:route_prefix]
162
+
163
+ [:url, :path].each do |path_or_url|
164
+ controller.expects("admin_tour_backpacks_#{path_or_url}").with({}).once
165
+ controller.send("collection_#{path_or_url}")
166
+
167
+ controller.expects("admin_backpack_#{path_or_url}").with(:backpack, {}).once
168
+ controller.send("resource_#{path_or_url}")
169
+
170
+ controller.expects("new_admin_backpack_#{path_or_url}").with({}).once
171
+ controller.send("new_resource_#{path_or_url}")
172
+
173
+ controller.expects("edit_admin_backpack_#{path_or_url}").with(:backpack, {}).once
174
+ controller.send("edit_resource_#{path_or_url}")
175
+
176
+ # With arg
177
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
178
+ controller.send("resource_#{path_or_url}", :arg)
179
+
180
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
181
+ controller.send("resource_#{path_or_url}", :arg)
182
+
183
+ # With options
184
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, :page => 1).once
185
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
186
+ end
187
+ end
188
+
189
+ def test_url_helpers_on_simple_inherited_singleton_resource
190
+ controller = UniversesController.new
191
+ controller.instance_variable_set('@universe', :universe)
192
+
193
+ [:url, :path].each do |path_or_url|
194
+ controller.expects("root_#{path_or_url}").with({}).once
195
+ controller.send("collection_#{path_or_url}")
196
+
197
+ controller.expects("universum_#{path_or_url}").with({}).once
198
+ controller.send("resource_#{path_or_url}")
199
+
200
+ controller.expects("new_universum_#{path_or_url}").with({}).once
201
+ controller.send("new_resource_#{path_or_url}")
202
+
203
+ controller.expects("edit_universum_#{path_or_url}").with({}).once
204
+ controller.send("edit_resource_#{path_or_url}")
205
+
206
+ # With options
207
+ # Also tests that argument sent are not used
208
+ controller.expects("universum_#{path_or_url}").with(:page => 1).once
209
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
210
+ end
211
+ end
212
+
213
+ def test_url_helpers_on_belongs_to
214
+ controller = TablesController.new
215
+ controller.instance_variable_set('@house', :house)
216
+ controller.instance_variable_set('@table', :table)
217
+
218
+ [:url, :path].each do |path_or_url|
219
+ controller.expects("house_tables_#{path_or_url}").with(:house, {}).once
220
+ controller.send("collection_#{path_or_url}")
221
+
222
+ controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
223
+ controller.send("resource_#{path_or_url}")
224
+
225
+ controller.expects("new_house_table_#{path_or_url}").with(:house, {}).once
226
+ controller.send("new_resource_#{path_or_url}")
227
+
228
+ controller.expects("edit_house_table_#{path_or_url}").with(:house, :table, {}).once
229
+ controller.send("edit_resource_#{path_or_url}")
230
+
231
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
232
+ controller.send("parent_#{path_or_url}")
233
+
234
+ controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
235
+ controller.send("edit_parent_#{path_or_url}")
236
+
237
+ # With arg
238
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
239
+ controller.send("resource_#{path_or_url}", :arg)
240
+
241
+ controller.expects("edit_house_table_#{path_or_url}").with(:house, :arg, {}).once
242
+ controller.send("edit_resource_#{path_or_url}", :arg)
243
+
244
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
245
+ controller.send("parent_#{path_or_url}", :arg)
246
+
247
+ # With options
248
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, :page => 1).once
249
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
250
+ end
251
+ end
252
+
253
+ def test_url_helpers_on_not_default_belongs_to
254
+ controller = RoomsController.new
255
+ controller.instance_variable_set('@house', :house)
256
+ controller.instance_variable_set('@room', :room)
257
+
258
+ [:url, :path].each do |path_or_url|
259
+ controller.expects("big_house_rooms_#{path_or_url}").with(:house, {}).once
260
+ controller.send("collection_#{path_or_url}")
261
+
262
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :room, {}).once
263
+ controller.send("resource_#{path_or_url}")
264
+
265
+ controller.expects("new_big_house_room_#{path_or_url}").with(:house, {}).once
266
+ controller.send("new_resource_#{path_or_url}")
267
+
268
+ controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :room, {}).once
269
+ controller.send("edit_resource_#{path_or_url}")
270
+
271
+ controller.expects("big_house_#{path_or_url}").with(:house, {}).once
272
+ controller.send("parent_#{path_or_url}")
273
+
274
+ controller.expects("edit_big_house_#{path_or_url}").with(:house, {}).once
275
+ controller.send("edit_parent_#{path_or_url}")
276
+
277
+ # With args
278
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, {}).once
279
+ controller.send("resource_#{path_or_url}", :arg)
280
+
281
+ controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :arg, {}).once
282
+ controller.send("edit_resource_#{path_or_url}", :arg)
283
+
284
+ controller.expects("big_house_#{path_or_url}").with(:arg, {}).once
285
+ controller.send("parent_#{path_or_url}", :arg)
286
+
287
+ # With options
288
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, :page => 1).once
289
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
290
+ end
291
+ end
292
+
293
+ def test_url_helpers_on_nested_belongs_to
294
+ controller = ChairsController.new
295
+ controller.instance_variable_set('@house', :house)
296
+ controller.instance_variable_set('@table', :table)
297
+ controller.instance_variable_set('@chair', :chair)
298
+
299
+ [:url, :path].each do |path_or_url|
300
+ controller.expects("house_table_chairs_#{path_or_url}").with(:house, :table, {}).once
301
+ controller.send("collection_#{path_or_url}")
302
+
303
+ controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
304
+ controller.send("resource_#{path_or_url}")
305
+
306
+ controller.expects("new_house_table_chair_#{path_or_url}").with(:house, :table, {}).once
307
+ controller.send("new_resource_#{path_or_url}")
308
+
309
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
310
+ controller.send("edit_resource_#{path_or_url}")
311
+
312
+ controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
313
+ controller.send("parent_#{path_or_url}")
314
+
315
+ controller.expects("edit_house_table_#{path_or_url}").with(:house, :table, {}).once
316
+ controller.send("edit_parent_#{path_or_url}")
317
+
318
+ # With args
319
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
320
+ controller.send("edit_resource_#{path_or_url}", :arg)
321
+
322
+ controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
323
+ controller.send("resource_#{path_or_url}", :arg)
324
+
325
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
326
+ controller.send("parent_#{path_or_url}", :arg)
327
+
328
+ # With options
329
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, :page => 1).once
330
+ controller.send("edit_resource_#{path_or_url}", :arg, :page => 1)
331
+ end
332
+ end
333
+
334
+ def test_url_helpers_on_singletons_with_belongs_to
335
+ controller = OwnersController.new
336
+ controller.instance_variable_set('@house', :house)
337
+ controller.instance_variable_set('@owner', :owner)
338
+
339
+ [:url, :path].each do |path_or_url|
340
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
341
+ controller.send("collection_#{path_or_url}")
342
+
343
+ controller.expects("house_owner_#{path_or_url}").with(:house, {}).once
344
+ controller.send("resource_#{path_or_url}")
345
+
346
+ controller.expects("new_house_owner_#{path_or_url}").with(:house, {}).once
347
+ controller.send("new_resource_#{path_or_url}")
348
+
349
+ controller.expects("edit_house_owner_#{path_or_url}").with(:house, {}).once
350
+ controller.send("edit_resource_#{path_or_url}")
351
+
352
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
353
+ controller.send("parent_#{path_or_url}")
354
+
355
+ controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
356
+ controller.send("edit_parent_#{path_or_url}")
357
+
358
+ # With options
359
+ # Also tests that argument sent are not used
360
+ controller.expects("house_owner_#{path_or_url}").with(:house, :page => 1).once
361
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
362
+ end
363
+ end
364
+
365
+ def test_url_helpers_on_polymorphic_belongs_to
366
+ house = House.new
367
+ bed = Bed.new
368
+
369
+ new_bed = Bed.new
370
+ Bed.stubs(:new).returns(new_bed)
371
+ new_bed.stubs(:persisted?).returns(false)
372
+
373
+ controller = BedsController.new
374
+ controller.instance_variable_set('@parent_type', :house)
375
+ controller.instance_variable_set('@house', house)
376
+ controller.instance_variable_set('@bed', bed)
377
+
378
+ [:url, :path].each do |path_or_url|
379
+ controller.expects("house_beds_#{path_or_url}").with(house).once
380
+ controller.send("collection_#{path_or_url}")
381
+
382
+ controller.expects("house_bed_#{path_or_url}").with(house, bed).once
383
+ controller.send("resource_#{path_or_url}")
384
+
385
+ controller.expects("new_house_bed_#{path_or_url}").with(house).once
386
+ controller.send("new_resource_#{path_or_url}")
387
+
388
+ controller.expects("edit_house_bed_#{path_or_url}").with(house, bed).once
389
+ controller.send("edit_resource_#{path_or_url}")
390
+
391
+ controller.expects("house_#{path_or_url}").with(house).once
392
+ controller.send("parent_#{path_or_url}")
393
+
394
+ controller.expects("edit_house_#{path_or_url}").with(house).once
395
+ controller.send("edit_parent_#{path_or_url}")
396
+ end
397
+
398
+ # With options
399
+ controller.expects("house_bed_url").with(house, bed, :page => 1).once
400
+ controller.send("resource_url", :page => 1)
401
+
402
+ controller.expects("house_url").with(house, :page => 1).once
403
+ controller.send("parent_url", :page => 1)
404
+
405
+ # With args
406
+ controller.expects("polymorphic_url").with([:arg, new_bed], {}).once
407
+ controller.send("collection_url", :arg)
408
+
409
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
410
+ controller.send("resource_url", :arg)
411
+
412
+ controller.expects("edit_polymorphic_url").with([house, :arg], {}).once
413
+ controller.send("edit_resource_url", :arg)
414
+
415
+ controller.expects("polymorphic_url").with([:arg], {}).once
416
+ controller.send("parent_url", :arg)
417
+ end
418
+
419
+ def test_url_helpers_on_polymorphic_belongs_to_using_uncountable
420
+ sheep = Sheep.new
421
+ news = News.new
422
+
423
+ new_sheep = Sheep.new
424
+ Sheep.stubs(:new).returns(new_sheep)
425
+ new_sheep.stubs(:persisted?).returns(false)
426
+
427
+ controller = SheepController.new
428
+ controller.instance_variable_set('@parent_type', :news)
429
+ controller.instance_variable_set('@news', news)
430
+ controller.instance_variable_set('@sheep', sheep)
431
+
432
+ [:url, :path].each do |path_or_url|
433
+ controller.expects("news_sheep_index_#{path_or_url}").with(news).once
434
+ controller.send("collection_#{path_or_url}")
435
+
436
+ controller.expects("news_sheep_#{path_or_url}").with(news, sheep).once
437
+ controller.send("resource_#{path_or_url}")
438
+
439
+ controller.expects("new_news_sheep_#{path_or_url}").with(news).once
440
+ controller.send("new_resource_#{path_or_url}")
441
+
442
+ controller.expects("edit_news_sheep_#{path_or_url}").with(news, sheep).once
443
+ controller.send("edit_resource_#{path_or_url}")
444
+
445
+ controller.expects("news_#{path_or_url}").with(news).once
446
+ controller.send("parent_#{path_or_url}")
447
+
448
+ controller.expects("edit_news_#{path_or_url}").with(news).once
449
+ controller.send("edit_parent_#{path_or_url}")
450
+ end
451
+
452
+ # With options
453
+ controller.expects("news_sheep_url").with(news, sheep, :page => 1).once
454
+ controller.send("resource_url", :page => 1)
455
+
456
+ controller.expects("news_url").with(news, :page => 1).once
457
+ controller.send("parent_url", :page => 1)
458
+
459
+ # With args
460
+ controller.expects("polymorphic_url").with([:arg, new_sheep], {}).once
461
+ controller.send("collection_url", :arg)
462
+
463
+ controller.expects("polymorphic_url").with([news, :arg], {}).once
464
+ controller.send("resource_url", :arg)
465
+
466
+ controller.expects("edit_polymorphic_url").with([news, :arg], {}).once
467
+ controller.send("edit_resource_url", :arg)
468
+
469
+ controller.expects("polymorphic_url").with([:arg], {}).once
470
+ controller.send("parent_url", :arg)
471
+ end
472
+
473
+ def test_url_helpers_on_namespaced_polymorphic_belongs_to
474
+ house = House.new
475
+ desk = Desk.new
476
+
477
+ new_desk = Desk.new
478
+ Desk.stubs(:new).returns(new_desk)
479
+ new_desk.stubs(:persisted?).returns(false)
480
+
481
+ controller = Admin::DesksController.new
482
+ controller.instance_variable_set('@parent_type', :house)
483
+ controller.instance_variable_set('@house', house)
484
+ controller.instance_variable_set('@desk', desk)
485
+
486
+ [:url, :path].each do |path_or_url|
487
+ controller.expects("admin_house_desks_#{path_or_url}").with(house).once
488
+ controller.send("collection_#{path_or_url}")
489
+
490
+ controller.expects("admin_house_desk_#{path_or_url}").with(house, desk).once
491
+ controller.send("resource_#{path_or_url}")
492
+
493
+ controller.expects("new_admin_house_desk_#{path_or_url}").with(house).once
494
+ controller.send("new_resource_#{path_or_url}")
495
+
496
+ controller.expects("edit_admin_house_desk_#{path_or_url}").with(house, desk).once
497
+ controller.send("edit_resource_#{path_or_url}")
498
+
499
+ controller.expects("admin_house_#{path_or_url}").with(house).once
500
+ controller.send("parent_#{path_or_url}")
501
+
502
+ controller.expects("edit_admin_house_#{path_or_url}").with(house).once
503
+ controller.send("edit_parent_#{path_or_url}")
504
+ end
505
+
506
+ # With options
507
+ controller.expects("admin_house_desk_url").with(house, desk, :page => 1).once
508
+ controller.send("resource_url", :page => 1)
509
+
510
+ controller.expects("admin_house_url").with(house, :page => 1).once
511
+ controller.send("parent_url", :page => 1)
512
+
513
+ # With args
514
+ controller.expects("polymorphic_url").with(['admin', :arg, new_desk], {}).once
515
+ controller.send("collection_url", :arg)
516
+
517
+ controller.expects("polymorphic_url").with(['admin', house, :arg], {}).once
518
+ controller.send("resource_url", :arg)
519
+
520
+ controller.expects("edit_polymorphic_url").with(['admin', house, :arg], {}).once
521
+ controller.send("edit_resource_url", :arg)
522
+
523
+ controller.expects("polymorphic_url").with(['admin', :arg], {}).once
524
+ controller.send("parent_url", :arg)
525
+ end
526
+
527
+ def test_url_helpers_on_nested_polymorphic_belongs_to
528
+ house = House.new
529
+ table = Table.new
530
+ dish = Dish.new
531
+
532
+ new_dish = Dish.new
533
+ Dish.stubs(:new).returns(new_dish)
534
+ new_dish.stubs(:persisted?).returns(false)
535
+
536
+ controller = DishesController.new
537
+ controller.instance_variable_set('@parent_type', :table)
538
+ controller.instance_variable_set('@house', house)
539
+ controller.instance_variable_set('@table', table)
540
+ controller.instance_variable_set('@dish', dish)
541
+
542
+ [:url, :path].each do |path_or_url|
543
+ controller.expects("house_table_dishes_#{path_or_url}").with(house, table).once
544
+ controller.send("collection_#{path_or_url}")
545
+
546
+ controller.expects("house_table_dish_#{path_or_url}").with(house, table, dish).once
547
+ controller.send("resource_#{path_or_url}")
548
+
549
+ controller.expects("new_house_table_dish_#{path_or_url}").with(house, table).once
550
+ controller.send("new_resource_#{path_or_url}")
551
+
552
+ controller.expects("edit_house_table_dish_#{path_or_url}").with(house, table, dish).once
553
+ controller.send("edit_resource_#{path_or_url}")
554
+
555
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
556
+ controller.send("parent_#{path_or_url}")
557
+
558
+ controller.expects("edit_house_table_#{path_or_url}").with(house, table).once
559
+ controller.send("edit_parent_#{path_or_url}")
560
+ end
561
+
562
+ # With options
563
+ controller.expects("house_table_dish_url").with(house, table, dish, :page => 1).once
564
+ controller.send("resource_url", :page => 1)
565
+
566
+ controller.expects("house_table_url").with(house, table, :page => 1).once
567
+ controller.send("parent_url", :page => 1)
568
+
569
+ # With args
570
+ controller.expects("polymorphic_url").with([house, table, :arg], {}).once
571
+ controller.send("resource_url", :arg)
572
+
573
+ controller.expects("edit_polymorphic_url").with([house, table, :arg], {}).once
574
+ controller.send("edit_resource_url", :arg)
575
+
576
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
577
+ controller.send("parent_url", :arg)
578
+ end
579
+
580
+ def test_url_helpers_on_singleton_nested_polymorphic_belongs_to
581
+ # This must not be usefull in singleton controllers...
582
+ # Center.new
583
+ house = House.new
584
+ table = Table.new
585
+
586
+ controller = CentersController.new
587
+ controller.instance_variable_set('@parent_type', :table)
588
+ controller.instance_variable_set('@house', house)
589
+ controller.instance_variable_set('@table', table)
590
+
591
+ # This must not be useful in singleton controllers...
592
+ # controller.instance_variable_set('@center', :center)
593
+
594
+ [:url, :path].each do |path_or_url|
595
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
596
+ controller.send("collection_#{path_or_url}")
597
+
598
+ controller.expects("house_table_center_#{path_or_url}").with(house, table).once
599
+ controller.send("resource_#{path_or_url}")
600
+
601
+ controller.expects("new_house_table_center_#{path_or_url}").with(house, table).once
602
+ controller.send("new_resource_#{path_or_url}")
603
+
604
+ controller.expects("edit_house_table_center_#{path_or_url}").with(house, table).once
605
+ controller.send("edit_resource_#{path_or_url}")
606
+
607
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
608
+ controller.send("parent_#{path_or_url}")
609
+
610
+ controller.expects("edit_house_table_#{path_or_url}").with(house, table).once
611
+ controller.send("edit_parent_#{path_or_url}")
612
+ end
613
+
614
+ # With options
615
+ controller.expects("house_table_center_url").with(house, table, :page => 1)
616
+ controller.send("resource_url", :page => 1)
617
+
618
+ controller.expects("house_table_url").with(house, table, :page => 1)
619
+ controller.send("parent_url", :page => 1)
620
+
621
+ # With args
622
+ controller.expects("polymorphic_url").with([house, table, :center], {}).once
623
+ controller.send("resource_url", :arg)
624
+
625
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
626
+ controller.send("parent_url", :arg)
627
+ end
628
+
629
+ def test_url_helpers_on_optional_polymorphic_belongs_to
630
+ bed = Bed.new
631
+ new_bed = Bed.new
632
+ Bed.stubs(:new).returns(new_bed)
633
+ new_bed.stubs(:persisted?).returns(false)
634
+
635
+ controller = BedsController.new
636
+ controller.instance_variable_set('@parent_type', nil)
637
+ controller.instance_variable_set('@bed', bed)
638
+
639
+ [:url, :path].each do |path_or_url|
640
+ controller.expects("beds_#{path_or_url}").with().once
641
+ controller.send("collection_#{path_or_url}")
642
+
643
+ controller.expects("bed_#{path_or_url}").with(bed).once
644
+ controller.send("resource_#{path_or_url}")
645
+
646
+ controller.expects("new_bed_#{path_or_url}").with().once
647
+ controller.send("new_resource_#{path_or_url}")
648
+
649
+ controller.expects("edit_bed_#{path_or_url}").with(bed).once
650
+ controller.send("edit_resource_#{path_or_url}")
651
+ end
652
+
653
+ # With options
654
+ controller.expects("bed_url").with(bed, :page => 1).once
655
+ controller.send("resource_url", :page => 1)
656
+
657
+ # With args
658
+ controller.expects("polymorphic_url").with([:arg], {}).once
659
+ controller.send("resource_url", :arg)
660
+
661
+ controller.expects("edit_polymorphic_url").with([:arg], {}).once
662
+ controller.send("edit_resource_url", :arg)
663
+ end
664
+
665
+ end