inherited_resources 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +6 -0
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +86 -0
  5. data/README.rdoc +91 -37
  6. data/Rakefile +0 -22
  7. data/inherited_resources.gemspec +24 -0
  8. data/lib/inherited_resources.rb +7 -2
  9. data/lib/inherited_resources/actions.rb +4 -4
  10. data/lib/inherited_resources/base.rb +11 -10
  11. data/lib/inherited_resources/base_helpers.rb +32 -3
  12. data/lib/inherited_resources/class_methods.rb +102 -10
  13. data/lib/inherited_resources/shallow_helpers.rb +87 -0
  14. data/lib/inherited_resources/url_helpers.rb +48 -2
  15. data/lib/inherited_resources/version.rb +1 -1
  16. data/test/aliases_test.rb +1 -1
  17. data/test/association_chain_test.rb +1 -1
  18. data/test/base_test.rb +16 -8
  19. data/test/belongs_to_test.rb +44 -2
  20. data/test/belongs_to_with_shallow_test.rb +93 -0
  21. data/test/class_methods_test.rb +6 -4
  22. data/test/customized_base_test.rb +1 -1
  23. data/test/customized_belongs_to_test.rb +2 -2
  24. data/test/customized_redirect_to_test.rb +32 -0
  25. data/test/defaults_test.rb +117 -11
  26. data/test/locales/en.yml +17 -0
  27. data/test/nested_belongs_to_test.rb +1 -1
  28. data/test/nested_belongs_to_with_shallow_test.rb +123 -0
  29. data/test/nested_model_with_shallow_test.rb +133 -0
  30. data/test/optional_belongs_to_test.rb +4 -4
  31. data/test/polymorphic_test.rb +5 -5
  32. data/test/redirect_to_test.rb +1 -1
  33. data/test/singleton_test.rb +5 -2
  34. data/test/test_helper.rb +8 -24
  35. data/test/url_helpers_test.rb +243 -2
  36. data/test/views/cars/edit.html.erb +1 -0
  37. data/test/views/cars/index.html.erb +1 -0
  38. data/test/views/cars/new.html.erb +1 -0
  39. data/test/views/cars/show.html.erb +1 -0
  40. data/test/views/cities/edit.html.erb +1 -0
  41. data/test/views/cities/index.html.erb +1 -0
  42. data/test/views/cities/new.html.erb +1 -0
  43. data/test/views/cities/show.html.erb +1 -0
  44. data/test/views/comments/edit.html.erb +1 -0
  45. data/test/views/comments/index.html.erb +1 -0
  46. data/test/views/comments/new.html.erb +1 -0
  47. data/test/views/comments/show.html.erb +1 -0
  48. data/test/views/educations/new.html.erb +0 -0
  49. data/test/views/employees/edit.html.erb +1 -0
  50. data/test/views/employees/index.html.erb +1 -0
  51. data/test/views/employees/new.html.erb +1 -0
  52. data/test/views/employees/show.html.erb +1 -0
  53. data/test/views/groups/edit.html.erb +0 -0
  54. data/test/views/managers/edit.html.erb +1 -0
  55. data/test/views/managers/new.html.erb +1 -0
  56. data/test/views/managers/show.html.erb +1 -0
  57. data/test/views/painters/edit.html.erb +1 -0
  58. data/test/views/painters/index.html.erb +1 -0
  59. data/test/views/painters/new.html.erb +1 -0
  60. data/test/views/painters/show.html.erb +1 -0
  61. data/test/views/pets/edit.html.erb +1 -0
  62. data/test/views/pets/index.html.erb +1 -0
  63. data/test/views/pets/new.html.erb +1 -0
  64. data/test/views/pets/show.html.erb +1 -0
  65. data/test/views/plates/edit.html.erb +1 -0
  66. data/test/views/plates/index.html.erb +1 -0
  67. data/test/views/plates/new.html.erb +1 -0
  68. data/test/views/plates/show.html.erb +1 -0
  69. data/test/views/products/edit.html.erb +1 -0
  70. data/test/views/products/index.html.erb +1 -0
  71. data/test/views/products/new.html.erb +1 -0
  72. data/test/views/products/show.html.erb +1 -0
  73. data/test/views/professors/edit.html.erb +1 -0
  74. data/test/views/professors/index.html.erb +1 -0
  75. data/test/views/professors/new.html.erb +1 -0
  76. data/test/views/professors/show.html.erb +1 -0
  77. data/test/views/projects/index.html.erb +1 -0
  78. data/test/views/projects/index.json.erb +1 -0
  79. data/test/views/projects/respond_to_skip_default_template.html.erb +1 -0
  80. data/test/views/projects/respond_with_resource.html.erb +1 -0
  81. data/test/views/students/edit.html.erb +1 -0
  82. data/test/views/students/new.html.erb +1 -0
  83. data/test/views/tags/edit.html.erb +1 -0
  84. data/test/views/tags/index.html.erb +1 -0
  85. data/test/views/tags/new.html.erb +1 -0
  86. data/test/views/tags/show.html.erb +1 -0
  87. data/test/views/trees/edit.html.erb +1 -0
  88. data/test/views/trees/index.html.erb +1 -0
  89. data/test/views/trees/new.html.erb +1 -0
  90. data/test/views/trees/show.html.erb +1 -0
  91. data/test/views/university/professors/edit.html.erb +1 -0
  92. data/test/views/university/professors/index.html.erb +1 -0
  93. data/test/views/university/professors/new.html.erb +1 -0
  94. data/test/views/university/professors/show.html.erb +1 -0
  95. data/test/views/users/create.js.erb +1 -0
  96. data/test/views/users/destroy.js.erb +1 -0
  97. data/test/views/users/edit.html.erb +1 -0
  98. data/test/views/users/index.html.erb +1 -0
  99. data/test/views/users/new.html.erb +1 -0
  100. data/test/views/users/show.html.erb +1 -0
  101. data/test/views/users/update.js.erb +1 -0
  102. metadata +180 -9
