josevalim-inherited_resources 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,11 @@
1
+ # Version 0.4.6
2
+
3
+ * Dealing with namespaced controllers out of the box;
4
+ * Added support to namespaced routes through :route_prefix.
5
+
1
6
  # Version 0.4.5
2
7
 
3
- * Added fix when resource_url is not defined;
8
+ * Added fix when resource_url is not defined.
4
9
 
5
10
  # Version 0.4.4
6
11
 
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  Inherited Resources
2
2
  License: MIT
3
- Version: 0.4.5
3
+ Version: 0.4.6
4
4
 
5
5
  You can also read this README in pretty html at the GitHub project Wiki page:
6
6
 
@@ -111,6 +111,15 @@ just doing:
111
111
  defaults :resource_class => User, :collection_name => 'users', :instance_name => 'user'
112
112
  end
113
113
 
114
+ Namespaced controllers work out of the box, but if you need to specify a
115
+ different route prefix, you can do the following:
116
+
117
+ class Administrators::PeopleController < InheritedResources::Base
118
+ defaults :route_prefix => 'admin'
119
+ end
120
+
121
+ Then your named routes will be: 'admin_people_url', 'admin_person_url' and so on.
122
+
114
123
  Further extensions is done by overwriting two methods. Let's suppose you want
115
124
  to add pagination to your projects collection:
116
125
 
@@ -155,6 +155,16 @@
155
155
  # defaults :resource_class => User, :instance_name => 'user', :collection_name => 'users'
156
156
  # end
157
157
  #
158
+ # Namespaced controllers work out of the box, but if you need to specify a
159
+ # different route prefix, you can do the following:
160
+ #
161
+ # class Administrators::PeopleController < InheritedResources::Base
162
+ # defaults :route_prefix => 'admin'
163
+ # end
164
+ #
165
+ # Then your named routes will be: 'admin_people_url', 'admin_person_url' and
166
+ # so on.
167
+ #
158
168
  # Further customizations can be done replacing some methods. Check
159
169
  # base_helpers.rb file for more information.
160
170
 
@@ -46,9 +46,15 @@
46
46
  #
47
47
  # * route_name => Allows you to specify what is the route name in your url
48
48
  # helper. By default is 'project'. But if your url helper should be
49
- # "admin_project_task_url" instead of "project_task_url", just do:
49
+ # "myproject_task_url" instead of "project_task_url", just do:
50
50
  #
51
- # belongs_to :project, :route_name => "admin_project"
51
+ # belongs_to :project, :route_name => "myproject"
52
+ #
53
+ # But if you want to use namespaced routes, you can do:
54
+ #
55
+ # defaults :route_prefix => :admin
56
+ #
57
+ # That would generate "admin_project_task_url".
52
58
  #
53
59
  # = nested_belongs_to
54
60
  #
@@ -238,7 +244,8 @@ module InheritedResources #:nodoc:
238
244
  raise ArgumentError, 'Class method :defaults expects a hash of options.' unless options.is_a? Hash
239
245
 
240
246
  options.symbolize_keys!
241
- options.assert_valid_keys(:resource_class, :collection_name, :instance_name, :class_name, :singleton)
247
+ options.assert_valid_keys(:resource_class, :collection_name, :instance_name,
248
+ :class_name, :route_prefix, :singleton)
242
249
 
243
250
  # Checks for special argument :resource_class and :class_name and sets it right away.
244
251
  self.resource_class = options.delete(:resource_class) if options[:resource_class]
@@ -285,7 +292,9 @@ module InheritedResources #:nodoc:
285
292
  options = symbols.extract_options!
286
293
 
287
294
  options.symbolize_keys!
288
- options.assert_valid_keys(:class_name, :parent_class, :instance_name, :param, :finder, :route_name, :collection_name, :singleton, :polymorphic, :optional)
295
+ options.assert_valid_keys(:class_name, :parent_class, :instance_name, :param,
296
+ :finder, :route_name, :collection_name, :singleton,
297
+ :polymorphic, :optional)
289
298
 
290
299
  optional = options.delete(:optional)
291
300
  singleton = options.delete(:singleton)
@@ -380,6 +389,10 @@ module InheritedResources #:nodoc:
380
389
  config[:collection_name] = base.controller_name.to_sym
381
390
  config[:instance_name] = base.controller_name.singularize.to_sym
382
391
 
392
+ # Deal with namespaced controllers
393
+ namespaces = base.controller_path.split('/')[0..-2]
394
+ config[:route_prefix] = namespaces.join('_') unless namespaces.empty?
395
+
383
396
  # Initialize polymorphic, singleton and belongs_to parameters
384
397
  base.singleton = false
385
398
  base.parents_symbols = []
@@ -44,6 +44,9 @@ module InheritedResources #:nodoc:
44
44
  resource_config = base.resources_configuration[:self]
45
45
  polymorphic = false
46
46
 
47
+ # Add route_prefix if any.
48
+ resource_segments << resource_config[:route_prefix] if resource_config[:route_prefix]
49
+
47
50
  # Deal with belongs_to associations and polymorphic associations.
48
51
  # Remember that we don't have to build the segments in polymorphic cases,
49
52
  # because the url will be polymorphic_url.
