josevalim-inherited_resources 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -167,7 +167,8 @@ module InheritedResources #:nodoc:
167
167
  ivars << 'resource_class.new'
168
168
  end
169
169
 
170
- ivars = "[#{ivars.join(', ')}]"
170
+ # Add compact to deal with polymorphic optional associations.
171
+ ivars = "[#{ivars.join(', ')}].compact"
171
172
  else
172
173
  # In the last case, if segments is empty (this usually happens with
173
174
  # root singleton resources, we set it to root)
@@ -1,109 +1,99 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
- class GreatSchool
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 Post
4
7
  end
5
8
 
6
- class Professor
7
- def self.human_name; 'Professor'; end
9
+ class Comment
10
+ def self.human_name; 'Comment'; end
8
11
  end
9
12
 
10
- BELONGS_TO_OPTIONS = {
11
- :parent_class => GreatSchool,
12
- :instance_name => :great_school,
13
- :finder => :find_by_title!,
14
- :param => :school_title
15
- }
16
-
17
- class ProfessorsController < InheritedResources::Base
18
- belongs_to :school, BELONGS_TO_OPTIONS
13
+ class CommentsController < InheritedResources::Base
14
+ belongs_to :post
19
15
  end
20
16
 
21
- # Create a TestHelper module with some helpers
22
17
  class BelongsToTest < TEST_CLASS
18
+
23
19
  def setup
24
- @controller = ProfessorsController.new
20
+ @controller = CommentsController.new
25
21
  @controller.request = @request = ActionController::TestRequest.new
26
22
  @controller.response = @response = ActionController::TestResponse.new
27
23
  end
28
24
 
29
- def test_expose_the_resquested_school_with_chosen_instance_variable
30
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
31
- Professor.stubs(:find).returns([mock_professor])
32
- get :index, :school_title => 'nice'
33
- assert_equal mock_school, assigns(:great_school)
25
+ def test_expose_all_comments_as_instance_variable_on_index
26
+ Post.expects(:find).with('37').returns(mock_post)
27
+ mock_post.expects(:comments).returns(Comment)
28
+ Comment.expects(:find).with(:all).returns([mock_comment])
29
+ get :index, :post_id => '37'
30
+ assert_equal mock_post, assigns(:post)
31
+ assert_equal [mock_comment], assigns(:comments)
34
32
  end
35
33
 
36
- def test_expose_the_resquested_school_with_chosen_instance_variable
37
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
38
- Professor.stubs(:find).returns(mock_professor)
39
- get :show, :school_title => 'nice'
40
- assert_equal mock_school, assigns(:great_school)
34
+ def test_expose_the_resquested_comment_on_show
35
+ Post.expects(:find).with('37').returns(mock_post)
36
+ mock_post.expects(:comments).returns(Comment)
37
+ Comment.expects(:find).with('42').returns(mock_comment)
38
+ get :show, :id => '42', :post_id => '37'
39
+ assert_equal mock_post, assigns(:post)
40
+ assert_equal mock_comment, assigns(:comment)
41
41
  end
42
42
 
43
- def test_expose_the_resquested_school_with_chosen_instance_variable
44
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
45
- Professor.stubs(:build).returns(mock_professor)
46
- get :new, :school_title => 'nice'
47
- assert_equal mock_school, assigns(:great_school)
43
+ def test_expose_a_new_comment_on_new
44
+ Post.expects(:find).with('37').returns(mock_post)
45
+ mock_post.expects(:comments).returns(Comment)
46
+ Comment.expects(:build).returns(mock_comment)
47
+ get :new, :post_id => '37'
48
+ assert_equal mock_post, assigns(:post)
49
+ assert_equal mock_comment, assigns(:comment)
48
50
  end
49
51
 
50
- def test_expose_the_resquested_school_with_chosen_instance_variable
51
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
52
- Professor.stubs(:find).returns(mock_professor)
53
- get :edit, :school_title => 'nice'
54
- assert_equal mock_school, assigns(:great_school)
52
+ def test_expose_the_resquested_comment_on_edit
53
+ Post.expects(:find).with('37').returns(mock_post)
54
+ mock_post.expects(:comments).returns(Comment)
55
+ Comment.expects(:find).with('42').returns(mock_comment)
56
+ get :edit, :id => '42', :post_id => '37'
57
+ assert_equal mock_post, assigns(:post)
58
+ assert_equal mock_comment, assigns(:comment)
55
59
  end
