JackDanger-inherited_resources 0.9.1

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 (37) hide show
  1. data/CHANGELOG +101 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +523 -0
  4. data/Rakefile +37 -0
  5. data/VERSION +1 -0
  6. data/lib/inherited_resources.rb +23 -0
  7. data/lib/inherited_resources/actions.rb +79 -0
  8. data/lib/inherited_resources/base.rb +42 -0
  9. data/lib/inherited_resources/base_helpers.rb +363 -0
  10. data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  11. data/lib/inherited_resources/class_methods.rb +337 -0
  12. data/lib/inherited_resources/dsl.rb +26 -0
  13. data/lib/inherited_resources/dumb_responder.rb +20 -0
  14. data/lib/inherited_resources/has_scope_helpers.rb +83 -0
  15. data/lib/inherited_resources/legacy/respond_to.rb +156 -0
  16. data/lib/inherited_resources/legacy/responder.rb +200 -0
  17. data/lib/inherited_resources/polymorphic_helpers.rb +155 -0
  18. data/lib/inherited_resources/singleton_helpers.rb +95 -0
  19. data/lib/inherited_resources/url_helpers.rb +179 -0
  20. data/test/aliases_test.rb +139 -0
  21. data/test/association_chain_test.rb +125 -0
  22. data/test/base_test.rb +225 -0
  23. data/test/belongs_to_test.rb +87 -0
  24. data/test/class_methods_test.rb +138 -0
  25. data/test/customized_belongs_to_test.rb +76 -0
  26. data/test/defaults_test.rb +70 -0
  27. data/test/flash_test.rb +88 -0
  28. data/test/has_scope_test.rb +139 -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/respond_to_test.rb +155 -0
  34. data/test/singleton_test.rb +83 -0
  35. data/test/test_helper.rb +31 -0
  36. data/test/url_helpers_test.rb +537 -0
  37. metadata +88 -0