@@ -0,0 +1,17 @@
1
+ en:
2
+ flash:
3
+ addresses:
4
+ create:
5
+ notice: "You created a new address close to <b>{{reference}}</b>."
6
+ update:
7
+ notice: "Nice! {{resource_name}} was updated with success!"
8
+ error: "Oh no! We could not update your address!"
9
+ admin:
10
+ actions:
11
+ create:
12
+ notice: "Admin notice message."
13
+ error: "Admin error message."
14
+ addresses:
15
+ create:
16
+ notice: "Admin, you created a new address close to <b>{{reference}}</b>."
17
+
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  class Country
4
4
  end
@@ -0,0 +1,123 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Dresser
4
+ end
5
+
6
+ class Shelf
7
+ end
8
+
9
+ class Plate
10
+ end
11
+
12
+ class PlatesController < InheritedResources::Base
13
+ belongs_to :dresser, :shelf, :shallow => true
14
+ end
15
+
16
+ class NestedBelongsToWithShallowTest < ActionController::TestCase
17
+ tests PlatesController
18
+
19
+ def setup
20
+ mock_shelf.expects(:dresser).returns(mock_dresser)
21
+ mock_dresser.expects(:to_param).returns('13')
22
+
23
+ Dresser.expects(:find).with('13').returns(mock_dresser)
24
+ mock_dresser.expects(:shelves).returns(Shelf)
25
+ mock_shelf.expects(:plates).returns(Plate)
26
+
27
+ @controller.stubs(:resource_url).returns('/')
28
+ @controller.stubs(:collection_url).returns('/')
29
+ end
30
+
31
+ def test_assigns_dresser_and_shelf_and_plate_on_index
32
+ Shelf.expects(:find).with('37').twice.returns(mock_shelf)
33
+ Plate.expects(:all).returns([mock_plate])
34
+ get :index, :shelf_id => '37'
35
+
36
+ assert_equal mock_dresser, assigns(:dresser)
37
+ assert_equal mock_shelf, assigns(:shelf)
38
+ assert_equal [mock_plate], assigns(:plates)
39
+ end
40
+
41
+ def test_assigns_dresser_and_shelf_and_plate_on_show
42
+ should_find_parents
43
+ get :show, :id => '42'
44
+
45
+ assert_equal mock_dresser, assigns(:dresser)
46
+ assert_equal mock_shelf, assigns(:shelf)
47
+ assert_equal mock_plate, assigns(:plate)
48
+ end
49
+
50
+ def test_assigns_dresser_and_shelf_and_plate_on_new
51
+ Plate.expects(:build).returns(mock_plate)
52
+ Shelf.expects(:find).with('37').twice.returns(mock_shelf)
53
+ get :new, :shelf_id => '37'
54
+
55
+ assert_equal mock_dresser, assigns(:dresser)
56
+ assert_equal mock_shelf, assigns(:shelf)
57
+ assert_equal mock_plate, assigns(:plate)
58
+ end
59
+
60
+ def test_assigns_dresser_and_shelf_and_plate_on_edit
61
+ should_find_parents
62
+ get :edit, :id => '42'
63
+
64
+ assert_equal mock_dresser, assigns(:dresser)
65
+ assert_equal mock_shelf, assigns(:shelf)
66
+ assert_equal mock_plate, assigns(:plate)
67
+ end
68
+
69
+
70
+ def test_assigns_dresser_and_shelf_and_plate_on_create
71
+ Shelf.expects(:find).with('37').twice.returns(mock_shelf)
72
+
73
+ Plate.expects(:build).with({'these' => 'params'}).returns(mock_plate)
74
+ mock_plate.expects(:save).returns(true)
75
+ post :create, :shelf_id => '37', :plate => {:these => 'params'}
76
+
77
+ assert_equal mock_dresser, assigns(:dresser)
78
+ assert_equal mock_shelf, assigns(:shelf)
79
+ assert_equal mock_plate, assigns(:plate)
80
+ end
81
+
82
+ def test_assigns_dresser_and_shelf_and_plate_on_update
83
+ should_find_parents
84
+ mock_plate.expects(:update_attributes).returns(true)
85
+ put :update, :id => '42', :plate => {:these => 'params'}
86
+
87
+ assert_equal mock_dresser, assigns(:dresser)
88
+ assert_equal mock_shelf, assigns(:shelf)
89
+ assert_equal mock_plate, assigns(:plate)
90
+ end
91
+
92
+ def test_assigns_dresser_and_shelf_and_plate_on_destroy
93
+ should_find_parents
94
+ mock_plate.expects(:destroy)
95
+ delete :destroy, :id => '42'
96
+
97
+ assert_equal mock_dresser, assigns(:dresser)
98
+ assert_equal mock_shelf, assigns(:shelf)
99
+ assert_equal mock_plate, assigns(:plate)
100
+ end
101
+
102
+
103
+ protected
104
+ def should_find_parents
105
+ Plate.expects(:find).with('42').returns(mock_plate)
106
+ mock_plate.expects(:shelf).returns(mock_shelf)
107
+ mock_shelf.expects(:to_param).returns('37')
108
+ Plate.expects(:find).with('42').returns(mock_plate)
109
+ Shelf.expects(:find).with('37').returns(mock_shelf)
110
+ end
111
+
112
+ def mock_dresser(stubs={})
113
+ @mock_dresser ||= mock(stubs)
114
+ end
115
+
116
+ def mock_shelf(stubs={})
117
+ @mock_shelf ||= mock(stubs)
118
+ end
119
+
120
+ def mock_plate(stubs={})
121
+ @mock_plate ||= mock(stubs)
122
+ end
123
+ end
@@ -0,0 +1,133 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Subfaculty
4
+ end
5
+
6
+ class Speciality
7
+ end
8
+
9
+ module Plan
10
+ class Group
11
+ end
12
+
13
+ class Education
14
+ end
15
+ end
16
+
17
+ class GroupsController < InheritedResources::Base
18
+ defaults :resource_class => Plan::Group, :finder => :find_by_slug
19
+ belongs_to :subfaculty, :shallow => true do
20
+ belongs_to :speciality
21
+ end
22
+ end
23
+
24
+ class EducationsController < InheritedResources::Base
25
+ defaults :resource_class => Plan::Education
26
+ belongs_to :subfaculty, :shallow => true do
27
+ belongs_to :speciality do
28
+ belongs_to :group, :parent_class => Plan::Group,
29
+ :instance_name => :plan_group,
30
+ :param => :group_id,
31
+ :finder => :find_by_slug
32
+ end
33
+ end
34
+ end
35
+
36
+ class NestedModelWithShallowTest < ActionController::TestCase
37
+ tests GroupsController
38
+
39
+ def setup
40
+ mock_speciality.expects(:subfaculty).returns(mock_subfaculty)
41
+ mock_subfaculty.expects(:to_param).returns('13')
42
+
43
+ Subfaculty.expects(:find).with('13').returns(mock_subfaculty)
44
+ mock_subfaculty.expects(:specialities).returns(Speciality)
45
+ mock_speciality.expects(:groups).returns(Plan::Group)
46
+
47
+ @controller.stubs(:resource_url).returns('/')
48
+ @controller.stubs(:collection_url).returns('/')
49
+ end
50
+
51
+ def test_assigns_subfaculty_and_speciality_and_group_on_edit
52
+ should_find_parents
53
+ get :edit, :id => 'forty_two'
54
+
55
+ assert_equal mock_subfaculty, assigns(:subfaculty)
56
+ assert_equal mock_speciality, assigns(:speciality)
57
+ assert_equal mock_group, assigns(:group)
58
+ end
59
+
60
+ protected
61
+ def should_find_parents
62
+ Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
63
+ mock_group.expects(:speciality).returns(mock_speciality)
64
+ mock_speciality.expects(:to_param).returns('37')
65
+ Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
66
+ Speciality.expects(:find).with('37').returns(mock_speciality)
67
+ end
68
+
69
+ def mock_group(stubs={})
70
+ @mock_group ||= mock(stubs)
71
+ end
72
+
73
+ def mock_speciality(stubs={})
74
+ @mock_speciality ||= mock(stubs)
75
+ end
76
+
77
+ def mock_subfaculty(stubs={})
78
+ @mock_subfaculty ||= mock(stubs)
79
+ end
80
+ end
81
+
82
+ class TwoNestedModelWithShallowTest < ActionController::TestCase
83
+ tests EducationsController
84
+
85
+ def setup
86
+ mock_speciality.expects(:subfaculty).returns(mock_subfaculty)
87
+ mock_subfaculty.expects(:to_param).returns('13')
88
+ Subfaculty.expects(:find).with('13').returns(mock_subfaculty)
89
+ mock_subfaculty.expects(:specialities).returns(Speciality)
90
+ mock_speciality.expects(:groups).returns(Plan::Group)
91
+
92
+ @controller.stubs(:resource_url).returns('/')
93
+ @controller.stubs(:collection_url).returns('/')
94
+ end
95
+
96
+ def test_assigns_subfaculty_and_speciality_and_group_on_new
97
+ should_find_parents
98
+ get :new, :group_id => 'forty_two'
99
+
100
+ assert_equal mock_subfaculty, assigns(:subfaculty)
101
+ assert_equal mock_speciality, assigns(:speciality)
102
+ assert_equal mock_group, assigns(:plan_group)
103
+ assert_equal mock_education, assigns(:education)
104
+ end
105
+
106
+
107
+ protected
108
+ def should_find_parents
109
+ Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
110
+ mock_group.expects(:speciality).returns(mock_speciality)
111
+ mock_group.expects(:educations).returns(mock_education)
112
+ mock_education.expects(:build).returns(mock_education)
113
+ mock_speciality.expects(:to_param).returns('37')
114
+ Plan::Group.expects(:find_by_slug).with('forty_two').returns(mock_group)
115
+ Speciality.expects(:find).with('37').returns(mock_speciality)
116
+ end
117
+
118
+ def mock_group(stubs={})
119
+ @mock_group ||= mock(stubs)
120
+ end
121
+
122
+ def mock_education(stubs={})
123
+ @mock_education ||= mock(stubs)
124
+ end
125
+
126
+ def mock_speciality(stubs={})
127
+ @mock_speciality ||= mock(stubs)
128
+ end
129
+
130
+ def mock_subfaculty(stubs={})
131
+ @mock_subfaculty ||= mock(stubs)
132
+ end
133
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  class Brands; end
4
4
  class Category; end
