josevalim-inherited_resources 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/README +132 -105
- data/lib/inherited_resources/base.rb +15 -189
- data/lib/inherited_resources/base_helpers.rb +75 -39
- data/lib/inherited_resources/belongs_to_helpers.rb +62 -40
- data/lib/inherited_resources/class_methods.rb +268 -272
- data/lib/inherited_resources/dumb_responder.rb +7 -6
- data/lib/inherited_resources/has_scope_helpers.rb +65 -0
- data/lib/inherited_resources/polymorphic_helpers.rb +96 -5
- data/lib/inherited_resources/respond_to.rb +7 -7
- data/lib/inherited_resources/singleton_helpers.rb +48 -6
- data/lib/inherited_resources/url_helpers.rb +37 -36
- data/lib/inherited_resources.rb +4 -2
- data/test/base_helpers_test.rb +3 -92
- data/test/class_methods_test.rb +116 -75
- data/test/customized_belongs_to_test.rb +76 -0
- data/test/defaults_test.rb +2 -1
- data/test/flash_test.rb +83 -0
- data/test/has_scope_test.rb +171 -0
- data/test/test_helper.rb +4 -8
- data/test/url_helpers_test.rb +3 -3
- data/test/views/branches/edit.html.erb +1 -0
- data/test/views/branches/index.html.erb +1 -0
- data/test/views/branches/new.html.erb +1 -0
- data/test/views/branches/show.html.erb +1 -0
- data/test/views/pets/index.html.erb +1 -0
- data/test/views/pets/new.html.erb +1 -0
- data/test/views/pets/show.html.erb +1 -0
- data/test/views/trees/edit.html.erb +1 -0
- data/test/views/trees/index.html.erb +1 -0
- data/test/views/trees/new.html.erb +1 -0
- data/test/views/trees/show.html.erb +1 -0
- metadata +17 -2
data/test/class_methods_test.rb
CHANGED
@@ -5,7 +5,6 @@ class Folder; end
|
|
5
5
|
|
6
6
|
class BooksController < InheritedResources::Base
|
7
7
|
actions :index, :show
|
8
|
-
defaults :route_prefix => nil
|
9
8
|
end
|
10
9
|
|
11
10
|
class ReadersController < InheritedResources::Base
|
@@ -15,28 +14,23 @@ end
|
|
15
14
|
class FoldersController < InheritedResources::Base
|
16
15
|
end
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
class Dean
|
18
|
+
def self.human_name; 'Dean'; end
|
20
19
|
end
|
21
20
|
|
22
|
-
class
|
23
|
-
|
21
|
+
class SchoolsController < InheritedResources::Base
|
22
|
+
has_scope :by_city
|
23
|
+
has_scope :featured, :boolean => true, :only => :index, :key => :by_featured
|
24
|
+
has_scope :limit, :default => 10, :except => :index, :on => :anything
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
-
:
|
28
|
-
:instance_name => :great_school,
|
29
|
-
:finder => :find_by_title!,
|
30
|
-
:param => :school_title
|
31
|
-
}
|
32
|
-
|
33
|
-
class ProfessorsController < InheritedResources::Base
|
34
|
-
belongs_to :school, BELONGS_TO_OPTIONS
|
27
|
+
class DeansController < InheritedResources::Base
|
28
|
+
belongs_to :school
|
35
29
|
end
|
36
30
|
|
37
31
|
|
38
32
|
class ActionsClassMethodTest < ActiveSupport::TestCase
|
39
|
-
def
|
33
|
+
def test_actions_are_undefined
|
40
34
|
action_methods = BooksController.send(:action_methods)
|
41
35
|
assert_equal 2, action_methods.size
|
42
36
|
|
@@ -93,90 +87,137 @@ class DefaultsClassMethodTest < ActiveSupport::TestCase
|
|
93
87
|
end
|
94
88
|
end
|
95
89
|
|
90
|
+
class BelongsToErrorsTest < ActiveSupport::TestCase
|
91
|
+
def test_belongs_to_raise_errors_with_invalid_arguments
|
92
|
+
assert_raise ArgumentError do
|
93
|
+
DeansController.send(:belongs_to)
|
94
|
+
end
|
96
95
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
def setup
|
101
|
-
GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
|
102
|
-
|
103
|
-
@controller.stubs(:resource_url).returns('/')
|
104
|
-
@controller.stubs(:collection_url).returns('/')
|
96
|
+
assert_raise ArgumentError do
|
97
|
+
DeansController.send(:belongs_to, :nice, :invalid_key => '')
|
98
|
+
end
|
105
99
|
end
|
106
100
|
|
107
|
-
def
|
108
|
-
|
109
|
-
|
110
|
-
|
101
|
+
def test_belongs_to_raises_an_error_when_multiple_associations_are_given_with_options
|
102
|
+
assert_raise ArgumentError do
|
103
|
+
DeansController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
|
104
|
+
end
|
111
105
|
end
|
112
106
|
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
107
|
+
def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_block
|
108
|
+
InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
|
109
|
+
DeansController.send(:belongs_to, :school) do
|
110
|
+
belongs_to :association
|
111
|
+
end
|
112
|
+
ensure
|
113
|
+
DeansController.send(:parents_symbols=, [:school])
|
117
114
|
end
|
118
115
|
|
119
|
-
def
|
120
|
-
|
121
|
-
|
122
|
-
|
116
|
+
def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_multiple_blocks
|
117
|
+
InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
|
118
|
+
DeansController.send(:belongs_to, :school) do
|
119
|
+
belongs_to :association do
|
120
|
+
belongs_to :nested
|
121
|
+
end
|
122
|
+
end
|
123
|
+
ensure
|
124
|
+
DeansController.send(:parents_symbols=, [:school])
|
123
125
|
end
|
124
126
|
|
125
|
-
def
|
126
|
-
|
127
|
-
|
128
|
-
|
127
|
+
def test_belongs_to_raises_an_error_when_multiple_associations_are_given_with_block
|
128
|
+
assert_raise ArgumentError, "You cannot define multiple associations and give a block to belongs_to." do
|
129
|
+
DeansController.send(:belongs_to, :school, :another) do
|
130
|
+
belongs_to :association
|
131
|
+
end
|
132
|
+
end
|
133
|
+
ensure
|
134
|
+
DeansController.send(:parents_symbols=, [:school])
|
129
135
|
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class HasScopeClassMethods < ActiveSupport::TestCase
|
130
139
|
|
131
|
-
def
|
132
|
-
|
133
|
-
|
134
|
-
|
140
|
+
def test_scope_configuration_is_stored_as_hashes
|
141
|
+
config = SchoolsController.send(:scopes_configuration)
|
142
|
+
assert config.key?(:school)
|
143
|
+
assert config.key?(:anything)
|
144
|
+
|
145
|
+
assert config[:school].key?(:by_city)
|
146
|
+
assert config[:school].key?(:featured)
|
147
|
+
assert config[:anything].key?(:limit)
|
148
|
+
|
149
|
+
assert_equal config[:school][:by_city], { :key => :by_city, :only => [], :except => [] }
|
150
|
+
assert_equal config[:school][:featured], { :key => :by_featured, :only => [ :index ], :except => [], :boolean => true }
|
151
|
+
assert_equal config[:anything][:limit], { :key => :limit, :except => [ :index ], :only => [], :default => 10 }
|
135
152
|
end
|
136
153
|
|
137
|
-
def
|
138
|
-
|
139
|
-
|
140
|
-
|
154
|
+
def test_scope_on_value_is_guessed_inside_belongs_to_blocks
|
155
|
+
DeansController.send(:has_scope, :limit)
|
156
|
+
DeansController.send(:belongs_to, :school) do
|
157
|
+
has_scope :featured
|
158
|
+
has_scope :another, :on => :dean
|
159
|
+
end
|
160
|
+
|
161
|
+
config = DeansController.send(:scopes_configuration)
|
162
|
+
assert config[:school].key?(:featured)
|
163
|
+
assert config[:dean].key?(:limit)
|
164
|
+
assert config[:dean].key?(:another)
|
165
|
+
ensure
|
166
|
+
DeansController.send(:scopes_configuration=, {})
|
141
167
|
end
|
142
168
|
|
143
|
-
def
|
144
|
-
|
145
|
-
|
146
|
-
|
169
|
+
def test_scope_is_loaded_from_another_controller
|
170
|
+
DeansController.send(:load_scopes_from, SchoolsController)
|
171
|
+
config = DeansController.send(:scopes_configuration)
|
172
|
+
|
173
|
+
assert config.key?(:school)
|
174
|
+
assert config.key?(:anything)
|
175
|
+
|
176
|
+
assert config[:school].key?(:by_city)
|
177
|
+
assert config[:school].key?(:featured)
|
178
|
+
assert config[:anything].key?(:limit)
|
179
|
+
ensure
|
180
|
+
DeansController.send(:scopes_configuration=, {})
|
147
181
|
end
|
148
182
|
|
149
|
-
|
183
|
+
def test_scope_is_deep_merged_from_another_controller
|
184
|
+
config = DeansController.send(:scopes_configuration)
|
150
185
|
|
151
|
-
|
152
|
-
|
153
|
-
end
|
186
|
+
DeansController.send(:has_scope, :featured, :on => :school)
|
187
|
+
assert_equal config[:school][:featured], { :key => :featured, :only => [ ], :except => [] }
|
154
188
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
189
|
+
DeansController.send(:load_scopes_from, SchoolsController)
|
190
|
+
assert config.key?(:school)
|
191
|
+
assert config[:school].key?(:by_city)
|
192
|
+
assert config[:school].key?(:featured)
|
193
|
+
assert_equal config[:school][:featured], { :key => :by_featured, :only => [ :index ], :except => [], :boolean => true }
|
194
|
+
end
|
159
195
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
ProfessorsController.send(:belongs_to)
|
164
|
-
end
|
196
|
+
def test_scope_is_loaded_from_another_controller_with_on_specified
|
197
|
+
DeansController.send(:load_scopes_from, SchoolsController, :on => :school)
|
198
|
+
config = DeansController.send(:scopes_configuration)
|
165
199
|
|
166
|
-
|
167
|
-
|
168
|
-
|
200
|
+
assert config.key?(:school)
|
201
|
+
assert config[:school].key?(:by_city)
|
202
|
+
assert config[:school].key?(:featured)
|
169
203
|
|
170
|
-
|
171
|
-
|
172
|
-
|
204
|
+
assert !config.key?(:anything)
|
205
|
+
ensure
|
206
|
+
DeansController.send(:scopes_configuration=, {})
|
173
207
|
end
|
174
208
|
|
175
|
-
def
|
176
|
-
|
177
|
-
|
209
|
+
def test_scope_is_loaded_from_another_controller_with_on_guessed
|
210
|
+
DeansController.send(:belongs_to, :school) do
|
211
|
+
load_scopes_from SchoolsController
|
212
|
+
end
|
213
|
+
config = DeansController.send(:scopes_configuration)
|
214
|
+
|
215
|
+
assert config.key?(:school)
|
216
|
+
assert config[:school].key?(:by_city)
|
217
|
+
assert config[:school].key?(:featured)
|
218
|
+
|
219
|
+
assert !config.key?(:anything)
|
178
220
|
ensure
|
179
|
-
|
180
|
-
ProfessorsController.send(:parents_symbols=, [:school])
|
221
|
+
DeansController.send(:scopes_configuration=, {})
|
181
222
|
end
|
182
223
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class GreatSchool
|
4
|
+
end
|
5
|
+
|
6
|
+
class Professor
|
7
|
+
def self.human_name; 'Professor'; end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ProfessorsController < InheritedResources::Base
|
11
|
+
belongs_to :school, :parent_class => GreatSchool, :instance_name => :great_school,
|
12
|
+
:finder => :find_by_title!, :param => :school_title
|
13
|
+
end
|
14
|
+
|
15
|
+
class CustomizedBelongsToTest < ActionController::TestCase
|
16
|
+
tests ProfessorsController
|
17
|
+
|
18
|
+
def setup
|
19
|
+
GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
|
20
|
+
@controller.stubs(:resource_url).returns('/')
|
21
|
+
@controller.stubs(:collection_url).returns('/')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_index
|
25
|
+
Professor.stubs(:find).returns([mock_professor])
|
26
|
+
get :index, :school_title => 'nice'
|
27
|
+
assert_equal mock_school, assigns(:great_school)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_show
|
31
|
+
Professor.stubs(:find).returns(mock_professor)
|
32
|
+
get :show, :school_title => 'nice'
|
33
|
+
assert_equal mock_school, assigns(:great_school)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_new
|
37
|
+
Professor.stubs(:build).returns(mock_professor)
|
38
|
+
get :new, :school_title => 'nice'
|
39
|
+
assert_equal mock_school, assigns(:great_school)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_edit
|
43
|
+
Professor.stubs(:find).returns(mock_professor)
|
44
|
+
get :edit, :school_title => 'nice'
|
45
|
+
assert_equal mock_school, assigns(:great_school)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_create
|
49
|
+
Professor.stubs(:build).returns(mock_professor(:save => true))
|
50
|
+
post :create, :school_title => 'nice'
|
51
|
+
assert_equal mock_school, assigns(:great_school)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_update
|
55
|
+
Professor.stubs(:find).returns(mock_professor(:update_attributes => true))
|
56
|
+
put :update, :school_title => 'nice'
|
57
|
+
assert_equal mock_school, assigns(:great_school)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_expose_the_resquested_school_with_chosen_instance_variable_on_destroy
|
61
|
+
Professor.stubs(:find).returns(mock_professor(:destroy => true))
|
62
|
+
delete :destroy, :school_title => 'nice'
|
63
|
+
assert_equal mock_school, assigns(:great_school)
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def mock_school(stubs={})
|
69
|
+
@mock_school ||= mock(stubs)
|
70
|
+
end
|
71
|
+
|
72
|
+
def mock_professor(stubs={})
|
73
|
+
@mock_professor ||= mock(stubs)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
data/test/defaults_test.rb
CHANGED
@@ -5,7 +5,8 @@ class Malarz
|
|
5
5
|
end
|
6
6
|
|
7
7
|
class PaintersController < InheritedResources::Base
|
8
|
-
defaults :instance_name => 'malarz', :collection_name => 'malarze',
|
8
|
+
defaults :instance_name => 'malarz', :collection_name => 'malarze',
|
9
|
+
:resource_class => Malarz, :route_prefix => nil
|
9
10
|
end
|
10
11
|
|
11
12
|
class DefaultsTest < ActionController::TestCase
|
data/test/flash_test.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Address
|
4
|
+
def self.human_name; 'Address'; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class AddressesController < InheritedResources::Base
|
8
|
+
protected
|
9
|
+
def interpolation_options
|
10
|
+
{ :reference => 'Ocean Avenue' }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Admin; end
|
15
|
+
class Admin::AddressesController < InheritedResources::Base
|
16
|
+
protected
|
17
|
+
def interpolation_options
|
18
|
+
{ :reference => 'Ocean Avenue' }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class FlashBaseHelpersTest < ActionController::TestCase
|
23
|
+
tests AddressesController
|
24
|
+
|
25
|
+
def setup
|
26
|
+
@request.accept = 'application/xml'
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_success_flash_message_on_create_with_yml
|
30
|
+
Address.stubs(:new).returns(mock_address(:save => true))
|
31
|
+
@controller.stubs(:address_url)
|
32
|
+
post :create
|
33
|
+
assert_equal 'You created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_success_flash_message_on_create_with_namespaced_controller
|
37
|
+
@controller = Admin::AddressesController.new
|
38
|
+
Address.stubs(:new).returns(mock_address(:save => true))
|
39
|
+
@controller.stubs(:address_url)
|
40
|
+
post :create
|
41
|
+
assert_equal 'Admin, you created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_failure_flash_message_on_create_with_namespaced_controller_actions
|
45
|
+
@controller = Admin::AddressesController.new
|
46
|
+
Address.stubs(:new).returns(mock_address(:save => false))
|
47
|
+
@controller.stubs(:address_url)
|
48
|
+
post :create
|
49
|
+
assert_equal 'Admin error message.', flash[:error]
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_inherited_success_flash_message_on_update_on_namespaced_controllers
|
53
|
+
@controller = Admin::AddressesController.new
|
54
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => true))
|
55
|
+
put :update
|
56
|
+
assert_response :success
|
57
|
+
assert_equal 'Nice! Address was updated with success!', flash[:notice]
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_success_flash_message_on_update
|
61
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => true))
|
62
|
+
put :update
|
63
|
+
assert_response :success
|
64
|
+
assert_equal 'Nice! Address was updated with success!', flash[:notice]
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_failure_flash_message_on_update
|
68
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => false, :errors => []))
|
69
|
+
put :update
|
70
|
+
assert_equal 'Oh no! We could not update your address!', flash[:error]
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_success_flash_message_on_destroy
|
74
|
+
Address.stubs(:find).returns(mock_address(:destroy => true))
|
75
|
+
delete :destroy
|
76
|
+
assert_equal 'Address was successfully destroyed.', flash[:notice]
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
def mock_address(stubs={})
|
81
|
+
@mock_address ||= mock(stubs)
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Tree
|
4
|
+
def self.human_name; 'Tree'; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Branch
|
8
|
+
def self.human_name; 'Branch'; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TreesController < InheritedResources::Base
|
12
|
+
has_scope :color
|
13
|
+
has_scope :only_tall, :boolean => true, :only => :index
|
14
|
+
has_scope :shadown_range, :default => 10, :except => [ :index, :show, :destroy ]
|
15
|
+
has_scope :root_type, :key => :root
|
16
|
+
has_scope :another, :on => :anything, :default => 10, :only => :destroy
|
17
|
+
end
|
18
|
+
|
19
|
+
class BranchesController < InheritedResources::Base
|
20
|
+
belongs_to :tree do
|
21
|
+
load_scopes_from TreesController
|
22
|
+
end
|
23
|
+
|
24
|
+
has_scope :by_size
|
25
|
+
end
|
26
|
+
|
27
|
+
class HasScopeTest < ActionController::TestCase
|
28
|
+
tests TreesController
|
29
|
+
|
30
|
+
def setup
|
31
|
+
@controller.stubs(:resource_url).returns('/')
|
32
|
+
@controller.stubs(:collection_url).returns('/')
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_boolean_scope_is_called_when_boolean_param_is_true
|
36
|
+
Tree.expects(:only_tall).with().returns(Tree).in_sequence
|
37
|
+
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
38
|
+
get :index, :only_tall => 'true'
|
39
|
+
assert_equal([mock_tree], assigns(:trees))
|
40
|
+
assert_equal({ :only_tall => 'true' }, assigns(:current_scopes))
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_boolean_scope_is_called_when_boolean_param_is_false
|
44
|
+
Tree.expects(:only_tall).never
|
45
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
46
|
+
get :index, :only_tall => 'false'
|
47
|
+
assert_equal([mock_tree], assigns(:trees))
|
48
|
+
assert_equal({ :only_tall => 'false' }, assigns(:current_scopes))
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_scope_is_called_only_on_index
|
52
|
+
Tree.expects(:only_tall).never
|
53
|
+
Tree.expects(:find).with('42').returns(mock_tree)
|
54
|
+
get :show, :only_tall => 'true', :id => '42'
|
55
|
+
assert_equal(mock_tree, assigns(:tree))
|
56
|
+
assert_equal({ }, assigns(:current_scopes))
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_scope_is_called_except_on_index
|
60
|
+
Tree.expects(:shadown_range).with().never
|
61
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
62
|
+
get :index, :shadown_range => 20
|
63
|
+
assert_equal([mock_tree], assigns(:trees))
|
64
|
+
assert_equal({ }, assigns(:current_scopes))
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_scope_is_called_with_arguments
|
68
|
+
Tree.expects(:color).with('blue').returns(Tree).in_sequence
|
69
|
+
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
70
|
+
get :index, :color => 'blue'
|
71
|
+
assert_equal([mock_tree], assigns(:trees))
|
72
|
+
assert_equal({ :color => 'blue' }, assigns(:current_scopes))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_multiple_scopes_are_called
|
76
|
+
Tree.expects(:only_tall).with().returns(Tree)
|
77
|
+
Tree.expects(:color).with('blue').returns(Tree)
|
78
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
79
|
+
get :index, :color => 'blue', :only_tall => 'true'
|
80
|
+
assert_equal([mock_tree], assigns(:trees))
|
81
|
+
assert_equal({ :color => 'blue', :only_tall => 'true' }, assigns(:current_scopes))
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_scope_is_called_with_default_value
|
85
|
+
Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence
|
86
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
87
|
+
get :edit, :id => '42'
|
88
|
+
assert_equal(mock_tree, assigns(:tree))
|
89
|
+
assert_equal({ :shadown_range => 10 }, assigns(:current_scopes))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_default_scope_value_can_be_overwritten
|
93
|
+
Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence
|
94
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
95
|
+
get :edit, :id => '42', :shadown_range => '20'
|
96
|
+
assert_equal(mock_tree, assigns(:tree))
|
97
|
+
assert_equal({ :shadown_range => '20' }, assigns(:current_scopes))
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_scope_with_different_key
|
101
|
+
Tree.expects(:root_type).with('outside').returns(Tree).in_sequence
|
102
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
103
|
+
get :show, :id => '42', :root => 'outside'
|
104
|
+
assert_equal(mock_tree, assigns(:tree))
|
105
|
+
assert_equal({ :root => 'outside' }, assigns(:current_scopes))
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_scope_on_another_object_is_never_called
|
109
|
+
Tree.expects(:another).never
|
110
|
+
Tree.expects(:find).with('42').returns(mock_tree)
|
111
|
+
mock_tree.expects(:destroy)
|
112
|
+
delete :destroy, :id => '42'
|
113
|
+
assert_equal(mock_tree, assigns(:tree))
|
114
|
+
assert_equal({ }, assigns(:current_scopes))
|
115
|
+
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
|
119
|
+
def mock_tree(stubs={})
|
120
|
+
@mock_tree ||= mock(stubs)
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
class NestedHasScopeTest < ActionController::TestCase
|
126
|
+
tests BranchesController
|
127
|
+
|
128
|
+
def test_scope_is_applied_on_parent
|
129
|
+
Tree.expects(:only_tall).with().returns(Tree).in_sequence
|
130
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
131
|
+
mock_tree.expects(:branches).returns(Branch).in_sequence
|
132
|
+
Branch.expects(:find).with(:all).returns([mock_branch]).in_sequence
|
133
|
+
get :index, :only_tall => 'true', :tree_id => '42'
|
134
|
+
assert_equal(mock_tree, assigns(:tree))
|
135
|
+
assert_equal([mock_branch], assigns(:branches))
|
136
|
+
assert_equal({ :only_tall => 'true' }, assigns(:current_scopes))
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_scope_is_applied_on_resource
|
140
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
141
|
+
mock_tree.expects(:branches).returns(Branch).in_sequence
|
142
|
+
Branch.expects(:by_size).with('10').returns(Branch).in_sequence
|
143
|
+
Branch.expects(:find).with(:all).returns([mock_branch]).in_sequence
|
144
|
+
get :index, :by_size => '10', :tree_id => '42'
|
145
|
+
assert_equal(mock_tree, assigns(:tree))
|
146
|
+
assert_equal([mock_branch], assigns(:branches))
|
147
|
+
assert_equal({ :by_size => '10' }, assigns(:current_scopes))
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_scope_is_applied_on_both_resource_and_parent
|
151
|
+
Tree.expects(:only_tall).with().returns(Tree).in_sequence
|
152
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
153
|
+
mock_tree.expects(:branches).returns(Branch).in_sequence
|
154
|
+
Branch.expects(:by_size).with('10').returns(Branch).in_sequence
|
155
|
+
Branch.expects(:find).with(:all).returns([mock_branch]).in_sequence
|
156
|
+
get :index, :only_tall => 'true', :by_size => '10', :tree_id => '42'
|
157
|
+
assert_equal(mock_tree, assigns(:tree))
|
158
|
+
assert_equal([mock_branch], assigns(:branches))
|
159
|
+
assert_equal({ :by_size => '10', :only_tall => 'true' }, assigns(:current_scopes))
|
160
|
+
end
|
161
|
+
|
162
|
+
protected
|
163
|
+
|
164
|
+
def mock_tree(stubs={})
|
165
|
+
@mock_tree ||= mock(stubs)
|
166
|
+
end
|
167
|
+
|
168
|
+
def mock_branch(stubs={})
|
169
|
+
@mock_branch ||= mock(stubs)
|
170
|
+
end
|
171
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'rubygems'
|
3
|
+
require 'ruby-debug'
|
3
4
|
require 'mocha'
|
4
5
|
|
5
6
|
ENV["RAILS_ENV"] = "test"
|
@@ -12,19 +13,14 @@ require 'action_controller/test_process'
|
|
12
13
|
I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
|
13
14
|
I18n.reload!
|
14
15
|
|
15
|
-
#
|
16
|
-
|
16
|
+
# Add IR to load path and load the main file
|
17
|
+
ActiveSupport::Dependencies.load_paths << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
18
|
+
require_dependency 'inherited_resources'
|
17
19
|
|
18
|
-
# Define ApplicationController
|
19
20
|
class ApplicationController < ActionController::Base; end
|
20
21
|
|
21
|
-
# Load InheritedResources::Base after defining ApplicationController
|
22
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/base.rb'
|
23
|
-
|
24
|
-
# Define view_paths
|
25
22
|
ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
|
26
23
|
|
27
|
-
# Define default routes
|
28
24
|
ActionController::Routing::Routes.draw do |map|
|
29
25
|
map.connect ':controller/:action/:id'
|
30
26
|
end
|
data/test/url_helpers_test.rb
CHANGED
@@ -31,18 +31,18 @@ class ChairsController < InheritedResources::Base
|
|
31
31
|
end
|
32
32
|
|
33
33
|
class OwnersController < InheritedResources::Base
|
34
|
-
|
34
|
+
singleton_belongs_to :house
|
35
35
|
end
|
36
36
|
|
37
37
|
class Bed; end
|
38
38
|
class BedsController < InheritedResources::Base
|
39
|
-
|
39
|
+
optional_belongs_to :house, :building
|
40
40
|
end
|
41
41
|
|
42
42
|
class Dish; end
|
43
43
|
class DishesController < InheritedResources::Base
|
44
44
|
belongs_to :house do
|
45
|
-
|
45
|
+
polymorphic_belongs_to :table, :kitchen
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -0,0 +1 @@
|
|
1
|
+
Edit HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Index HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
New HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Show HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Index HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
New HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Show HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Edit HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Index HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
New HTML
|
@@ -0,0 +1 @@
|
|
1
|
+
Show HTML
|