inherited_resources 0.9.5 → 1.0.pre
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 +7 -2
- data/README.rdoc +40 -113
- data/Rakefile +3 -1
- data/init.rb +1 -0
- data/lib/inherited_resources.rb +13 -4
- data/lib/inherited_resources/actions.rb +10 -22
- data/lib/inherited_resources/base.rb +3 -2
- data/lib/inherited_resources/base_helpers.rb +22 -115
- data/lib/inherited_resources/belongs_to_helpers.rb +8 -0
- data/lib/inherited_resources/blank_slate.rb +12 -0
- data/lib/inherited_resources/class_methods.rb +8 -84
- data/lib/inherited_resources/legacy/respond_to.rb +11 -13
- data/lib/inherited_resources/legacy/responder.rb +43 -23
- data/lib/inherited_resources/locales/en.yml +10 -0
- data/lib/inherited_resources/responder.rb +6 -0
- data/lib/inherited_resources/version.rb +1 -1
- data/test/aliases_test.rb +8 -3
- data/test/base_test.rb +25 -4
- data/test/class_methods_test.rb +1 -19
- data/test/customized_base_test.rb +10 -4
- metadata +28 -9
- data/lib/inherited_resources/dumb_responder.rb +0 -20
- data/lib/inherited_resources/has_scope_helpers.rb +0 -83
- data/test/flash_test.rb +0 -88
- data/test/has_scope_test.rb +0 -139
@@ -39,11 +39,17 @@ module CarTestHelper
|
|
39
39
|
@controller = CarsController.new
|
40
40
|
@controller.request = @request = ActionController::TestRequest.new
|
41
41
|
@controller.response = @response = ActionController::TestResponse.new
|
42
|
+
@controller.stubs(:car_url).returns("/")
|
42
43
|
end
|
43
44
|
|
44
45
|
protected
|
45
|
-
def mock_car(
|
46
|
-
@mock_car ||=
|
46
|
+
def mock_car(expectations={})
|
47
|
+
@mock_car ||= begin
|
48
|
+
car = mock(expectations.except(:errors))
|
49
|
+
car.stubs(:class).returns(Car)
|
50
|
+
car.stubs(:errors).returns(expectations.fetch(:errors, {}))
|
51
|
+
car
|
52
|
+
end
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
@@ -154,9 +160,9 @@ class DestroyActionCustomizedBaseTest < ActionController::TestCase
|
|
154
160
|
end
|
155
161
|
|
156
162
|
def test_show_flash_message_when_cannot_be_deleted
|
157
|
-
Car.stubs(:get).returns(mock_car(:destroy_successfully => false))
|
163
|
+
Car.stubs(:get).returns(mock_car(:destroy_successfully => false, :errors => { :fail => true }))
|
158
164
|
delete :destroy
|
159
|
-
assert_equal flash[:
|
165
|
+
assert_equal flash[:alert], 'Car could not be destroyed.'
|
160
166
|
end
|
161
167
|
end
|
162
168
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inherited_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -11,8 +11,27 @@ cert_chain: []
|
|
11
11
|
|
12
12
|
date: 2009-12-22 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: responders
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.2"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: has_scope
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0.3"
|
34
|
+
version:
|
16
35
|
description: Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important.
|
17
36
|
email: jose.valim@gmail.com
|
18
37
|
executables: []
|
@@ -26,18 +45,20 @@ files:
|
|
26
45
|
- MIT-LICENSE
|
27
46
|
- README.rdoc
|
28
47
|
- Rakefile
|
48
|
+
- init.rb
|
29
49
|
- lib/inherited_resources.rb
|
30
50
|
- lib/inherited_resources/actions.rb
|
31
51
|
- lib/inherited_resources/base.rb
|
32
52
|
- lib/inherited_resources/base_helpers.rb
|
33
53
|
- lib/inherited_resources/belongs_to_helpers.rb
|
54
|
+
- lib/inherited_resources/blank_slate.rb
|
34
55
|
- lib/inherited_resources/class_methods.rb
|
35
56
|
- lib/inherited_resources/dsl.rb
|
36
|
-
- lib/inherited_resources/dumb_responder.rb
|
37
|
-
- lib/inherited_resources/has_scope_helpers.rb
|
38
57
|
- lib/inherited_resources/legacy/respond_to.rb
|
39
58
|
- lib/inherited_resources/legacy/responder.rb
|
59
|
+
- lib/inherited_resources/locales/en.yml
|
40
60
|
- lib/inherited_resources/polymorphic_helpers.rb
|
61
|
+
- lib/inherited_resources/responder.rb
|
41
62
|
- lib/inherited_resources/singleton_helpers.rb
|
42
63
|
- lib/inherited_resources/url_helpers.rb
|
43
64
|
- lib/inherited_resources/version.rb
|
@@ -58,9 +79,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
79
|
version:
|
59
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
81
|
requirements:
|
61
|
-
- - "
|
82
|
+
- - ">"
|
62
83
|
- !ruby/object:Gem::Version
|
63
|
-
version:
|
84
|
+
version: 1.3.1
|
64
85
|
version:
|
65
86
|
requirements: []
|
66
87
|
|
@@ -78,8 +99,6 @@ test_files:
|
|
78
99
|
- test/customized_base_test.rb
|
79
100
|
- test/customized_belongs_to_test.rb
|
80
101
|
- test/defaults_test.rb
|
81
|
-
- test/flash_test.rb
|
82
|
-
- test/has_scope_test.rb
|
83
102
|
- test/nested_belongs_to_test.rb
|
84
103
|
- test/optional_belongs_to_test.rb
|
85
104
|
- test/polymorphic_test.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module InheritedResources
|
2
|
-
# = Dumb Responder
|
3
|
-
#
|
4
|
-
# This responder discards all messages sent to him.
|
5
|
-
#
|
6
|
-
class DumbResponder
|
7
|
-
|
8
|
-
instance_methods.each do |m|
|
9
|
-
undef_method m unless m =~ /^(__|object_id)/
|
10
|
-
end
|
11
|
-
|
12
|
-
# This is like a good husband, he will just listen everything that his wife
|
13
|
-
# says (which is a lot) without complaining. :)
|
14
|
-
#
|
15
|
-
def method_missing(*args)
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
module InheritedResources
|
2
|
-
|
3
|
-
# = has_scopes
|
4
|
-
#
|
5
|
-
# This module in included in your controller when has_scope is called for the
|
6
|
-
# first time.
|
7
|
-
#
|
8
|
-
module HasScopeHelpers
|
9
|
-
TRUE_VALUES = ["true", true, "1", 1] unless self.const_defined?(:TRUE_VALUES)
|
10
|
-
|
11
|
-
protected
|
12
|
-
|
13
|
-
# Overwrites apply to scope to implement default scope logic.
|
14
|
-
#
|
15
|
-
def apply_scope_to(target_object) #:nodoc:
|
16
|
-
@current_scopes ||= {}
|
17
|
-
|
18
|
-
self.scopes_configuration.each do |scope, options|
|
19
|
-
next unless apply_scope_to_action?(options)
|
20
|
-
key = options[:as]
|
21
|
-
|
22
|
-
if params.key?(key)
|
23
|
-
value, call_scope = params[key], true
|
24
|
-
elsif options.key?(:default)
|
25
|
-
value, call_scope = options[:default], true
|
26
|
-
value = value.call(self) if value.is_a?(Proc)
|
27
|
-
end
|
28
|
-
|
29
|
-
if call_scope
|
30
|
-
@current_scopes[key] = value
|
31
|
-
|
32
|
-
if options[:boolean]
|
33
|
-
target_object = target_object.send(scope) if TRUE_VALUES.include?(value)
|
34
|
-
else
|
35
|
-
target_object = target_object.send(scope, value)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
target_object
|
41
|
-
end
|
42
|
-
|
43
|
-
# Given an options with :only and :except arrays, check if the scope
|
44
|
-
# can be performed in the current action.
|
45
|
-
#
|
46
|
-
def apply_scope_to_action?(options) #:nodoc:
|
47
|
-
return false unless applicable?(options[:if], true)
|
48
|
-
return false unless applicable?(options[:unless], false)
|
49
|
-
if options[:only].empty?
|
50
|
-
if options[:except].empty?
|
51
|
-
true
|
52
|
-
else
|
53
|
-
!options[:except].include?(action_name.to_sym)
|
54
|
-
end
|
55
|
-
else
|
56
|
-
options[:only].include?(action_name.to_sym)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# Evaluates the scope options :if or :unless. Returns true if the proc
|
61
|
-
# method, or string evals to the expected value.
|
62
|
-
#
|
63
|
-
def applicable?(string_proc_or_symbol, expected)
|
64
|
-
case string_proc_or_symbol
|
65
|
-
when String
|
66
|
-
eval(string_proc_or_symbol) == expected
|
67
|
-
when Proc
|
68
|
-
string_proc_or_symbol.call(self) == expected
|
69
|
-
when Symbol
|
70
|
-
send(string_proc_or_symbol) == expected
|
71
|
-
else
|
72
|
-
true
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
# Returns the scopes used in this action.
|
77
|
-
#
|
78
|
-
def current_scopes
|
79
|
-
@current_scopes || {}
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
end
|
data/test/flash_test.rb
DELETED
@@ -1,88 +0,0 @@
|
|
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
|
data/test/has_scope_test.rb
DELETED
@@ -1,139 +0,0 @@
|
|
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, :unless => :show_all_colors?
|
9
|
-
has_scope :only_tall, :boolean => true, :only => :index, :if => :restrict_to_only_tall_trees?
|
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
|
-
|
14
|
-
protected
|
15
|
-
def restrict_to_only_tall_trees?
|
16
|
-
true
|
17
|
-
end
|
18
|
-
|
19
|
-
def show_all_colors?
|
20
|
-
false
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class HasScopeTest < ActionController::TestCase
|
25
|
-
tests TreesController
|
26
|
-
|
27
|
-
def setup
|
28
|
-
@controller.stubs(:resource_url).returns('/')
|
29
|
-
@controller.stubs(:collection_url).returns('/')
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_boolean_scope_is_called_when_boolean_param_is_true
|
33
|
-
Tree.expects(:only_tall).with().returns(Tree).in_sequence
|
34
|
-
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
35
|
-
get :index, :only_tall => 'true'
|
36
|
-
assert_equal([mock_tree], assigns(:trees))
|
37
|
-
assert_equal({ :only_tall => 'true' }, assigns(:current_scopes))
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_boolean_scope_is_called_when_boolean_param_is_false
|
41
|
-
Tree.expects(:only_tall).never
|
42
|
-
Tree.expects(:find).with(:all).returns([mock_tree])
|
43
|
-
get :index, :only_tall => 'false'
|
44
|
-
assert_equal([mock_tree], assigns(:trees))
|
45
|
-
assert_equal({ :only_tall => 'false' }, assigns(:current_scopes))
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_scope_is_called_only_on_index
|
49
|
-
Tree.expects(:only_tall).never
|
50
|
-
Tree.expects(:find).with('42').returns(mock_tree)
|
51
|
-
get :show, :only_tall => 'true', :id => '42'
|
52
|
-
assert_equal(mock_tree, assigns(:tree))
|
53
|
-
assert_equal({ }, assigns(:current_scopes))
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_scope_is_skipped_when_if_option_is_false
|
57
|
-
@controller.stubs(:restrict_to_only_tall_trees?).returns(false)
|
58
|
-
Tree.expects(:only_tall).never
|
59
|
-
Tree.expects(:find).with(:all).returns([mock_tree])
|
60
|
-
get :index, :only_tall => 'true'
|
61
|
-
assert_equal([mock_tree], assigns(:trees))
|
62
|
-
assert_equal({ }, assigns(:current_scopes))
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_scope_is_skipped_when_unless_option_is_true
|
66
|
-
@controller.stubs(:show_all_colors?).returns(true)
|
67
|
-
Tree.expects(:color).never
|
68
|
-
Tree.expects(:find).with(:all).returns([mock_tree])
|
69
|
-
get :index, :color => 'blue'
|
70
|
-
assert_equal([mock_tree], assigns(:trees))
|
71
|
-
assert_equal({ }, assigns(:current_scopes))
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_scope_is_called_except_on_index
|
75
|
-
Tree.expects(:shadown_range).with().never
|
76
|
-
Tree.expects(:find).with(:all).returns([mock_tree])
|
77
|
-
get :index, :shadown_range => 20
|
78
|
-
assert_equal([mock_tree], assigns(:trees))
|
79
|
-
assert_equal({ }, assigns(:current_scopes))
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_scope_is_called_with_arguments
|
83
|
-
Tree.expects(:color).with('blue').returns(Tree).in_sequence
|
84
|
-
Tree.expects(:find).with(:all).returns([mock_tree]).in_sequence
|
85
|
-
get :index, :color => 'blue'
|
86
|
-
assert_equal([mock_tree], assigns(:trees))
|
87
|
-
assert_equal({ :color => 'blue' }, assigns(:current_scopes))
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_multiple_scopes_are_called
|
91
|
-
Tree.expects(:only_tall).with().returns(Tree)
|
92
|
-
Tree.expects(:color).with('blue').returns(Tree)
|
93
|
-
Tree.expects(:find).with(:all).returns([mock_tree])
|
94
|
-
get :index, :color => 'blue', :only_tall => 'true'
|
95
|
-
assert_equal([mock_tree], assigns(:trees))
|
96
|
-
assert_equal({ :color => 'blue', :only_tall => 'true' }, assigns(:current_scopes))
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_scope_is_called_with_default_value
|
100
|
-
Tree.expects(:shadown_range).with(10).returns(Tree).in_sequence
|
101
|
-
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
102
|
-
get :edit, :id => '42'
|
103
|
-
assert_equal(mock_tree, assigns(:tree))
|
104
|
-
assert_equal({ :shadown_range => 10 }, assigns(:current_scopes))
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_default_scope_value_can_be_overwritten
|
108
|
-
Tree.expects(:shadown_range).with('20').returns(Tree).in_sequence
|
109
|
-
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
110
|
-
get :edit, :id => '42', :shadown_range => '20'
|
111
|
-
assert_equal(mock_tree, assigns(:tree))
|
112
|
-
assert_equal({ :shadown_range => '20' }, assigns(:current_scopes))
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_scope_with_different_key
|
116
|
-
Tree.expects(:root_type).with('outside').returns(Tree).in_sequence
|
117
|
-
Tree.expects(:find).with('42').returns(mock_tree).in_sequence
|
118
|
-
get :show, :id => '42', :root => 'outside'
|
119
|
-
assert_equal(mock_tree, assigns(:tree))
|
120
|
-
assert_equal({ :root => 'outside' }, assigns(:current_scopes))
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_scope_with_default_value_as_proc
|
124
|
-
session[:height] = 100
|
125
|
-
Tree.expects(:calculate_height).with(100).returns(Tree).in_sequence
|
126
|
-
Tree.expects(:new).returns(mock_tree).in_sequence
|
127
|
-
get :new
|
128
|
-
assert_equal(mock_tree, assigns(:tree))
|
129
|
-
assert_equal({ :calculate_height => 100 }, assigns(:current_scopes))
|
130
|
-
end
|
131
|
-
|
132
|
-
protected
|
133
|
-
|
134
|
-
def mock_tree(stubs={})
|
135
|
-
@mock_tree ||= mock(stubs)
|
136
|
-
end
|
137
|
-
|
138
|
-
end
|
139
|
-
|