@@ -22,14 +22,14 @@ class OptionalTest < ActionController::TestCase
22
22
  def test_expose_all_products_as_instance_variable_with_category
23
23
  Category.expects(:find).with('37').returns(mock_category)
24
24
  mock_category.expects(:products).returns(Product)
25
- Product.expects(:find).with(:all).returns([mock_product])
25
+ Product.expects(:all).returns([mock_product])
26
26
  get :index, :category_id => '37'
27
27
  assert_equal mock_category, assigns(:category)
28
28
  assert_equal [mock_product], assigns(:products)
29
29
  end
30
30
 
31
31
  def test_expose_all_products_as_instance_variable_without_category
32
- Product.expects(:find).with(:all).returns([mock_product])
32
+ Product.expects(:all).returns([mock_product])
33
33
  get :index
34
34
  assert_equal nil, assigns(:category)
35
35
  assert_equal [mock_product], assigns(:products)
@@ -142,7 +142,7 @@ class OptionalTest < ActionController::TestCase
142
142
  end
143
143
 
144
144
  def test_polymorphic_helpers
145
- Product.expects(:find).with(:all).returns([mock_product])
145
+ Product.expects(:all).returns([mock_product])
146
146
  get :index
147
147
 
148
148
  assert !@controller.send(:parent?)
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  class Factory; end
4
4
  class Company; end