@@ -0,0 +1,68 @@
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', :resource_class => Malarz
9
+ end
10
+
11
+ class DefaultsTest < TEST_CLASS
12
+ def setup
13
+ @controller = PaintersController.new
14
+ @controller.request = @request = ActionController::TestRequest.new
15
+ @controller.response = @response = ActionController::TestResponse.new
16
+ end
17
+
18
+ def test_expose_all_painters_as_instance_variable
19
+ Malarz.expects(:find).with(:all).returns([mock_painter])
20
+ get :index
21
+ assert_equal [mock_painter], assigns(:malarze)
22
+ end
23
+
24
+ def test_expose_the_resquested_painter_on_show
25
+ Malarz.expects(:find).with('42').returns(mock_painter)
26
+ get :show, :id => '42'
27
+ assert_equal mock_painter, assigns(:malarz)
28
+ end
29
+
30
+ def test_expose_a_new_painter
31
+ Malarz.expects(:new).returns(mock_painter)
32
+ get :new
33
+ assert_equal mock_painter, assigns(:malarz)
34
+ end
35
+
36
+ def test_expose_the_resquested_painter_on_edit
37
+ Malarz.expects(:find).with('42').returns(mock_painter)
38
+ get :edit, :id => '42'
39
+ assert_response :success
40
+ assert_equal mock_painter, assigns(:malarz)
41
+ end
42
+
43
+ def test_expose_a_newly_create_painter_when_saved_with_success
44
+ Malarz.expects(:new).with({'these' => 'params'}).returns(mock_painter(:save => true))
45
+ post :create, :malarz => {:these => 'params'}
46
+ assert_equal mock_painter, assigns(:malarz)
47
+ end
48
+
49
+ def test_update_the_requested_object
50
+ Malarz.expects(:find).with('42').returns(mock_painter)
51
+ mock_painter.expects(:update_attributes).with({'these' => 'params'}).returns(true)
52
+ put :update, :id => '42', :malarz => {:these => 'params'}
53
+ assert_equal mock_painter, assigns(:malarz)
54
+ end
55
+
56
+ def test_the_resquested_painter_is_destroyed
57
+ Malarz.expects(:find).with('42').returns(mock_painter)
58
+ mock_painter.expects(:destroy)
59
+ delete :destroy, :id => '42'
60
+ assert_equal mock_painter, assigns(:malarz)
61
+ end
62
+
63
+ protected
64
+ def mock_painter(stubs={})
65
+ @mock_painter ||= mock(stubs)
66
+ end
67
+ end
68
+
@@ -9,6 +9,11 @@ class House; end
9
9
  class HousesController < InheritedResources::Base
10
10
  end
11
11
 
12
+ class Backpack; end
13
+ module Admin; end
14
+ class Admin::BackpacksController < InheritedResources::Base
15
+ end
16
+
12
17
  class Table; end
13
18
  class TablesController < InheritedResources::Base
14
19
  belongs_to :house
@@ -82,6 +87,38 @@ class UrlHelpersTest < ActiveSupport::TestCase
82
87
  end
83
88
  end
84
89
 
90
+ def test_url_helpers_on_simple_inherited_namespaced_resource
91
+ controller = Admin::BackpacksController.new
92
+ controller.instance_variable_set('@backpack', :backpack)
93
+
94
+ assert_equal 'admin', controller.class.resources_configuration[:self][:route_prefix]
95
+
96
+ [:url, :path].each do |path_or_url|
97
+ controller.expects("admin_backpacks_#{path_or_url}").with({}).once
98
+ controller.send("collection_#{path_or_url}")
99
+
100
+ controller.expects("admin_backpack_#{path_or_url}").with(:backpack, {}).once
101
+ controller.send("resource_#{path_or_url}")
102
+
103
+ controller.expects("new_admin_backpack_#{path_or_url}").with({}).once
104
+ controller.send("new_resource_#{path_or_url}")
105
+
106
+ controller.expects("edit_admin_backpack_#{path_or_url}").with(:backpack, {}).once
107
+ controller.send("edit_resource_#{path_or_url}")
108
+
109
+ # With arg
110
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
111
+ controller.send("resource_#{path_or_url}", :arg)
112
+
113
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
114
+ controller.send("resource_#{path_or_url}", :arg)
115
+
116
+ # With options
117
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, :page => 1).once
118
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
119
+ end
120
+ end
121
+
85
122
  def test_url_helpers_on_simple_inherited_singleton_resource
86
123
  controller = UniversesController.new
87
124
  controller.instance_variable_set('@universe', :universe)
@@ -0,0 +1 @@
1
+ Edit HTML
@@ -0,0 +1 @@
1
+ Index HTML
@@ -0,0 +1 @@
1
+ New HTML
@@ -0,0 +1 @@
1
+ Show HTML
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.5
4
+ version: 0.4.6
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-03-06 00:00:00 -08:00
12
+ date: 2009-03-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -71,6 +71,7 @@ test_files:
71
71
  - test/base_test.rb
72
72
  - test/belongs_to_test.rb
73
73
  - test/class_methods_test.rb
74
+ - test/defaults_test.rb
74
75
  - test/nested_belongs_to_test.rb
75
76
  - test/optional_belongs_to_test.rb
76
77
  - test/polymorphic_test.rb
@@ -94,6 +95,10 @@ test_files:
94
95
  - test/views/managers/edit.html.erb
95
96
  - test/views/managers/new.html.erb
96
97
  - test/views/managers/show.html.erb
98
+ - test/views/painters/edit.html.erb
99
+ - test/views/painters/index.html.erb
100
+ - test/views/painters/new.html.erb
101
+ - test/views/painters/show.html.erb
97
102
  - test/views/pets/edit.html.erb
98
103
  - test/views/products/edit.html.erb
99
104
  - test/views/products/index.html.erb