josevalim-inherited_resources 0.2.1 → 0.2.2

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 CHANGED
@@ -1,3 +1,9 @@
1
+ # Version 0.2.2
2
+
3
+ * Bug fix when having root singleton resources. Calling collection_url would
4
+ raise "NoMethodError _url", not it will call root_url.
5
+ * More comments on UrlHelpers.
6
+
1
7
  # Version 0.2.1
2
8
 
3
9
  * Bug fix when ApplicationController is already loaded when we load respond_to.
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  Inherited Resources
2
2
  License: MIT
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
 
5
5
  You can also read this README in pretty html at the GitHub project Wiki page:
6
6
 
@@ -44,10 +44,9 @@ module InheritedResources #:nodoc:
44
44
  resource_config = base.resources_configuration[:self]
45
45
  polymorphic = false
46
46
 
47
- # Deal with belongs_to associations.
48
- #
49
- # If we find a :polymorphic symbol, it means that we should not add
50
- # :route_name to resource_segments (which will be useless anyway).
47
+ # Deal with belongs_to associations and polymorphic associations.
48
+ # Remember that we don't have to build the segments in polymorphic cases,
49
+ # because the url will be polymorphic_url.
51
50
  #
52
51
  base.parents_symbols.map do |symbol|
53
52
  if symbol == :polymorphic
@@ -61,13 +60,18 @@ module InheritedResources #:nodoc:
61
60
  end
62
61
 
63
62
  # Deals with singleton.
64
- # We define collection_url to singletons just for compatibility mode.
65
63
  #
64
+ # If not a singleton, we add the current collection name and build the
65
+ # collection url. It can build for example:
66
+ #
67
+ # project_tasks_url
68
+ #
69
+ # If it's a singleton we also build a collection, just for compatibility.
66
70
  # The collection_url for singleton is the parent show url. For example,
67
71
  # if we have ProjectsController with ManagerController, where the second
68
- # is the singleton, the collection url would be: project_url.
72
+ # is the singleton, the collection url would be: project_url(@project).
69
73
  #
70
- # This is where you are going to be redirected after destroing the manager.
74
+ # This is where you are going to be redirected after destroying the manager.
71
75
  #
72
76
  unless base.singleton
73
77
  resource_segments << resource_config[:collection_name]
@@ -81,7 +85,15 @@ module InheritedResources #:nodoc:
81
85
  resource_segments << resource_config[:instance_name]
82
86
  generate_url_and_path_helpers(base, :new, :resource, resource_segments, resource_ivars, polymorphic)
83
87
 
84
- # We don't add the resource ivar to edit and resource url if singleton
88
+ # We don't add the resource_ivar to edit and show url if singleton.
89
+ # Singletons are simply:
90
+ #
91
+ # edit_project_manager_url(@project)
92
+ #
93
+ # Instead of:
94
+ #
95
+ # edit_project_task_url(@project, @task)
96
+ #
85
97
  resource_ivars << resource_config[:instance_name] unless base.singleton
86
98
 
87
99
  # Prepare and add resource_url and edit_resource_url
@@ -93,10 +105,10 @@ module InheritedResources #:nodoc:
93
105
  ivars = resource_ivars.map{|i| i == :parent ? :parent : "@#{i}" }
94
106
 
95
107
  # If it's not a singleton, ivars are not empty, not a collection or
96
- # not a new hew helper, we can add args to the method.
108
+ # not a new named route, we can add args to the method.
97
109
  #
98
110
  arg = unless base.singleton || ivars.empty? || name == :collection || prefix == :new
99
- ivars.push("(given_arg || #{ivars.pop})")
111
+ ivars.push "(given_arg || #{ivars.pop})"
100
112
  'given_arg=nil'
101
113
  else
102
114
  ''
@@ -110,12 +122,44 @@ module InheritedResources #:nodoc:
110
122
 
111
123
  # Customization to allow polymorphic with singletons.
112
124
  #