@@ -23,7 +23,7 @@ class PolymorphicFactoriesTest < ActionController::TestCase
23
23
  end
24
24
 
25
25
  def test_expose_all_employees_as_instance_variable_on_index
26
- Employee.expects(:find).with(:all).returns([mock_employee])
26
+ Employee.expects(:all).returns([mock_employee])
27
27
  get :index, :factory_id => '37'
28
28
  assert_equal mock_factory, assigns(:factory)
29
29
  assert_equal [mock_employee], assigns(:employees)
@@ -77,7 +77,7 @@ class PolymorphicFactoriesTest < ActionController::TestCase
77
77
  def test_polymorphic_helpers
78
78
  mock_factory.stubs(:class).returns(Factory)
79
79
 
80
- Employee.expects(:find).with(:all).returns([mock_employee])
80
+ Employee.expects(:all).returns([mock_employee])
81
81
  get :index, :factory_id => '37'
82
82
 
83
83
  assert @controller.send(:parent?)
@@ -110,7 +110,7 @@ class PolymorphicCompanyTest < ActionController::TestCase
110
110
  end
111
111
 
112
112
  def test_expose_all_employees_as_instance_variable_on_index
113
- Employee.expects(:find).with(:all).returns([mock_employee])
113
+ Employee.expects(:all).returns([mock_employee])
114
114
  get :index, :company_id => '37'
