josevalim-inherited_resources 0.5.2 → 0.6.0
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.
- data/CHANGELOG +12 -5
- data/README +13 -3
- data/lib/inherited_resources/base.rb +29 -19
- data/lib/inherited_resources/base_helpers.rb +17 -8
- data/lib/inherited_resources/respond_to.rb +26 -63
- data/test/aliases_test.rb +5 -11
- data/test/base_helpers_test.rb +5 -8
- data/test/base_test.rb +8 -11
- data/test/belongs_to_test.rb +9 -21
- data/test/class_methods_test.rb +20 -21
- data/test/defaults_test.rb +5 -4
- data/test/nested_belongs_to_test.rb +11 -41
- data/test/optional_belongs_to_test.rb +22 -48
- data/test/polymorphic_test.rb +98 -23
- data/test/redirect_to_test.rb +52 -0
- data/test/respond_to_test.rb +23 -26
- data/test/singleton_test.rb +4 -4
- data/test/test_helper.rb +0 -7
- metadata +3 -2
data/test/polymorphic_test.rb
CHANGED
@@ -11,17 +11,18 @@ class EmployeesController < InheritedResources::Base
|
|
11
11
|
belongs_to :factory, :company, :polymorphic => true
|
12
12
|
end
|
13
13
|
|
14
|
-
class
|
14
|
+
class PolymorphicFactoriesTest < ActionController::TestCase
|
15
|
+
tests EmployeesController
|
15
16
|
|
16
17
|
def setup
|
17
|
-
|
18
|
-
|
19
|
-
|
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('/')
|
20
23
|
end
|
21
24
|
|
22
25
|
def test_expose_all_employees_as_instance_variable_on_index
|
23
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
24
|
-
mock_factory.expects(:employees).returns(Employee)
|
25
26
|
Employee.expects(:find).with(:all).returns([mock_employee])
|
26
27
|
get :index, :factory_id => '37'
|
27
28
|
assert_equal mock_factory, assigns(:factory)
|
@@ -29,8 +30,6 @@ class PolymorphicTest < TEST_CLASS
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def test_expose_the_resquested_employee_on_show
|
32
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
33
|
-
mock_factory.expects(:employees).returns(Employee)
|
34
33
|
Employee.expects(:find).with('42').returns(mock_employee)
|
35
34
|
get :show, :id => '42', :factory_id => '37'
|
36
35
|
assert_equal mock_factory, assigns(:factory)
|
@@ -38,8 +37,6 @@ class PolymorphicTest < TEST_CLASS
|
|
38
37
|
end
|
39
38
|
|
40
39
|
def test_expose_a_new_employee_on_new
|
41
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
42
|
-
mock_factory.expects(:employees).returns(Employee)
|
43
40
|
Employee.expects(:build).returns(mock_employee)
|
44
41
|
get :new, :factory_id => '37'
|
45
42
|
assert_equal mock_factory, assigns(:factory)
|
@@ -47,8 +44,6 @@ class PolymorphicTest < TEST_CLASS
|
|
47
44
|
end
|
48
45
|
|
49
46
|
def test_expose_the_resquested_employee_on_edit
|
50
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
51
|
-
mock_factory.expects(:employees).returns(Employee)
|
52
47
|
Employee.expects(:find).with('42').returns(mock_employee)
|
53
48
|
get :edit, :id => '42', :factory_id => '37'
|
54
49
|
assert_equal mock_factory, assigns(:factory)
|
@@ -57,8 +52,6 @@ class PolymorphicTest < TEST_CLASS
|
|
57
52
|
end
|
58
53
|
|
59
54
|
def test_expose_a_newly_create_employee_on_create
|
60
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
61
|
-
mock_factory.expects(:employees).returns(Employee)
|
62
55
|
Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
|
63
56
|
post :create, :factory_id => '37', :employee => {:these => 'params'}
|
64
57
|
assert_equal mock_factory, assigns(:factory)
|
@@ -66,8 +59,6 @@ class PolymorphicTest < TEST_CLASS
|
|
66
59
|
end
|
67
60
|
|
68
61
|
def test_update_the_requested_object_on_update
|
69
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
70
|
-
mock_factory.expects(:employees).returns(Employee)
|
71
62
|
Employee.expects(:find).with('42').returns(mock_employee)
|
72
63
|
mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
|
73
64
|
put :update, :id => '42', :factory_id => '37', :employee => {:these => 'params'}
|
@@ -76,8 +67,6 @@ class PolymorphicTest < TEST_CLASS
|
|
76
67
|
end
|
77
68
|
|
78
69
|
def test_the_resquested_employee_is_destroyed_on_destroy
|
79
|
-
Factory.expects(:find).with('37').returns(mock_factory)
|
80
|
-
mock_factory.expects(:employees).returns(Employee)
|
81
70
|
Employee.expects(:find).with('42').returns(mock_employee)
|
82
71
|
mock_employee.expects(:destroy)
|
83
72
|
delete :destroy, :id => '42', :factory_id => '37'
|
@@ -86,9 +75,8 @@ class PolymorphicTest < TEST_CLASS
|
|
86
75
|
end
|
87
76
|
|
88
77
|
def test_polymorphic_helpers
|
89
|
-
|
90
|
-
|
91
|
-
new_factory.expects(:employees).returns(Employee)
|
78
|
+
mock_factory.stubs(:class).returns(Factory)
|
79
|
+
|
92
80
|
Employee.expects(:find).with(:all).returns([mock_employee])
|
93
81
|
get :index, :factory_id => '37'
|
94
82
|
|
@@ -96,8 +84,8 @@ class PolymorphicTest < TEST_CLASS
|
|
96
84
|
assert_equal :factory, assigns(:parent_type)
|
97
85
|
assert_equal :factory, @controller.send(:parent_type)
|
98
86
|
assert_equal Factory, @controller.send(:parent_class)
|
99
|
-
assert_equal
|
100
|
-
assert_equal
|
87
|
+
assert_equal mock_factory, assigns(:factory)
|
88
|
+
assert_equal mock_factory, @controller.send(:parent)
|
101
89
|
end
|
102
90
|
|
103
91
|
protected
|
@@ -109,3 +97,90 @@ class PolymorphicTest < TEST_CLASS
|
|
109
97
|
@mock_employee ||= mock(stubs)
|
110
98
|
end
|
111
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,52 @@
|
|
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
|
+
|
9
|
+
def create
|
10
|
+
create!('http://test.host/')
|
11
|
+
end
|
12
|
+
|
13
|
+
def update
|
14
|
+
update!('http://test.host/')
|
15
|
+
end
|
16
|
+
|
17
|
+
def destroy
|
18
|
+
destroy!('http://test.host/')
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class RedirectToTest < ActionController::TestCase
|
24
|
+
tests MachinesController
|
25
|
+
|
26
|
+
def test_redirect_to_the_given_url_on_create
|
27
|
+
Machine.stubs(:new).returns(mock_machine(:save => true))
|
28
|
+
@controller.expects(:resource_url).times(0)
|
29
|
+
post :create
|
30
|
+
assert_redirected_to 'http://test.host/'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_redirect_to_the_given_url_on_update
|
34
|
+
Machine.stubs(:find).returns(mock_machine(:update_attributes => true))
|
35
|
+
@controller.expects(:resource_url).times(0)
|
36
|
+
put :update
|
37
|
+
assert_redirected_to 'http://test.host/'
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_redirect_to_the_given_url_on_destroy
|
41
|
+
Machine.stubs(:find).returns(mock_machine(:destroy => true))
|
42
|
+
@controller.expects(:collection_url).times(0)
|
43
|
+
put :destroy
|
44
|
+
assert_redirected_to 'http://test.host/'
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
def mock_machine(stubs={})
|
49
|
+
@mock_machine ||= mock(stubs)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/test/respond_to_test.rb
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
class ProjectsController < ActionController::Base
|
18
18
|
# Inherited respond_to definition is:
|
19
19
|
# respond_to :html
|
20
|
-
|
20
|
+
respond_to :xml, :except => :edit
|
21
21
|
respond_to :html
|
22
22
|
respond_to :rjs => :edit
|
23
23
|
respond_to :rss, :only => 'index'
|
@@ -51,14 +51,12 @@ end
|
|
51
51
|
class SuperProjectsController < ProjectsController
|
52
52
|
end
|
53
53
|
|
54
|
-
class RespondToUnitTest <
|
55
|
-
|
56
|
-
@controller = class_controller.new
|
57
|
-
@controller.request = @request = ActionController::TestRequest.new
|
58
|
-
@controller.response = @response = ActionController::TestResponse.new
|
54
|
+
class RespondToUnitTest < ActionController::TestCase
|
55
|
+
tests ProjectsController
|
59
56
|
|
60
|
-
|
61
|
-
@
|
57
|
+
def setup
|
58
|
+
@formats = @controller.formats_for_respond_to
|
59
|
+
@responder = ActionController::MimeResponds::Responder.new(@controller)
|
62
60
|
end
|
63
61
|
|
64
62
|
def test_respond_to_class_method_without_options
|
@@ -109,10 +107,13 @@ class RespondToUnitTest < TEST_CLASS
|
|
109
107
|
end
|
110
108
|
|
111
109
|
def test_clear_respond_to
|
112
|
-
|
110
|
+
@controller = SuperProjectsController.new
|
111
|
+
@controller.request = ActionController::TestRequest.new
|
113
112
|
|
114
|
-
# Those responses are inherited from ProjectsController
|
115
113
|
@controller.action_name = 'index'
|
114
|
+
@responder = ActionController::MimeResponds::Responder.new(@controller)
|
115
|
+
|
116
|
+
# Those responses are inherited from ProjectsController
|
116
117
|
assert @responder.action_respond_to_format?('html') # defined
|
117
118
|
assert @responder.action_respond_to_format?('xml') # inherited
|
118
119
|
assert @responder.action_respond_to_format?('rss') # explicit only
|
@@ -125,35 +126,35 @@ class RespondToUnitTest < TEST_CLASS
|
|
125
126
|
assert !@responder.action_respond_to_format?('rss')
|
126
127
|
end
|
127
128
|
|
128
|
-
def
|
129
|
+
def test_respond_except_any_does_not_respond_to_mime_all
|
129
130
|
prepare_responder_to_respond!
|
130
131
|
|
131
|
-
@responder.
|
132
|
+
@responder.respond_except_any
|
132
133
|
assert !@performed
|
133
|
-
assert !@responder.responded?
|
134
134
|
|
135
135
|
@responder.respond
|
136
136
|
assert @performed
|
137
137
|
end
|
138
138
|
|
139
|
-
def
|
139
|
+
def test_respond_any_responds_to_mime_all
|
140
140
|
prepare_responder_to_respond!
|
141
141
|
|
142
|
-
@responder.
|
142
|
+
@responder.respond_any
|
143
143
|
assert @performed
|
144
|
-
assert @responder.responded?
|
145
144
|
end
|
146
145
|
|
147
|
-
def
|
146
|
+
def test_respond_any_responds_only_to_all
|
148
147
|
prepare_responder_to_respond!('text/html')
|
149
148
|
|
150
|
-
@responder.
|
149
|
+
@responder.respond_any
|
151
150
|
assert !@performed
|
152
|
-
assert !@responder.responded?
|
153
151
|
end
|
154
152
|
|
155
153
|
protected
|
156
|
-
def prepare_responder_to_respond!(content_type
|
154
|
+
def prepare_responder_to_respond!(content_type='*/*')
|
155
|
+
@controller.request = @request = ActionController::TestRequest.new
|
156
|
+
@controller.response = @response = ActionController::TestResponse.new
|
157
|
+
|
157
158
|
@request.accept = content_type
|
158
159
|
@responder = ActionController::MimeResponds::Responder.new(@controller)
|
159
160
|
@performed = false
|
@@ -172,12 +173,8 @@ class RespondToUnitTest < TEST_CLASS
|
|
172
173
|
end
|
173
174
|
end
|
174
175
|
|
175
|
-
class RespondToFunctionalTest <
|
176
|
-
|
177
|
-
@controller = ProjectsController.new
|
178
|
-
@controller.request = @request = ActionController::TestRequest.new
|
179
|
-
@controller.response = @response = ActionController::TestResponse.new
|
180
|
-
end
|
176
|
+
class RespondToFunctionalTest < ActionController::TestCase
|
177
|
+
tests ProjectsController
|
181
178
|
|
182
179
|
def test_respond_with_layout_rendering
|
183
180
|
@request.accept = 'text/html'
|
data/test/singleton_test.rb
CHANGED
@@ -14,12 +14,12 @@ class ManagersController < InheritedResources::Base
|
|
14
14
|
belongs_to :store, :singleton => true
|
15
15
|
end
|
16
16
|
|
17
|
-
class SingletonTest <
|
17
|
+
class SingletonTest < ActionController::TestCase
|
18
|
+
tests ManagersController
|
18
19
|
|
19
20
|
def setup
|
20
|
-
@controller
|
21
|
-
@controller.
|
22
|
-
@controller.response = @response = ActionController::TestResponse.new
|
21
|
+
@controller.stubs(:resource_url).returns('/')
|
22
|
+
@controller.stubs(:collection_url).returns('/')
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_expose_the_resquested_manager_on_show
|
data/test/test_helper.rb
CHANGED
@@ -4,18 +4,11 @@ require 'mocha'
|
|
4
4
|
|
5
5
|
ENV["RAILS_ENV"] = "test"
|
6
6
|
|
7
|
-
# Used to tests Rails 2.2.2 version
|
8
|
-
# gem 'activesupport', '2.2.2'
|
9
|
-
# gem 'actionpack', '2.2.2'
|
10
|
-
# TEST_CLASS = Test::Unit::TestCase
|
11
|
-
|
12
7
|
require 'active_support'
|
13
8
|
require 'action_controller'
|
14
9
|
require 'action_controller/test_case'
|
15
10
|
require 'action_controller/test_process'
|
16
11
|
|
17
|
-
TEST_CLASS = ActionController::TestCase
|
18
|
-
|
19
12
|
I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
|
20
13
|
I18n.reload!
|
21
14
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: josevalim-inherited_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-04-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -75,6 +75,7 @@ test_files:
|
|
75
75
|
- test/nested_belongs_to_test.rb
|
76
76
|
- test/optional_belongs_to_test.rb
|
77
77
|
- test/polymorphic_test.rb
|
78
|
+
- test/redirect_to_test.rb
|
78
79
|
- test/respond_to_test.rb
|
79
80
|
- test/singleton_test.rb
|
80
81
|
- test/test_helper.rb
|