@@ -0,0 +1,164 @@
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
+ class OptionalTest < ActionController::TestCase
15
+ tests ProductsController
16
+
17
+ def setup
18
+ @controller.stubs(:resource_url).returns('/')
19
+ @controller.stubs(:collection_url).returns('/')
20
+ end
21
+
22
+ def test_expose_all_products_as_instance_variable_with_category
23
+ Category.expects(:find).with('37').returns(mock_category)
24
+ mock_category.expects(:products).returns(Product)
25
+ Product.expects(:find).with(:all).returns([mock_product])
26
+ get :index, :category_id => '37'
27
+ assert_equal mock_category, assigns(:category)
28
+ assert_equal [mock_product], assigns(:products)
29
+ end
30
+
31
+ def test_expose_all_products_as_instance_variable_without_category
32
+ Product.expects(:find).with(:all).returns([mock_product])
33
+ get :index
34
+ assert_equal nil, assigns(:category)
35
+ assert_equal [mock_product], assigns(:products)
36
+ end
37
+
38
+ def test_expose_the_resquested_product_with_category
39
+ Category.expects(:find).with('37').returns(mock_category)
40
+ mock_category.expects(:products).returns(Product)
41
+ Product.expects(:find).with('42').returns(mock_product)
42
+ get :show, :id => '42', :category_id => '37'
43
+ assert_equal mock_category, assigns(:category)
44
+ assert_equal mock_product, assigns(:product)
45
+ end
46
+
47
+ def test_expose_the_resquested_product_without_category
48
+ Product.expects(:find).with('42').returns(mock_product)
49
+ get :show, :id => '42'
50
+ assert_equal nil, assigns(:category)
51
+ assert_equal mock_product, assigns(:product)
52
+ end
53
+
54
+ def test_expose_a_new_product_with_category
55
+ Category.expects(:find).with('37').returns(mock_category)
56
+ mock_category.expects(:products).returns(Product)
57
+ Product.expects(:build).returns(mock_product)
58
+ get :new, :category_id => '37'
59
+ assert_equal mock_category, assigns(:category)
60
+ assert_equal mock_product, assigns(:product)
61
+ end
62
+
63
+ def test_expose_a_new_product_without_category
64
+ Product.expects(:new).returns(mock_product)
65
+ get :new
66
+ assert_equal nil, assigns(:category)
67
+ assert_equal mock_product, assigns(:product)
68
+ end
69
+
70
+ def test_expose_the_resquested_product_for_edition_with_category
71
+ Category.expects(:find).with('37').returns(mock_category)
72
+ mock_category.expects(:products).returns(Product)
73
+ Product.expects(:find).with('42').returns(mock_product)
74
+ get :edit, :id => '42', :category_id => '37'
75
+ assert_equal mock_category, assigns(:category)
76
+ assert_equal mock_product, assigns(:product)
77
+ end
78
+
79
+ def test_expose_the_resquested_product_for_edition_without_category
80
+ Product.expects(:find).with('42').returns(mock_product)
81
+ get :edit, :id => '42'
82
+ assert_equal nil, assigns(:category)
83
+ assert_equal mock_product, assigns(:product)
84
+ end
85
+
86
+ def test_expose_a_newly_create_product_with_category
87
+ Category.expects(:find).with('37').returns(mock_category)
88
+ mock_category.expects(:products).returns(Product)
89
+ Product.expects(:build).with({'these' => 'params'}).returns(mock_product(:save => true))
90
+ post :create, :category_id => '37', :product => {:these => 'params'}
91
+ assert_equal mock_category, assigns(:category)
92
+ assert_equal mock_product, assigns(:product)
93
+ end
94
+
95
+ def test_expose_a_newly_create_product_without_category
96
+ Product.expects(:new).with({'these' => 'params'}).returns(mock_product(:save => true))
97
+ post :create, :product => {:these => 'params'}
98
+ assert_equal nil, assigns(:category)
99
+ assert_equal mock_product, assigns(:product)
100
+ end
101
+
102
+ def test_update_the_requested_object_with_category
103
+ Category.expects(:find).with('37').returns(mock_category)
104
+ mock_category.expects(:products).returns(Product)
105
+ Product.expects(:find).with('42').returns(mock_product)
106
+ mock_product.expects(:update_attributes).with({'these' => 'params'}).returns(true)
107
+
108
+ put :update, :id => '42', :category_id => '37', :product => {:these => 'params'}
109
+ assert_equal mock_category, assigns(:category)
110
+ assert_equal mock_product, assigns(:product)
111
+ end
112
+
113
+ def test_update_the_requested_object_without_category
114
+ Product.expects(:find).with('42').returns(mock_product)
115
+ mock_product.expects(:update_attributes).with({'these' => 'params'}).returns(true)
116
+
117
+ put :update, :id => '42', :product => {:these => 'params'}
118
+ assert_equal nil, assigns(:category)
119
+ assert_equal mock_product, assigns(:product)
120
+ end
121
+
122
+ def test_the_resquested_product_is_destroyed_with_category
123
+ Category.expects(:find).with('37').returns(mock_category)
124
+ mock_category.expects(:products).returns(Product)
125
+ Product.expects(:find).with('42').returns(mock_product)
126
+ mock_product.expects(:destroy)
127
+ @controller.expects(:collection_url).returns('/')
128
+
129
+ delete :destroy, :id => '42', :category_id => '37'
130
+ assert_equal mock_category, assigns(:category)
131
+ assert_equal mock_product, assigns(:product)
132
+ end
133
+
134
+ def test_the_resquested_product_is_destroyed_without_category
135
+ Product.expects(:find).with('42').returns(mock_product)
136
+ mock_product.expects(:destroy)
137
+ @controller.expects(:collection_url).returns('/')
138
+
139
+ delete :destroy, :id => '42'
140
+ assert_equal nil, assigns(:category)
141
+ assert_equal mock_product, assigns(:product)
142
+ end
143
+
144
+ def test_polymorphic_helpers
145
+ Product.expects(:find).with(:all).returns([mock_product])
146
+ get :index
147
+
148
+ assert !@controller.send(:parent?)
149
+ assert_equal nil, assigns(:parent_type)
150
+ assert_equal nil, @controller.send(:parent_type)
151
+ assert_equal nil, @controller.send(:parent_class)
152
+ assert_equal nil, assigns(:category)
153
+ assert_equal nil, @controller.send(:parent)
154
+ end
155
+
156
+ protected
157
+ def mock_category(stubs={})
158
+ @mock_category ||= mock(stubs)
159
+ end
160
+
161
+ def mock_product(stubs={})
162
+ @mock_product ||= mock(stubs)
163
+ end
164
+ end
@@ -0,0 +1,186 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
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(:find).with(: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_resquested_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_resquested_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_resquested_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(:find).with(: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(:find).with(: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_resquested_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_resquested_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_resquested_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(:find).with(: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.dirname(__FILE__) + '/test_helper'
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,155 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Project
4
+ def to_html
5
+ 'Generated HTML'
6
+ end
7
+
8
+ def to_xml
9
+ 'Generated XML'
10
+ end
11
+
12
+ [:to_json, :to_rss, :to_rjs].each do |method|
13
+ undef_method method if respond_to? method
14
+ end
15
+ end
16
+
17
+ class ProjectsController < ActionController::Base
18
+ respond_to :html
19
+ respond_to :xml, :except => :edit
20
+ respond_to :rjs, :only => :edit
21
+ respond_to :rss, :only => :index
22
+ respond_to :json, :except => :index
23
+ respond_to :csv, :except => :index
24
+
25
+ def index
26
+ respond_with(Project.new)
27
+ end
28
+
29
+ def respond_with_resource
30
+ respond_with(Project.new)
31
+ end
32
+
33
+ def respond_with_resource_and_options
34
+ respond_with(Project.new, :location => 'http://test.host/')
35
+ end
36
+
37
+ def respond_with_resource_and_blocks
38
+ respond_with(Project.new) do |format|
39
+ format.json { render :text => 'Render JSON' }
40
+ format.rss { render :text => 'Render RSS' }
41
+ end
42
+ end
43
+
44
+ # If the user request Mime::ALL and we have a template called action.html.erb,
45
+ # the html template should be rendered *unless* html is specified inside the
46
+ # block. This tests exactly this case.
47
+ #
48
+ def respond_to_skip_default_template
49
+ respond_with(Project.new) do |format|
50
+ format.html { render :text => 'Render HTML' }
51
+ end
52
+ end
53
+ end
54
+
55
+ class SuperProjectsController < ProjectsController
56
+ end
57
+
58
+ class RespondToFunctionalTest < ActionController::TestCase
59
+ tests ProjectsController
60
+
61
+ def test_respond_with_layout_rendering
62
+ @request.accept = 'text/html'
63
+ get :index
64
+ assert_equal 'Index HTML', @response.body.strip
65
+ end
66
+
67
+ def test_respond_with_calls_to_format_on_resource
68
+ @request.accept = 'application/xml'
69
+ get :index
70
+ assert_equal 'Generated XML', @response.body.strip
71
+ end
72
+
73
+ def test_respond_with_inherits_format
74
+ @request.accept = 'application/xml'
75
+ get :index
76
+ assert_equal 'Generated XML', @response.body.strip
77
+ end
78
+
79
+ def test_respond_with_renders_status_not_acceptable_if_mime_type_is_not_registered
80
+ @request.accept = 'text/csv'
81
+ get :index
82
+ assert_equal '406 Not Acceptable', @response.status
83
+ end
84
+
85
+ def test_respond_with_raises_error_if_could_not_respond
86
+ @request.accept = 'application/rss+xml'
87
+ assert_raise ActionView::MissingTemplate do
88
+ get :index
89
+ end
90
+ end
91
+
92
+ def test_respond_to_all
93
+ @request.accept = '*/*'
94
+ get :index
95
+ assert_equal 'Index HTML', @response.body.strip
96
+ end
97
+
98
+ def test_respond_with_sets_content_type_properly
99
+ @request.accept = 'text/html'
100
+ get :index
101
+ assert_equal 'text/html', @response.content_type
102
+ assert_equal :html, @response.template.template_format
103
+
104
+ @request.accept = 'application/xml'
105
+ get :index
106
+ assert_equal 'application/xml', @response.content_type
107
+ assert_equal :xml, @response.template.template_format
108
+ end
109
+
110
+ def test_respond_with_forwads_extra_options_to_render
111
+ @request.accept = 'application/xml'
112
+ get :respond_with_resource_and_options
113
+ assert_equal 'Generated XML', @response.body.strip
114
+ assert_equal 'http://test.host/', @response.headers['Location']
115
+ end
116
+
117
+ def test_respond_to_when_a_resource_is_given_as_option
118
+ @request.accept = 'text/html'
119
+ get :respond_with_resource
120
+ assert_equal 'RespondTo HTML', @response.body.strip
121
+
122
+ @request.accept = 'application/xml'
123
+ get :respond_with_resource
124
+ assert_equal 'Generated XML', @response.body.strip
125
+
126
+ @request.accept = 'application/rss+xml'
127
+ get :respond_with_resource
128
+ assert_equal '406 Not Acceptable', @response.status
129
+
130
+ @request.accept = 'application/json'
131
+ assert_raise ActionView::MissingTemplate do
132
+ get :respond_with_resource
133
+ end
134
+ end
135
+
136
+ def test_respond_to_overwrite_class_method_definition
137
+ @request.accept = 'application/rss+xml'
138
+ get :respond_with_resource_and_blocks
139
+ assert_equal 'Render RSS', @response.body.strip
140
+ end
141
+
142
+ def test_respond_to_first_configured_mime_in_respond_to_when_mime_type_is_all
143
+ @request.accept = '*/*'
144
+ assert_raise ActionView::MissingTemplate do
145
+ get :respond_with_resource_and_blocks
146
+ end
147
+ assert_equal 'text/html', @response.content_type
148
+ end
149
+
150
+ def test_respond_to_skip_default_template_when_it_is_in_block
151
+ @request.accept = '*/*'
152
+ get :respond_to_skip_default_template
153
+ assert_equal 'Render HTML', @response.body.strip
154
+ end
155
+ end