115
115
  assert_equal mock_company, assigns(:company)
116
116
  assert_equal [mock_employee], assigns(:employees)
@@ -164,7 +164,7 @@ class PolymorphicCompanyTest < ActionController::TestCase
164
164
  def test_polymorphic_helpers
165
165
  mock_company.stubs(:class).returns(Company)
166
166
 
167
- Employee.expects(:find).with(:all).returns([mock_employee])
167
+ Employee.expects(:all).returns([mock_employee])
168
168
  get :index, :company_id => '37'
169
169
 
170
170
  assert @controller.send(:parent?)
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  class Machine;
4
4
  def self.human_name; 'Machine'; end
@@ -1,13 +1,15 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  # This test file is instead to test the how controller flow and actions
4
4
  # using a belongs_to association. This is done using mocks a la rspec.
5
5
  #
6
6
  class Store
7
+ extend ActiveModel::Naming
7
8
  end
8
9
 
9
10
  class Manager
10
- def self.human_name; 'Manager'; end
11
+ extend ActiveModel::Naming
12
+ # def self.human_name; 'Manager'; end
11
13
  end
12
14
 
13
15
  class ManagersController < InheritedResources::Base
@@ -66,6 +68,7 @@ class SingletonTest < ActionController::TestCase
66
68
  def test_the_requested_manager_is_destroyed_on_destroy
67
69
  Store.expects(:find).with('37').returns(mock_store)
68
70
  mock_store.expects(:manager).returns(mock_manager)
71
+ @controller.expects(:parent_url).returns('http://test.host/')
69
72
  mock_manager.expects(:destroy)
70
73
  delete :destroy, :store_id => '37'
71
74
  assert_equal mock_store, assigns(:store)
@@ -1,21 +1,7 @@
1
1
  require 'rubygems'
2
+ require 'bundler'
2
3
 
3
- gem 'responders', '0.6.0'
4
- gem 'activesupport', '3.0.0.beta2'
5
- gem 'activemodel', '3.0.0.beta2'
6
- gem 'actionpack', '3.0.0.beta2'
7
- gem 'railties', '3.0.0.beta2'
8
-
9
- begin
10
- gem "test-unit"
11
- rescue LoadError
12
- end
13
-
14
- begin
15
- gem "ruby-debug"
16
- require 'ruby-debug'
17
- rescue LoadError
18
- end
4
+ Bundler.setup
19
5
 
20
6
  require 'test/unit'
21
7
  require 'mocha'
@@ -25,10 +11,7 @@ RAILS_ROOT = "anywhere"
25
11
 
26
12
  require "active_support"
27
13
  require "active_model"
28
-
29
14
  require "action_controller"