56
60
 
57
- def test_expose_the_resquested_school_with_chosen_instance_variable
58
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
59
- Professor.stubs(:build).returns(mock_professor(:save => true))
60
- post :create, :school_title => 'nice'
61
- assert_equal mock_school, assigns(:great_school)
61
+ def test_expose_a_newly_create_comment_on_create
62
+ Post.expects(:find).with('37').returns(mock_post)
63
+ mock_post.expects(:comments).returns(Comment)
64
+ Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(:save => true))
65
+ post :create, :post_id => '37', :comment => {:these => 'params'}
66
+ assert_equal mock_post, assigns(:post)
67
+ assert_equal mock_comment, assigns(:comment)
62
68
  end
63
69
 
64
- def test_expose_the_resquested_school_with_chosen_instance_variable
65
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
66
- Professor.stubs(:find).returns(mock_professor(:update_attributes => true))
67
- put :update, :school_title => 'nice'
68
- assert_equal mock_school, assigns(:great_school)
70
+ def test_update_the_requested_object_on_update
71
+ Post.expects(:find).with('37').returns(mock_post)
72
+ mock_post.expects(:comments).returns(Comment)
73
+ Comment.expects(:find).with('42').returns(mock_comment)
74
+ mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
75
+ put :update, :id => '42', :post_id => '37', :comment => {:these => 'params'}
76
+ assert_equal mock_post, assigns(:post)
77
+ assert_equal mock_comment, assigns(:comment)
69
78
  end
70
79
 
71
- def test_expose_the_resquested_school_with_chosen_instance_variable
72
- GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
73
- Professor.stubs(:find).returns(mock_professor(:destroy => true))
74
- delete :destroy, :school_title => 'nice'
75
- assert_equal mock_school, assigns(:great_school)
80
+ def test_the_resquested_comment_is_destroyed_on_destroy
81
+ Post.expects(:find).with('37').returns(mock_post)
82
+ mock_post.expects(:comments).returns(Comment)
83
+ Comment.expects(:find).with('42').returns(mock_comment)
84
+ mock_comment.expects(:destroy)
85
+ delete :destroy, :id => '42', :post_id => '37'
86
+ assert_equal mock_post, assigns(:post)
87
+ assert_equal mock_comment, assigns(:comment)
76
88
  end
77
-
78
- def test_belongs_to_raise_errors_with_invalid_arguments
79
- assert_raise ArgumentError do
80
- ProfessorsController.send(:belongs_to)
81
- end
82
-
83
- assert_raise ArgumentError do
84
- ProfessorsController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
85
- end
86
-
87
- assert_raise ArgumentError do
88
- ProfessorsController.send(:belongs_to, :nice, :invalid_key => '')
89
- end
90
- end
91
-
92
- def test_url_helpers_are_recreated_when_defaults_change
93
- InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
94
- ProfessorsController.send(:defaults, BELONGS_TO_OPTIONS)
95
- ensure
96
- # Reestore default settings
97
- ProfessorsController.send(:parents_symbols=, [:school])
98
- end
99
-
100
89
  protected
101
- def mock_school(stubs={})
102
- @mock_school ||= mock(stubs)
90
+ def mock_post(stubs={})
91
+ @mock_post ||= mock(stubs)
103
92
  end
104
93
 
105
- def mock_professor(stubs={})
106
- @mock_professor ||= mock(stubs)
94
+ def mock_comment(stubs={})
95
+ @mock_comment ||= mock(stubs)
107
96
  end
97
+
108
98
  end
109
99
 
@@ -14,7 +14,27 @@ end
14
14
  class FoldersController < InheritedResources::Base
15
15
  end
16
16
 
