emmanuel-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.
- data/CHANGELOG +95 -0
- data/MIT-LICENSE +20 -0
- data/README +528 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/lib/inherited_resources/actions.rb +74 -0
- data/lib/inherited_resources/base.rb +42 -0
- data/lib/inherited_resources/base_helpers.rb +333 -0
- data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
- data/lib/inherited_resources/class_methods.rb +334 -0
- data/lib/inherited_resources/dumb_responder.rb +20 -0
- data/lib/inherited_resources/has_scope_helpers.rb +65 -0
- data/lib/inherited_resources/legacy/respond_to.rb +151 -0
- data/lib/inherited_resources/legacy/responder.rb +181 -0
- data/lib/inherited_resources/polymorphic_helpers.rb +155 -0
- data/lib/inherited_resources/singleton_helpers.rb +95 -0
- data/lib/inherited_resources/url_helpers.rb +173 -0
- data/lib/inherited_resources.rb +23 -0
- data/test/aliases_test.rb +139 -0
- data/test/base_helpers_test.rb +76 -0
- data/test/base_test.rb +219 -0
- data/test/belongs_to_test.rb +87 -0
- data/test/class_methods_test.rb +137 -0
- data/test/customized_belongs_to_test.rb +76 -0
- data/test/defaults_test.rb +70 -0
- data/test/flash_test.rb +88 -0
- data/test/has_scope_test.rb +112 -0
- data/test/nested_belongs_to_test.rb +108 -0
- data/test/optional_belongs_to_test.rb +164 -0
- data/test/polymorphic_test.rb +186 -0
- data/test/redirect_to_test.rb +51 -0
- data/test/respond_to_test.rb +155 -0
- data/test/singleton_test.rb +83 -0
- data/test/test_helper.rb +30 -0
- data/test/url_helpers_test.rb +471 -0
- metadata +86 -0
@@ -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
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Malarz
|
4
|
+
def self.human_name; 'Painter'; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class PaintersController < InheritedResources::Base
|
8
|
+
defaults :instance_name => 'malarz', :collection_name => 'malarze',
|
9
|
+
:resource_class => Malarz, :route_prefix => nil
|
10
|
+
end
|
11
|
+
|
12
|
+
class DefaultsTest < ActionController::TestCase
|
13
|
+
tests PaintersController
|
14
|
+
|
15
|
+
def setup
|
16
|
+
@controller.stubs(:resource_url).returns('/')
|
17
|
+
@controller.stubs(:collection_url).returns('/')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_expose_all_painters_as_instance_variable
|
21
|
+
Malarz.expects(:find).with(:all).returns([mock_painter])
|
22
|
+
get :index
|
23
|
+
assert_equal [mock_painter], assigns(:malarze)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_expose_the_resquested_painter_on_show
|
27
|
+
Malarz.expects(:find).with('42').returns(mock_painter)
|
28
|
+
get :show, :id => '42'
|
29
|
+
assert_equal mock_painter, assigns(:malarz)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_expose_a_new_painter
|
33
|
+
Malarz.expects(:new).returns(mock_painter)
|
34
|
+
get :new
|
35
|
+
assert_equal mock_painter, assigns(:malarz)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_expose_the_resquested_painter_on_edit
|
39
|
+
Malarz.expects(:find).with('42').returns(mock_painter)
|
40
|
+
get :edit, :id => '42'
|
41
|
+
assert_response :success
|
42
|
+
assert_equal mock_painter, assigns(:malarz)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_expose_a_newly_create_painter_when_saved_with_success
|
46
|
+
Malarz.expects(:new).with({'these' => 'params'}).returns(mock_painter(:save => true))
|
47
|
+
post :create, :malarz => {:these => 'params'}
|
48
|
+
assert_equal mock_painter, assigns(:malarz)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_update_the_requested_object
|
52
|
+
Malarz.expects(:find).with('42').returns(mock_painter)
|
53
|
+
mock_painter.expects(:update_attributes).with({'these' => 'params'}).returns(true)
|
54
|
+
put :update, :id => '42', :malarz => {:these => 'params'}
|
55
|
+
assert_equal mock_painter, assigns(:malarz)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_the_resquested_painter_is_destroyed
|
59
|
+
Malarz.expects(:find).with('42').returns(mock_painter)
|
60
|
+
mock_painter.expects(:destroy)
|
61
|
+
delete :destroy, :id => '42'
|
62
|
+
assert_equal mock_painter, assigns(:malarz)
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
def mock_painter(stubs={})
|
67
|
+
@mock_painter ||= mock(stubs)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
data/test/flash_test.rb
ADDED
@@ -0,0 +1,88 @@
|
|
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
|
+
respond_to :xml
|
9
|
+
protected
|
10
|
+
def interpolation_options
|
11
|
+
{ :reference => 'Ocean Avenue' }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Admin; end
|
16
|
+
class Admin::AddressesController < InheritedResources::Base
|
17
|
+
respond_to :xml
|
18
|
+
protected
|
19
|
+
def interpolation_options
|
20
|
+
{ :reference => 'Ocean Avenue' }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class FlashBaseHelpersTest < ActionController::TestCase
|
25
|
+
tests AddressesController
|
26
|
+
|
27
|
+
def setup
|
28
|
+
super
|
29
|
+
@request.accept = 'application/xml'
|
30
|
+
@controller.stubs(:resource_url).returns("http://test.host/")
|
31
|
+
@controller.stubs(:collection_url).returns("http://test.host/")
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_success_flash_message_on_create_with_yml
|
35
|
+
Address.stubs(:new).returns(mock_address(:save => true))
|
36
|
+
post :create
|
37
|
+
assert_equal 'You created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_success_flash_message_on_create_with_namespaced_controller
|
41
|
+
@controller = Admin::AddressesController.new
|
42
|
+
@controller.stubs(:resource_url).returns("http://test.host/")
|
43
|
+
Address.stubs(:new).returns(mock_address(:save => true))
|
44
|
+
post :create
|
45
|
+
assert_equal 'Admin, you created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_failure_flash_message_on_create_with_namespaced_controller_actions
|
49
|
+
@controller = Admin::AddressesController.new
|
50
|
+
@controller.stubs(:resource_url).returns("http://test.host/")
|
51
|
+
Address.stubs(:new).returns(mock_address(:save => false))
|
52
|
+
post :create
|
53
|
+
assert_equal 'Admin error message.', flash[:error]
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_inherited_success_flash_message_on_update_on_namespaced_controllers
|
57
|
+
@controller = Admin::AddressesController.new
|
58
|
+
@controller.stubs(:resource_url).returns("http://test.host/")
|
59
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => true))
|
60
|
+
put :update
|
61
|
+
assert_response :success
|
62
|
+
assert_equal 'Nice! Address was updated with success!', flash[:notice]
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_success_flash_message_on_update
|
66
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => true))
|
67
|
+
put :update
|
68
|
+
assert_response :success
|
69
|
+
assert_equal 'Nice! Address was updated with success!', flash[:notice]
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_failure_flash_message_on_update
|
73
|
+
Address.stubs(:find).returns(mock_address(:update_attributes => false, :errors => {:some => :error}))
|
74
|
+
put :update
|
75
|
+
assert_equal 'Oh no! We could not update your address!', flash[:error]
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_success_flash_message_on_destroy
|
79
|
+
Address.stubs(:find).returns(mock_address(:destroy => true))
|
80
|
+
delete :destroy
|
81
|
+
assert_equal 'Address was successfully destroyed.', flash[:notice]
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
def mock_address(stubs={})
|
86
|
+
@mock_address ||= stub(stubs.merge(:to_xml => "xml"))
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Tree
|
4
|
+
def self.human_name; 'Tree'; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class TreesController < InheritedResources::Base
|
8
|
+
has_scope :color
|
9
|
+
has_scope :only_tall, :boolean => true, :only => :index
|
10
|
+
has_scope :shadown_range, :default => 10, :except => [ :index, :show, :destroy, :new ]
|
11
|
+
has_scope :root_type, :as => :root
|
12
|
+
has_scope :calculate_height, :default => proc {|c| c.session[:height] || 20 }, :only => :new
|
13
|
+
end
|
14
|
+
|
15
|
+
class HasScopeTest < ActionController::TestCase
|
16
|
+
tests TreesController
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@controller.stubs(:resource_url).returns('/')
|
20
|
+
@controller.stubs(:collection_url).returns('/')
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_boolean_scope_is_called_when_boolean_param_is_true
|
24
|
+
Tree.expects(:only_tall).with().returns(Tree).in_sequence
|
25
|
+
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
26
|
+
get :index, :only_tall => 'true'
|
27
|
+
assert_equal([mock_tree], assigns(:trees))
|
28
|
+
assert_equal({ :only_tall => 'true' }, assigns(:current_scopes))
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_boolean_scope_is_called_when_boolean_param_is_false
|
32
|
+
Tree.expects(:only_tall).never
|
33
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
34
|
+
get :index, :only_tall => 'false'
|
35
|
+
assert_equal([mock_tree], assigns(:trees))
|
36
|
+
assert_equal({ :only_tall => 'false' }, assigns(:current_scopes))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_scope_is_called_only_on_index
|
40
|
+
Tree.expects(:only_tall).never
|
41
|
+
Tree.expects(:find).with('42').returns(mock_tree)
|
42
|
+
get :show, :only_tall => 'true', :id => '42'
|
43
|
+
assert_equal(mock_tree, assigns(:tree))
|
44
|
+
assert_equal({ }, assigns(:current_scopes))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_scope_is_called_except_on_index
|
48
|
+
Tree.expects(:shadown_range).with().never
|
49
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
50
|
+
get :index, :shadown_range => 20
|
51
|
+
assert_equal([mock_tree], assigns(:trees))
|
52
|
+
assert_equal({ }, assigns(:current_scopes))
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_scope_is_called_with_arguments
|
56
|
+
Tree.expects(:color).with('blue').returns(Tree).in_sequence
|
57
|
+
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
58
|
+
get :index, :color => 'blue'
|
59
|
+
assert_equal([mock_tree], assigns(:trees))
|
60
|
+
assert_equal({ :color => 'blue' }, assigns(:current_scopes))
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_multiple_scopes_are_called
|
64
|
+
Tree.expects(:only_tall).with().returns(Tree)
|
65
|
+
Tree.expects(:color).with('blue').returns(Tree)
|
66
|
+
Tree.expects(:find).with(:all).returns([mock_tree])
|
67
|
+
get :index, :color => 'blue', :only_tall => 'true'
|
68
|
+
assert_equal([mock_tree], assigns(:trees))
|
69
|
+
assert_equal({ :color => 'blue', :only_tall => 'true' }, assigns(:current_scopes))
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_scope_is_called_with_default_value
|
73
|
+
Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence
|
74
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
75
|
+
get :edit, :id => '42'
|
76
|
+
assert_equal(mock_tree, assigns(:tree))
|
77
|
+
assert_equal({ :shadown_range => 10 }, assigns(:current_scopes))
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_default_scope_value_can_be_overwritten
|
81
|
+
Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence
|
82
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
83
|
+
get :edit, :id => '42', :shadown_range => '20'
|
84
|
+
assert_equal(mock_tree, assigns(:tree))
|
85
|
+
assert_equal({ :shadown_range => '20' }, assigns(:current_scopes))
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_scope_with_different_key
|
89
|
+
Tree.expects(:root_type).with('outside').returns(Tree).in_sequence
|
90
|
+
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
91
|
+
get :show, :id => '42', :root => 'outside'
|
92
|
+
assert_equal(mock_tree, assigns(:tree))
|
93
|
+
assert_equal({ :root => 'outside' }, assigns(:current_scopes))
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_scope_with_default_value_as_proc
|
97
|
+
session[:height] = 100
|
98
|
+
Tree.expects(:calculate_height).with(100).returns(Tree).in_sequence
|
99
|
+
Tree.expects(:new).returns(mock_tree).in_sequence
|
100
|
+
get :new
|
101
|
+
assert_equal(mock_tree, assigns(:tree))
|
102
|
+
assert_equal({ :calculate_height => 100 }, assigns(:current_scopes))
|
103
|
+
end
|
104
|
+
|
105
|
+
protected
|
106
|
+
|
107
|
+
def mock_tree(stubs={})
|
108
|
+
@mock_tree ||= mock(stubs)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Country
|
4
|
+
end
|
5
|
+
|
6
|
+
class State
|
7
|
+
end
|
8
|
+
|
9
|
+
class City
|
10
|
+
def self.human_name; 'City'; end
|
11
|
+
end
|
12
|
+
|
13
|
+
class CitiesController < InheritedResources::Base
|
14
|
+
belongs_to :country, :state
|
15
|
+
end
|
16
|
+
|
17
|
+
class NestedBelongsToTest < ActionController::TestCase
|
18
|
+
tests CitiesController
|
19
|
+
|
20
|
+
def setup
|
21
|
+
Country.expects(:find).with('13').returns(mock_country)
|
22
|
+
mock_country.expects(:states).returns(State)
|
23
|
+
State.expects(:find).with('37').returns(mock_state)
|
24
|
+
mock_state.expects(:cities).returns(City)
|
25
|
+
|
26
|
+
@controller.stubs(:resource_url).returns('/')
|
27
|
+
@controller.stubs(:collection_url).returns('/')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_assigns_country_and_state_and_city_on_create
|
31
|
+
City.expects(:find).with(:all).returns([mock_city])
|
32
|
+
get :index, :state_id => '37', :country_id => '13'
|
33
|
+
|
34
|
+
assert_equal mock_country, assigns(:country)
|
35
|
+
assert_equal mock_state, assigns(:state)
|
36
|
+
assert_equal [mock_city], assigns(:cities)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_assigns_country_and_state_and_city_on_show
|
40
|
+
City.expects(:find).with('42').returns(mock_city)
|
41
|
+
get :show, :id => '42', :state_id => '37', :country_id => '13'
|
42
|
+
|
43
|
+
assert_equal mock_country, assigns(:country)
|
44
|
+
assert_equal mock_state, assigns(:state)
|
45
|
+
assert_equal mock_city, assigns(:city)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_assigns_country_and_state_and_city_on_new
|
49
|
+
City.expects(:build).returns(mock_city)
|
50
|
+
get :new, :state_id => '37', :country_id => '13'
|
51
|
+
|
52
|
+
assert_equal mock_country, assigns(:country)
|
53
|
+
assert_equal mock_state, assigns(:state)
|
54
|
+
assert_equal mock_city, assigns(:city)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_assigns_country_and_state_and_city_on_edit
|
58
|
+
City.expects(:find).with('42').returns(mock_city)
|
59
|
+
get :edit, :id => '42', :state_id => '37', :country_id => '13'
|
60
|
+
|
61
|
+
assert_equal mock_country, assigns(:country)
|
62
|
+
assert_equal mock_state, assigns(:state)
|
63
|
+
assert_equal mock_city, assigns(:city)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_assigns_country_and_state_and_city_on_create
|
67
|
+
City.expects(:build).with({'these' => 'params'}).returns(mock_city)
|
68
|
+
mock_city.expects(:save).returns(true)
|
69
|
+
post :create, :state_id => '37', :country_id => '13', :city => {:these => 'params'}
|
70
|
+
|
71
|
+
assert_equal mock_country, assigns(:country)
|
72
|
+
assert_equal mock_state, assigns(:state)
|
73
|
+
assert_equal mock_city, assigns(:city)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_assigns_country_and_state_and_city_on_update
|
77
|
+
City.expects(:find).with('42').returns(mock_city)
|
78
|
+
mock_city.expects(:update_attributes).returns(true)
|
79
|
+
put :update, :id => '42', :state_id => '37', :country_id => '13', :city => {:these => 'params'}
|
80
|
+
|
81
|
+
assert_equal mock_country, assigns(:country)
|
82
|
+
assert_equal mock_state, assigns(:state)
|
83
|
+
assert_equal mock_city, assigns(:city)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_assigns_country_and_state_and_city_on_destroy
|
87
|
+
City.expects(:find).with('42').returns(mock_city)
|
88
|
+
mock_city.expects(:destroy)
|
89
|
+
delete :destroy, :id => '42', :state_id => '37', :country_id => '13'
|
90
|
+
|
91
|
+
assert_equal mock_country, assigns(:country)
|
92
|
+
assert_equal mock_state, assigns(:state)
|
93
|
+
assert_equal mock_city, assigns(:city)
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
def mock_country(stubs={})
|
98
|
+
@mock_country ||= mock(stubs)
|
99
|
+
end
|
100
|
+
|
101
|
+
def mock_state(stubs={})
|
102
|
+
@mock_state ||= mock(stubs)
|
103
|
+
end
|
104
|
+
|
105
|
+
def mock_city(stubs={})
|
106
|
+
@mock_city ||= mock(stubs)
|
107
|
+
end
|
108
|
+
end
|
@@ -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
|