30
- require "action_dispatch/middleware/flash"
31
-
32
15
  require "rails/railtie"
33
16
 
34
17
  I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
@@ -37,15 +20,16 @@ I18n.reload!
37
20
  class ApplicationController < ActionController::Base; end
38
21
 
39
22
  # Add IR to load path and load the main file
40
- ActiveSupport::Dependencies.load_paths << File.expand_path(File.dirname(__FILE__) + '/../lib')
41
- require_dependency 'inherited_resources'
23
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
24
+ require 'inherited_resources'
42
25
 
43
26
  ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
44
27
 
45
28
  InheritedResources::Routes = ActionDispatch::Routing::RouteSet.new
46
- InheritedResources::Routes.draw do |map|
47
- map.connect ':controller/:action/:id'
48
- map.connect ':controller/:action'
29
+ InheritedResources::Routes.draw do
30
+ match ':controller(/:action(/:id))'
31
+ match ':controller(/:action)'
32
+ resources 'posts'
49
33
  end
50
34
 
51
35
  ActionController::Base.send :include, InheritedResources::Routes.url_helpers
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  class Universe
4
4
  extend ActiveModel::Naming
@@ -13,6 +13,12 @@ end
13
13
  class HousesController < InheritedResources::Base
14
14
  end
15
15
 
16
+ class News
17
+ extend ActiveModel::Naming
18
+ end
19
+ class NewsController < InheritedResources::Base
20
+ end
21
+
16
22
  class Backpack
17
23
  extend ActiveModel::Naming
18
24
  end
@@ -49,6 +55,13 @@ class BedsController < InheritedResources::Base
49
55
  optional_belongs_to :house, :building
50
56
  end
51
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
+
52
65
  class Desk
53
66
  extend ActiveModel::Naming
54
67
  end
@@ -78,6 +91,38 @@ class CentersController < InheritedResources::Base
78
91
  end
79
92
  end
80
93
 
94
+ class Mirror
95
+ extend ActiveModel::Naming
96
+ end
97
+ class MirrorsController < InheritedResources::Base
98
+ belongs_to :house, :shallow => true
99
+ end
100
+ class Admin::MirrorsController < InheritedResources::Base
101
+ belongs_to :house, :shallow => true
102
+ end
103
+
104
+
105
+ class Display
106
+ extend ActiveModel::Naming
107
+ end
108
+
109
+ class Window
110
+ extend ActiveModel::Naming
111
+ end
112
+
113
+ class Button
114
+ extend ActiveModel::Naming
115
+ end
116
+
117
+ class ButtonsController < InheritedResources::Base
118
+ belongs_to :display, :window, :shallow => true
119
+ custom_actions :resource => :delete, :collection => :search
120
+ end
121
+
122
+ class ImageButtonsController < ButtonsController
123
+ end
124
+
125
+
81
126
  # Create a TestHelper module with some helpers
82
127
  class UrlHelpersTest < ActiveSupport::TestCase
83
128
 
@@ -111,6 +156,36 @@ class UrlHelpersTest < ActiveSupport::TestCase
111
156
  end
112
157
  end
113
158
 
159
+ def test_url_helpers_on_simple_inherited_resource_using_uncountable
160
+ controller = NewsController.new
161
+ controller.instance_variable_set('@news', :news)
162
+
163
+ [:url, :path].each do |path_or_url|
164
+ controller.expects("news_index_#{path_or_url}").with({}).once
165
+ controller.send("collection_#{path_or_url}")
166
+
167
+ controller.expects("news_#{path_or_url}").with(:news, {}).once
168
+ controller.send("resource_#{path_or_url}")
169
+
170
+ controller.expects("new_news_#{path_or_url}").with({}).once
171
+ controller.send("new_resource_#{path_or_url}")
172
+
173
+ controller.expects("edit_news_#{path_or_url}").with(:news, {}).once
174
+ controller.send("edit_resource_#{path_or_url}")
175
+
176
+ # With arg
177
+ controller.expects("news_#{path_or_url}").with(:arg, {}).once
178
+ controller.send("resource_#{path_or_url}", :arg)
179
+
180
+ controller.expects("news_#{path_or_url}").with(:arg, {}).once
181
+ controller.send("resource_#{path_or_url}", :arg)
182
+
183
+ # With options
184
+ controller.expects("news_#{path_or_url}").with(:arg, :page => 1).once
185
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
186
+ end
187
+ end
188
+
114
189
  def test_url_helpers_on_simple_inherited_namespaced_resource