113
- # In such cases, we must send a string with the resource instance name
114
- # to polymorphic_url.
125
+ # Let's take the projects and companies where each one has one manager
126
+ # example. The url helpers would be:
127
+ #
128
+ # company_manager_url(@company)
129
+ # project_manager_url(@project)
130
+ #
131
+ # Notice how the manager is not sent in the helper, because it's a
132
+ # singleton. So, polymorphic urls would be:
133
+ #
134
+ # polymorphic_url(@company)
135
+ # polymorphic_url(@project)
115
136
  #
116
- # When not a singleton, but a collection or new url, we should add
117
- # resource_class.new to instance ivars to allow polymorphic_url to
118
- # deal with it properly.
137
+ # Obviously, this won't work properly. So in such cases, polymorphic
138
+ # with singlestons we have to send this:
139
+ #
140
+ # polymorphic_url(@company, 'manager')
141
+ # polymorphic_url(@project, 'manager')
142
+ #
143
+ # This is exactly what we are doing here.
144
+ #
145
+ # The other case to be handle, is collection and new helpers with
146
+ # polymorphic urls.
147
+ #
148
+ # In such cases, we usually would not send anything:
149
+ #
150
+ # project_tasks_url(@project)
151
+ # new_project_task_url(@project)
152
+ #
153
+ # But this wouldn't work with polymorphic urls by the same reason as
154
+ # singletons:
155
+ #
156
+ # polymorphic_url(@project)
157
+ # new_polymorphic_url(@project)
158
+ #
159
+ # So we have to do this:
160
+ #
161
+ # polymorphic_url(@project, Task.new)
162
+ # new_polymorphic_url(@project, Task.new)
119
163
  #
120
164
  if base.singleton
121
165
  ivars << base.resources_configuration[:self][:instance_name].inspect unless name == :collection
@@ -125,7 +169,10 @@ module InheritedResources #:nodoc:
125
169
 
126
170
  ivars = "[#{ivars.join(', ')}]"
127
171
  else
128
- segments = resource_segments.join('_')
172
+ # In the last case, if segments is empty (this usually happens with
173
+ # root singleton resources, we set it to root)
174
+ #
175
+ segments = resource_segments.empty? ? 'root' : resource_segments.join('_')
129
176
  ivars = ivars.join(', ')
130
177
  end
131
178
 
@@ -1,5 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
+ class Universe; end
4
+ class UniversesController < InheritedResources::Base
5
+ defaults :singleton => true # Let's not discuss about this :P
6
+ end
7
+
3
8
  class House; end
4
9
  class HousesController < InheritedResources::Base
5
10
  end
@@ -73,6 +78,28 @@ class UrlHelpersTest < ActiveSupport::TestCase
73
78
  end
74
79
  end
75
80
 
81
+ def test_url_helpers_on_simple_inherited_singleton_resource
82
+ controller = UniversesController.new
83
+ controller.instance_variable_set('@universe', :universe)
84
+
85
+ [:url, :path].each do |path_or_url|
86
+ controller.expects("root_#{path_or_url}").with().once
87
+ controller.send("collection_#{path_or_url}")
88
+
89
+ controller.expects("universe_#{path_or_url}").with().once
90
+ controller.send("resource_#{path_or_url}")
91
+
92
+ controller.expects("new_universe_#{path_or_url}").with().once
93
+ controller.send("new_resource_#{path_or_url}")
94
+
95
+ controller.expects("edit_universe_#{path_or_url}").with().once
96
+ controller.send("edit_resource_#{path_or_url}")
97
+
98
+ # With arg
99
+ assert_raise(ArgumentError){ controller.send("resource_#{path_or_url}", :arg) }
100
+ end
101
+ end
102
+
76
103
  def test_url_helpers_on_belongs_to
77
104
  controller = TablesController.new
78
105
  controller.instance_variable_set('@house', :house)
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.2.1
4
+ version: 0.2.2
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-02-07 00:00:00 -08:00
12
+ date: 2009-02-12 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15