17
- class ActionsTest < ActiveSupport::TestCase
17
+ # For belongs_to tests
18
+ class GreatSchool
19
+ end
20
+
21
+ class Professor
22
+ def self.human_name; 'Professor'; end
23
+ end
24
+
25
+ BELONGS_TO_OPTIONS = {
26
+ :parent_class => GreatSchool,
27
+ :instance_name => :great_school,
28
+ :finder => :find_by_title!,
29
+ :param => :school_title
30
+ }
31
+
32
+ class ProfessorsController < InheritedResources::Base
33
+ belongs_to :school, BELONGS_TO_OPTIONS
34
+ end
35
+
36
+
37
+ class ActionsClassMethodTest < ActiveSupport::TestCase
18
38
  def test_actions_are_undefined_when_only_option_is_given
19
39
  action_methods = BooksController.send(:action_methods)
20
40
  assert_equal 2, action_methods.size
@@ -34,7 +54,8 @@ class ActionsTest < ActiveSupport::TestCase
34
54
  end
35
55
  end
36
56
 
37
- class DefaultsTest < ActiveSupport::TestCase
57
+
58
+ class DefaultsClassMethodTest < ActiveSupport::TestCase
38
59
  def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
39
60
  assert_nil ReadersController.send(:resource_class)
40
61
  end
@@ -69,5 +90,93 @@ class DefaultsTest < ActiveSupport::TestCase
69
90
  InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
70
91
  BooksController.send(:defaults, :instance_name => 'string', :collection_name => 'strings')
71
92
  end
93
+ end
94
+
95
+
96
+ class BelongsToClassMethodTest < TEST_CLASS
97
+ def setup
98
+ @controller = ProfessorsController.new
99
+ @controller.request = @request = ActionController::TestRequest.new
100
+ @controller.response = @response = ActionController::TestResponse.new
101
+ end
102
+
103
+ def test_expose_the_resquested_school_with_chosen_instance_variable
104
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
105
+ Professor.stubs(:find).returns([mock_professor])
106
+ get :index, :school_title => 'nice'
107
+ assert_equal mock_school, assigns(:great_school)
108
+ end
109
+
110
+ def test_expose_the_resquested_school_with_chosen_instance_variable
111
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
112
+ Professor.stubs(:find).returns(mock_professor)
113
+ get :show, :school_title => 'nice'
114
+ assert_equal mock_school, assigns(:great_school)
115
+ end
116
+
117
+ def test_expose_the_resquested_school_with_chosen_instance_variable
118
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
119
+ Professor.stubs(:build).returns(mock_professor)
120
+ get :new, :school_title => 'nice'
121
+ assert_equal mock_school, assigns(:great_school)
122
+ end
123
+
124
+ def test_expose_the_resquested_school_with_chosen_instance_variable
125
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
126
+ Professor.stubs(:find).returns(mock_professor)
127
+ get :edit, :school_title => 'nice'
128
+ assert_equal mock_school, assigns(:great_school)
129
+ end
130
+
131
+ def test_expose_the_resquested_school_with_chosen_instance_variable
132
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
133
+ Professor.stubs(:build).returns(mock_professor(:save => true))
134
+ post :create, :school_title => 'nice'
135
+ assert_equal mock_school, assigns(:great_school)
136
+ end
137
+
138
+ def test_expose_the_resquested_school_with_chosen_instance_variable
139
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
140
+ Professor.stubs(:find).returns(mock_professor(:update_attributes => true))
141
+ put :update, :school_title => 'nice'
142
+ assert_equal mock_school, assigns(:great_school)
143
+ end
144
+
145
+ def test_expose_the_resquested_school_with_chosen_instance_variable
146
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
147
+ Professor.stubs(:find).returns(mock_professor(:destroy => true))
148
+ delete :destroy, :school_title => 'nice'
149
+ assert_equal mock_school, assigns(:great_school)
150
+ end
151
+
152
+ def test_belongs_to_raise_errors_with_invalid_arguments
153
+ assert_raise ArgumentError do
154
+ ProfessorsController.send(:belongs_to)
155
+ end
156
+
157
+ assert_raise ArgumentError do
158
+ ProfessorsController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
159
+ end
160
+
161
+ assert_raise ArgumentError do
162
+ ProfessorsController.send(:belongs_to, :nice, :invalid_key => '')
163
+ end
164
+ end
72
165
 