115
190
  controller = Admin::BackpacksController.new
116
191
  controller.instance_variable_set('@backpack', :backpack)
@@ -322,7 +397,7 @@ class UrlHelpersTest < ActiveSupport::TestCase
322
397
  def test_url_helpers_on_polymorphic_belongs_to
323
398
  house = House.new
324
399
  bed = Bed.new
325
-
400
+
326
401
  new_bed = Bed.new
327
402
  Bed.stubs(:new).returns(new_bed)
328
403
  new_bed.stubs(:persisted?).returns(false)
@@ -373,6 +448,60 @@ class UrlHelpersTest < ActiveSupport::TestCase
373
448
  controller.send("parent_url", :arg)
374
449
  end
375
450
 
451
+ def test_url_helpers_on_polymorphic_belongs_to_using_uncountable
452
+ sheep = Sheep.new
453
+ news = News.new
454
+
455
+ new_sheep = Sheep.new
456
+ Sheep.stubs(:new).returns(new_sheep)
457
+ new_sheep.stubs(:persisted?).returns(false)
458
+
459
+ controller = SheepController.new
460
+ controller.instance_variable_set('@parent_type', :news)
461
+ controller.instance_variable_set('@news', news)
462
+ controller.instance_variable_set('@sheep', sheep)
463
+
464
+ [:url, :path].each do |path_or_url|
465
+ controller.expects("news_sheep_index_#{path_or_url}").with(news).once
466
+ controller.send("collection_#{path_or_url}")
467
+
468
+ controller.expects("news_sheep_#{path_or_url}").with(news, sheep).once
469
+ controller.send("resource_#{path_or_url}")
470
+
471
+ controller.expects("new_news_sheep_#{path_or_url}").with(news).once
472
+ controller.send("new_resource_#{path_or_url}")
473
+
474
+ controller.expects("edit_news_sheep_#{path_or_url}").with(news, sheep).once
475
+ controller.send("edit_resource_#{path_or_url}")
476
+
477
+ controller.expects("news_#{path_or_url}").with(news).once
478
+ controller.send("parent_#{path_or_url}")
479
+
480
+ controller.expects("edit_news_#{path_or_url}").with(news).once
481
+ controller.send("edit_parent_#{path_or_url}")
482
+ end
483
+
484
+ # With options
485
+ controller.expects("news_sheep_url").with(news, sheep, :page => 1).once
486
+ controller.send("resource_url", :page => 1)
487
+
488
+ controller.expects("news_url").with(news, :page => 1).once
489
+ controller.send("parent_url", :page => 1)
490
+
491
+ # With args
492
+ controller.expects("polymorphic_url").with([:arg, new_sheep], {}).once
493
+ controller.send("collection_url", :arg)
494
+
495
+ controller.expects("polymorphic_url").with([news, :arg], {}).once
496
+ controller.send("resource_url", :arg)
497
+
498
+ controller.expects("edit_polymorphic_url").with([news, :arg], {}).once
499
+ controller.send("edit_resource_url", :arg)
500
+
501
+ controller.expects("polymorphic_url").with([:arg], {}).once
502
+ controller.send("parent_url", :arg)
503
+ end
504
+
376
505
  def test_url_helpers_on_namespaced_polymorphic_belongs_to
377
506
  house = House.new
378
507
  desk = Desk.new
@@ -565,4 +694,116 @@ class UrlHelpersTest < ActiveSupport::TestCase
565
694
  controller.send("edit_resource_url", :arg)
566
695
  end
567
696
 