166
+ def test_url_helpers_are_recreated_when_defaults_change
167
+ InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
168
+ ProfessorsController.send(:defaults, BELONGS_TO_OPTIONS)
169
+ ensure
170
+ # Reestore default settings
171
+ ProfessorsController.send(:parents_symbols=, [:school])
172
+ end
173
+
174
+ protected
175
+ def mock_school(stubs={})
176
+ @mock_school ||= mock(stubs)
177
+ end
178
+
179
+ def mock_professor(stubs={})
180
+ @mock_professor ||= mock(stubs)
181
+ end
73
182
  end
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Brands; end
4
+ class Category; end
5
+
6
+ class Product
7
+ def self.human_name; 'Product'; end
8
+ end
9
+
10
+ class ProductsController < InheritedResources::Base
11
+ belongs_to :brand, :category, :polymorphic => true, :optional => true
12
+ end
13
+
14
+ # Create a TestHelper module with some helpers
15
+ module ProductTestHelper
16
+ def setup
17
+ @controller = ProductsController.new
18
+ @controller.request = @request = ActionController::TestRequest.new
19
+ @controller.response = @response = ActionController::TestResponse.new
20
+ end
21
+
22
+ protected
23
+ def mock_category(stubs={})
24
+ @mock_category ||= mock(stubs)
25
+ end
26
+
27
+ def mock_product(stubs={})
28
+ @mock_product ||= mock(stubs)
29
+ end
30
+ end
31
+
32
+ class IndexActionOptionalTest < TEST_CLASS
33
+ include ProductTestHelper
34
+
35
+ def test_expose_all_products_as_instance_variable_with_category
36
+ Category.expects(:find).with('37').returns(mock_category)
37
+ mock_category.expects(:products).returns(Product)
38
+ Product.expects(:find).with(:all).returns([mock_product])
39
+ get :index, :category_id => '37'
40
+ assert_equal mock_category, assigns(:category)
41
+ assert_equal [mock_product], assigns(:products)
42
+ end
43
+
44
+ def test_expose_all_products_as_instance_variable_without_category
45
+ Product.expects(:find).with(:all).returns([mock_product])
46
+ get :index
47
+ assert_equal nil, assigns(:category)
48
+ assert_equal [mock_product], assigns(:products)
49
+ end
50
+ end
51
+
52
+ class ShowActionOptionalTest < TEST_CLASS
53
+ include ProductTestHelper
54
+
55
+ def test_expose_the_resquested_product_with_category
56
+ Category.expects(:find).with('37').returns(mock_category)
57
+ mock_category.expects(:products).returns(Product)
58
+ Product.expects(:find).with('42').returns(mock_product)
59
+ get :show, :id => '42', :category_id => '37'
60
+ assert_equal mock_category, assigns(:category)
61
+ assert_equal mock_product, assigns(:product)
62
+ end
63
+
64
+ def test_expose_the_resquested_product_without_category
65
+ Product.expects(:find).with('42').returns(mock_product)
66
+ get :show, :id => '42'
67
+ assert_equal nil, assigns(:category)
68
+ assert_equal mock_product, assigns(:product)
69
+ end
70
+ end
71
+
72
+ class NewActionOptionalTest < TEST_CLASS
73
+ include ProductTestHelper
74
+
75
+ def test_expose_a_new_product_with_category
76
+ Category.expects(:find).with('37').returns(mock_category)
77
+ mock_category.expects(:products).returns(Product)
78
+ Product.expects(:build).returns(mock_product)
79
+ get :new, :category_id => '37'
80
+ assert_equal mock_category, assigns(:category)
81
+ assert_equal mock_product, assigns(:product)
82
+ end
83
+
84
+ def test_expose_a_new_product_without_category
85
+ Product.expects(:new).returns(mock_product)
86
+ get :new
87
+ assert_equal nil, assigns(:category)
88
+ assert_equal mock_product, assigns(:product)
89
+ end
90
+ end
91
+
92
+ class EditActionOptionalTest < TEST_CLASS
93
+ include ProductTestHelper
94
+
95
+ def test_expose_the_resquested_product_with_category
96
+ Category.expects(:find).with('37').returns(mock_category)
97
+ mock_category.expects(:products).returns(Product)
98
+ Product.expects(:find).with('42').returns(mock_product)
99
+ get :edit, :id => '42', :category_id => '37'
100
+ assert_equal mock_category, assigns(:category)
101
+ assert_equal mock_product, assigns(:product)
102
+ end
103
+
104
+ def test_expose_the_resquested_product_without_category
105
+ Product.expects(:find).with('42').returns(mock_product)
106
+ get :edit, :id => '42'
107
+ assert_equal nil, assigns(:category)
108
+ assert_equal mock_product, assigns(:product)
109
+ end
110
+ end
111
+
112
+ class CreateActionOptionalTest < TEST_CLASS
113
+ include ProductTestHelper
114
+
115
+ def test_expose_a_newly_create_product_with_category
116
+ Category.expects(:find).with('37').returns(mock_category)
117
+ mock_category.expects(:products).returns(Product)
118
+ Product.expects(:build).with({'these' => 'params'}).returns(mock_product(:save => true))
119
+ post :create, :category_id => '37', :product => {:these => 'params'}
120
+ assert_equal mock_category, assigns(:category)
121
+ assert_equal mock_product, assigns(:product)
122
+ end
123
+
124
+ def test_expose_a_newly_create_product_without_category
125
+ Product.expects(:new).with({'these' => 'params'}).returns(mock_product(:save => true))
126
+ post :create, :product => {:these => 'params'}
127
+ assert_equal nil, assigns(:category)
128
+ assert_equal mock_product, assigns(:product)
129
+ end
130
+ end
131
+
132
+ class UpdateActionOptionalTest < TEST_CLASS
133
+ include ProductTestHelper
134
+
135
+ def test_update_the_requested_object_with_category
136
+ Category.expects(:find).with('37').returns(mock_category)
137
+ mock_category.expects(:products).returns(Product)
138
+ Product.expects(:find).with('42').returns(mock_product)
139
+ mock_product.expects(:update_attributes).with({'these' => 'params'}).returns(true)
140
+ put :update, :id => '42', :category_id => '37', :product => {:these => 'params'}
141
+ assert_equal mock_category, assigns(:category)
142
+ assert_equal mock_product, assigns(:product)
143
+ end
144
+
145
+ def test_update_the_requested_object_without_category
146
+ Product.expects(:find).with('42').returns(mock_product)
147
+ mock_product.expects(:update_attributes).with({'these' => 'params'}).returns(true)
148
+ put :update, :id => '42', :product => {:these => 'params'}
149
+ assert_equal nil, assigns(:category)
150
+ assert_equal mock_product, assigns(:product)
151
+ end
152
+ end
153
+
154
+ class DestroyActionOptionalTest < TEST_CLASS
155
+ include ProductTestHelper
156
+
157
+ def test_the_resquested_product_is_destroyed_with_category
158
+ Category.expects(:find).with('37').returns(mock_category)
159
+ mock_category.expects(:products).returns(Product)
160
+ Product.expects(:find).with('42').returns(mock_product)
161
+ mock_product.expects(:destroy)
162
+ delete :destroy, :id => '42', :category_id => '37'
163
+ assert_equal mock_category, assigns(:category)
164
+ assert_equal mock_product, assigns(:product)
165
+ end
166
+
167
+ def test_the_resquested_product_is_destroyed_without_category
168
+ Product.expects(:find).with('42').returns(mock_product)
169
+ mock_product.expects(:destroy)
170
+ delete :destroy, :id => '42'
171
+ assert_equal nil, assigns(:category)
172
+ assert_equal mock_product, assigns(:product)
173
+ end
174
+ end
175
+
176
+ class OptionalHelpersTest < TEST_CLASS
177
+ include ProductTestHelper
178
+
179
+ def test_polymorphic_helpers
180
+ Product.expects(:find).with(:all).returns([mock_product])
181
+ get :index
182
+
183
+ assert !@controller.send(:parent?)
184
+ assert_equal nil, assigns(:parent_type)
185
+ assert_equal nil, @controller.send(:parent_type)
186
+ assert_equal nil, @controller.send(:parent_class)
187
+ assert_equal nil, assigns(:category)
188
+ assert_equal nil, @controller.send(:parent)
189
+ end
190
+ end