697
+
698
+ def test_url_helpers_on_belongs_to_with_shallowed_route
699
+ controller = MirrorsController.new
700
+ controller.instance_variable_set('@house', :house)
701
+ controller.instance_variable_set('@mirror', :mirror)
702
+
703
+ [:url, :path].each do |path_or_url|
704
+
705
+ controller.expects("house_mirrors_#{path_or_url}").with(:house, {}).once
706
+ controller.send("collection_#{path_or_url}")
707
+
708
+ controller.expects("mirror_#{path_or_url}").with(:mirror, {}).once
709
+ controller.send("resource_#{path_or_url}")
710
+
711
+ controller.expects("new_house_mirror_#{path_or_url}").with(:house, {}).once
712
+ controller.send("new_resource_#{path_or_url}")
713
+
714
+ controller.expects("edit_mirror_#{path_or_url}").with(:mirror, {}).once
715
+ controller.send("edit_resource_#{path_or_url}")
716
+
717
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
718
+ controller.send("parent_#{path_or_url}")
719
+
720
+ controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
721
+ controller.send("edit_parent_#{path_or_url}")
722
+ end
723
+ end
724
+
725
+ def test_url_helpers_on_nested_belongs_to_with_shallowed_route
726
+ controller = ButtonsController.new
727
+ controller.instance_variable_set('@display', :display)
728
+ controller.instance_variable_set('@window', :window)
729
+ controller.instance_variable_set('@button', :button)
730
+
731
+ [:url, :path].each do |path_or_url|
732
+ controller.expects("window_buttons_#{path_or_url}").with(:window, {}).once
733
+ controller.send("collection_#{path_or_url}")
734
+
735
+ controller.expects("button_#{path_or_url}").with(:button, {}).once
736
+ controller.send("resource_#{path_or_url}")
737
+
738
+ controller.expects("new_window_button_#{path_or_url}").with(:window, {}).once
739
+ controller.send("new_resource_#{path_or_url}")
740
+
741
+ controller.expects("edit_button_#{path_or_url}").with(:button, {}).once
742
+ controller.send("edit_resource_#{path_or_url}")
743
+
744
+ controller.expects("window_#{path_or_url}").with(:window, {}).once
745
+ controller.send("parent_#{path_or_url}")
746
+
747
+ controller.expects("edit_window_#{path_or_url}").with(:window, {}).once
748
+ controller.send("edit_parent_#{path_or_url}")
749
+ end
750
+ end
751
+
752
+ def test_url_helpers_with_custom_actions
753
+ controller = ButtonsController.new
754
+ controller.instance_variable_set('@display', :display)
755
+ controller.instance_variable_set('@window', :window)
756
+ controller.instance_variable_set('@button', :button)
757
+ [:url, :path].each do |path_or_url|
758
+ controller.expects("delete_button_#{path_or_url}").with(:button, {}).once
759
+ controller.send("delete_resource_#{path_or_url}")
760
+
761
+ controller.expects("search_window_buttons_#{path_or_url}").with(:window, {}).once
762
+ controller.send("search_resources_#{path_or_url}")
763
+ end
764
+ end
765
+
766
+ def test_helper_methods_with_custom_actions
767
+ controller = ButtonsController.new
768
+ helper_methods = controller.class._helpers.instance_methods.map {|m| m.to_s }
769
+ [:url, :path].each do |path_or_url|
770
+ assert helper_methods.include?("delete_resource_#{path_or_url}")
771
+ assert helper_methods.include?("search_resources_#{path_or_url}")
772
+ end
773
+ end
774
+
775
+ def test_helpers_on_inherited_controller
776
+ controller = ImageButtonsController.new
777
+ controller.expects("edit_image_button_path").once
778
+ controller.send("edit_resource_path")
779
+ controller.expects("delete_image_button_path").once
780
+ controller.send("delete_resource_path")
781
+ end
782
+
783
+ def test_url_helpers_on_namespaced_resource_with_shallowed_route
784
+ controller = Admin::MirrorsController.new
785
+ controller.instance_variable_set('@house', :house)
786
+ controller.instance_variable_set('@mirror', :mirror)
787
+
788
+ [:url, :path].each do |path_or_url|
789
+
790
+ controller.expects("admin_house_mirrors_#{path_or_url}").with(:house, {}).once
791
+ controller.send("collection_#{path_or_url}")
792
+
793
+ controller.expects("admin_mirror_#{path_or_url}").with(:mirror, {}).once
794
+ controller.send("resource_#{path_or_url}")
795
+
796
+ controller.expects("new_admin_house_mirror_#{path_or_url}").with(:house, {}).once
797
+ controller.send("new_resource_#{path_or_url}")
798
+
799
+ controller.expects("edit_admin_mirror_#{path_or_url}").with(:mirror, {}).once
800
+ controller.send("edit_resource_#{path_or_url}")
801
+
802
+ controller.expects("admin_house_#{path_or_url}").with(:house, {}).once
803
+ controller.send("parent_#{path_or_url}")
804
+
805
+ controller.expects("edit_admin_house_#{path_or_url}").with(:house, {}).once
806
+ controller.send("edit_parent_#{path_or_url}")
807
+ end
808
+ end
568
809
  end