josevalim-inherited_resources 0.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/README +28 -20
- data/lib/inherited_resources/base_helpers.rb +3 -3
- data/lib/inherited_resources/polymorphic_helpers.rb +1 -1
- data/lib/inherited_resources/url_helpers.rb +14 -10
- data/test/url_helpers_test.rb +86 -48
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Inherited Resources
|
2
2
|
License: MIT
|
3
|
-
Version: 0.4
|
3
|
+
Version: 0.4.1
|
4
4
|
|
5
5
|
You can also read this README in pretty html at the GitHub project Wiki page:
|
6
6
|
|
@@ -395,31 +395,39 @@ It will deal with everything again and hide the action :index from you.
|
|
395
395
|
URL Helpers
|
396
396
|
-----------
|
397
397
|
|
398
|
-
When you use InheritedResources it creates some URL helpers
|
399
|
-
And they handle everything for you.
|
398
|
+
When you use InheritedResources it creates some URL helpers.
|
399
|
+
And they handle everything for you. :)
|
400
400
|
|
401
401
|
# /posts/1/comments
|
402
|
-
resource_url
|
403
|
-
resource_url(comment)
|
404
|
-
new_resource_url
|
405
|
-
edit_resource_url
|
406
|
-
|
402
|
+
resource_url # => /posts/1/comments/#{@comment.to_param}
|
403
|
+
resource_url(comment) # => /posts/1/comments/#{comment.to_param}
|
404
|
+
new_resource_url # => /posts/1/comments/new
|
405
|
+
edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
|
406
|
+
edit_resource_url(comment) #=> /posts/1/comments/#{comment.to_param}/edit
|
407
|
+
collection_url # => /posts/1/comments
|
407
408
|
|
408
409
|
# /projects/1/tasks
|
409
|
-
resource_url
|
410
|
-
resource_url(task)
|
411
|
-
new_resource_url
|
412
|
-
edit_resource_url
|
413
|
-
|
414
|
-
|
410
|
+
resource_url # => /products/1/tasks/#{@task.to_param}
|
411
|
+
resource_url(task) # => /products/1/tasks/#{task.to_param}
|
412
|
+
new_resource_url # => /products/1/tasks/new
|
413
|
+
edit_resource_url # => /products/1/tasks/#{@task.to_param}/edit
|
414
|
+
edit_resource_url(task) # => /products/1/tasks/#{task.to_param}/edit
|
415
|
+
collection_url # => /products/1/tasks
|
416
|
+
|
415
417
|
# /users
|
416
|
-
resource_url
|
417
|
-
resource_url(user)
|
418
|
-
new_resource_url
|
419
|
-
edit_resource_url
|
420
|
-
|
418
|
+
resource_url # => /users/#{@user.to_param}
|
419
|
+
resource_url(user) # => /users/#{user.to_param}
|
420
|
+
new_resource_url # => /users/new
|
421
|
+
edit_resource_url # => /users/#{@user.to_param}/edit
|
422
|
+
edit_resource_url(user) # => /users/#{user.to_param}/edit
|
423
|
+
collection_url # => /users
|
424
|
+
|
425
|
+
Those urls helpers also accepts a hash as options, just as in named routes.
|
426
|
+
|
427
|
+
# /projects/1/tasks
|
428
|
+
collection_url(:page => 1, :limit => 10) #=> /products/1/tasks?page=1&limit=10
|
421
429
|
|
422
|
-
|
430
|
+
Another nice thing is that those urls are not guessed during runtime. They are
|
423
431
|
all created when your application is loaded (except for polymorphic
|
424
432
|
associations, that relies on Rails polymorphic_url).
|
425
433
|
|
@@ -88,14 +88,14 @@ module InheritedResources #:nodoc:
|
|
88
88
|
# set at begin_of_association_chain is not nil.
|
89
89
|
#
|
90
90
|
def parent?
|
91
|
-
|
91
|
+
false
|
92
92
|
end
|
93
93
|
|
94
94
|
# This methods gets your begin_of_association_chain and returns the
|
95
95
|
# scoped association.
|
96
96
|
#
|
97
97
|
def end_of_association_chain
|
98
|
-
if parent?
|
98
|
+
if begin_of_association_chain || parent?
|
99
99
|
begin_of_association_chain.send(resource_collection_name)
|
100
100
|
else
|
101
101
|
resource_class
|
@@ -105,7 +105,7 @@ module InheritedResources #:nodoc:
|
|
105
105
|
# Returns the appropriated method to build the resource.
|
106
106
|
#
|
107
107
|
def method_for_build
|
108
|
-
parent? ? :build : :new
|
108
|
+
(begin_of_association_chain || parent?) ? :build : :new
|
109
109
|
end
|
110
110
|
|
111
111
|
# Get resource ivar based on the current resource controller.
|
@@ -105,13 +105,10 @@ module InheritedResources #:nodoc:
|
|
105
105
|
ivars = resource_ivars.map{|i| i == :parent ? :parent : "@#{i}" }
|
106
106
|
|
107
107
|
# If it's not a singleton, ivars are not empty, not a collection or
|
108
|
-
# not a new named route, we can
|
108
|
+
# not a "new" named route, we can pass a resource as argument.
|
109
109
|
#
|
110
|
-
|
111
|
-
ivars.push "(
|
112
|
-
'given_arg=nil'
|
113
|
-
else
|
114
|
-
''
|
110
|
+
unless base.singleton || ivars.empty? || name == :collection || prefix == :new
|
111
|
+
ivars.push "(given_args.first || #{ivars.pop})"
|
115
112
|
end
|
116
113
|
|
117
114
|
# When polymorphic is true, the segments must be replace by :polymorphic
|
@@ -167,8 +164,10 @@ module InheritedResources #:nodoc:
|
|
167
164
|
ivars << 'resource_class.new'
|
168
165
|
end
|
169
166
|
|
167
|
+
ivars = "[#{ivars.join(', ')}]"
|
168
|
+
|
170
169
|
# Add compact to deal with polymorphic optional associations.
|
171
|
-
ivars
|
170
|
+
ivars << '.compact' if base.resources_configuration[:polymorphic][:optional]
|
172
171
|
else
|
173
172
|
# In the last case, if segments is empty (this usually happens with
|
174
173
|
# root singleton resources, we set it to root)
|
@@ -177,15 +176,20 @@ module InheritedResources #:nodoc:
|
|
177
176
|
ivars = ivars.join(', ')
|
178
177
|
end
|
179
178
|
|
180
|
-
prefix
|
179
|
+
prefix = prefix ? "#{prefix}_" : ''
|
180
|
+
|
181
|
+
# Add given_options to ivars
|
182
|
+
ivars << (ivars.empty? ? 'given_options' : ', given_options')
|
181
183
|
|
182
184
|
base.class_eval <<URL_HELPERS, __FILE__, __LINE__
|
183
185
|
protected
|
184
|
-
def #{prefix}#{name}_path(
|
186
|
+
def #{prefix}#{name}_path(*given_args)
|
187
|
+
given_options = given_args.extract_options!
|
185
188
|
#{prefix}#{segments}_path(#{ivars})
|
186
189
|
end
|
187
190
|
|
188
|
-
def #{prefix}#{name}_url(
|
191
|
+
def #{prefix}#{name}_url(*given_args)
|
192
|
+
given_options = given_args.extract_options!
|
189
193
|
#{prefix}#{segments}_url(#{ivars})
|
190
194
|
end
|
191
195
|
URL_HELPERS
|
data/test/url_helpers_test.rb
CHANGED
@@ -57,24 +57,28 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
57
57
|
controller.instance_variable_set('@house', :house)
|
58
58
|
|
59
59
|
[:url, :path].each do |path_or_url|
|
60
|
-
controller.expects("houses_#{path_or_url}").with().once
|
60
|
+
controller.expects("houses_#{path_or_url}").with({}).once
|
61
61
|
controller.send("collection_#{path_or_url}")
|
62
62
|
|
63
|
-
controller.expects("house_#{path_or_url}").with(:house).once
|
63
|
+
controller.expects("house_#{path_or_url}").with(:house, {}).once
|
64
64
|
controller.send("resource_#{path_or_url}")
|
65
65
|
|
66
|
-
controller.expects("new_house_#{path_or_url}").with().once
|
66
|
+
controller.expects("new_house_#{path_or_url}").with({}).once
|
67
67
|
controller.send("new_resource_#{path_or_url}")
|
68
68
|
|
69
|
-
controller.expects("edit_house_#{path_or_url}").with(:house).once
|
69
|
+
controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
|
70
70
|
controller.send("edit_resource_#{path_or_url}")
|
71
71
|
|
72
72
|
# With arg
|
73
|
-
controller.expects("house_#{path_or_url}").with(:arg).once
|
73
|
+
controller.expects("house_#{path_or_url}").with(:arg, {}).once
|
74
74
|
controller.send("resource_#{path_or_url}", :arg)
|
75
75
|
|
76
|
-
controller.expects("house_#{path_or_url}").with(:arg).once
|
76
|
+
controller.expects("house_#{path_or_url}").with(:arg, {}).once
|
77
77
|
controller.send("resource_#{path_or_url}", :arg)
|
78
|
+
|
79
|
+
# With options
|
80
|
+
controller.expects("house_#{path_or_url}").with(:arg, :page => 1).once
|
81
|
+
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
78
82
|
end
|
79
83
|
end
|
80
84
|
|
@@ -83,20 +87,22 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
83
87
|
controller.instance_variable_set('@universe', :universe)
|
84
88
|
|
85
89
|
[:url, :path].each do |path_or_url|
|
86
|
-
controller.expects("root_#{path_or_url}").with().once
|
90
|
+
controller.expects("root_#{path_or_url}").with({}).once
|
87
91
|
controller.send("collection_#{path_or_url}")
|
88
92
|
|
89
|
-
controller.expects("universe_#{path_or_url}").with().once
|
93
|
+
controller.expects("universe_#{path_or_url}").with({}).once
|
90
94
|
controller.send("resource_#{path_or_url}")
|
91
95
|
|
92
|
-
controller.expects("new_universe_#{path_or_url}").with().once
|
96
|
+
controller.expects("new_universe_#{path_or_url}").with({}).once
|
93
97
|
controller.send("new_resource_#{path_or_url}")
|
94
98
|
|
95
|
-
controller.expects("edit_universe_#{path_or_url}").with().once
|
99
|
+
controller.expects("edit_universe_#{path_or_url}").with({}).once
|
96
100
|
controller.send("edit_resource_#{path_or_url}")
|
97
101
|
|
98
|
-
# With
|
99
|
-
|
102
|
+
# With options
|
103
|
+
# Also tests that argument sent are not used
|
104
|
+
controller.expects("universe_#{path_or_url}").with(:page => 1).once
|
105
|
+
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
100
106
|
end
|
101
107
|
end
|
102
108
|
|
@@ -106,24 +112,28 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
106
112
|
controller.instance_variable_set('@table', :table)
|
107
113
|
|
108
114
|
[:url, :path].each do |path_or_url|
|
109
|
-
controller.expects("house_tables_#{path_or_url}").with(:house).once
|
115
|
+
controller.expects("house_tables_#{path_or_url}").with(:house, {}).once
|
110
116
|
controller.send("collection_#{path_or_url}")
|
111
117
|
|
112
|
-
controller.expects("house_table_#{path_or_url}").with(:house, :table).once
|
118
|
+
controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
|
113
119
|
controller.send("resource_#{path_or_url}")
|
114
120
|
|
115
|
-
controller.expects("new_house_table_#{path_or_url}").with(:house).once
|
121
|
+
controller.expects("new_house_table_#{path_or_url}").with(:house, {}).once
|
116
122
|
controller.send("new_resource_#{path_or_url}")
|
117
123
|
|
118
|
-
controller.expects("edit_house_table_#{path_or_url}").with(:house, :table).once
|
124
|
+
controller.expects("edit_house_table_#{path_or_url}").with(:house, :table, {}).once
|
119
125
|
controller.send("edit_resource_#{path_or_url}")
|
120
126
|
|
121
127
|
# With arg
|
122
|
-
controller.expects("house_table_#{path_or_url}").with(:house, :arg).once
|
128
|
+
controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
|
123
129
|
controller.send("resource_#{path_or_url}", :arg)
|
124
130
|
|
125
|
-
controller.expects("edit_house_table_#{path_or_url}").with(:house, :arg).once
|
131
|
+
controller.expects("edit_house_table_#{path_or_url}").with(:house, :arg, {}).once
|
126
132
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
133
|
+
|
134
|
+
# With options
|
135
|
+
controller.expects("house_table_#{path_or_url}").with(:house, :arg, :page => 1).once
|
136
|
+
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
127
137
|
end
|
128
138
|
end
|
129
139
|
|
@@ -133,24 +143,28 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
133
143
|
controller.instance_variable_set('@room', :room)
|
134
144
|
|
135
145
|
[:url, :path].each do |path_or_url|
|
136
|
-
controller.expects("big_house_rooms_#{path_or_url}").with(:house).once
|
146
|
+
controller.expects("big_house_rooms_#{path_or_url}").with(:house, {}).once
|
137
147
|
controller.send("collection_#{path_or_url}")
|
138
148
|
|
139
|
-
controller.expects("big_house_room_#{path_or_url}").with(:house, :room).once
|
149
|
+
controller.expects("big_house_room_#{path_or_url}").with(:house, :room, {}).once
|
140
150
|
controller.send("resource_#{path_or_url}")
|
141
151
|
|
142
|
-
controller.expects("new_big_house_room_#{path_or_url}").with(:house).once
|
152
|
+
controller.expects("new_big_house_room_#{path_or_url}").with(:house, {}).once
|
143
153
|
controller.send("new_resource_#{path_or_url}")
|
144
154
|
|
145
|
-
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :room).once
|
155
|
+
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :room, {}).once
|
146
156
|
controller.send("edit_resource_#{path_or_url}")
|
147
157
|
|
148
158
|
# With args
|
149
|
-
controller.expects("big_house_room_#{path_or_url}").with(:house, :arg).once
|
159
|
+
controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, {}).once
|
150
160
|
controller.send("resource_#{path_or_url}", :arg)
|
151
161
|
|
152
|
-
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :arg).once
|
162
|
+
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :arg, {}).once
|
153
163
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
164
|
+
|
165
|
+
# With options
|
166
|
+
controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, :page => 1).once
|
167
|
+
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
154
168
|
end
|
155
169
|
end
|
156
170
|
|
@@ -161,24 +175,28 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
161
175
|
controller.instance_variable_set('@chair', :chair)
|
162
176
|
|
163
177
|
[:url, :path].each do |path_or_url|
|
164
|
-
controller.expects("house_table_chairs_#{path_or_url}").with(:house, :table).once
|
178
|
+
controller.expects("house_table_chairs_#{path_or_url}").with(:house, :table, {}).once
|
165
179
|
controller.send("collection_#{path_or_url}")
|
166
180
|
|
167
|
-
controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :chair).once
|
181
|
+
controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
|
168
182
|
controller.send("resource_#{path_or_url}")
|
169
183
|
|
170
|
-
controller.expects("new_house_table_chair_#{path_or_url}").with(:house, :table).once
|
184
|
+
controller.expects("new_house_table_chair_#{path_or_url}").with(:house, :table, {}).once
|
171
185
|
controller.send("new_resource_#{path_or_url}")
|
172
186
|
|
173
|
-
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :chair).once
|
187
|
+
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
|
174
188
|
controller.send("edit_resource_#{path_or_url}")
|
175
189
|
|
176
190
|
# With args
|
177
|
-
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg).once
|
191
|
+
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
|
178
192
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
179
193
|
|
180
|
-
controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :arg).once
|
194
|
+
controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
|
181
195
|
controller.send("resource_#{path_or_url}", :arg)
|
196
|
+
|
197
|
+
# With options
|
198
|
+
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, :page => 1).once
|
199
|
+
controller.send("edit_resource_#{path_or_url}", :arg, :page => 1)
|
182
200
|
end
|
183
201
|
end
|
184
202
|
|
@@ -188,19 +206,22 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
188
206
|
controller.instance_variable_set('@owner', :owner)
|
189
207
|
|
190
208
|
[:url, :path].each do |path_or_url|
|
191
|
-
controller.expects("house_#{path_or_url}").with(:house).once
|
209
|
+
controller.expects("house_#{path_or_url}").with(:house, {}).once
|
192
210
|
controller.send("collection_#{path_or_url}")
|
193
211
|
|
194
|
-
controller.expects("house_owner_#{path_or_url}").with(:house).once
|
212
|
+
controller.expects("house_owner_#{path_or_url}").with(:house, {}).once
|
195
213
|
controller.send("resource_#{path_or_url}")
|
196
214
|
|
197
|
-
controller.expects("new_house_owner_#{path_or_url}").with(:house).once
|
215
|
+
controller.expects("new_house_owner_#{path_or_url}").with(:house, {}).once
|
198
216
|
controller.send("new_resource_#{path_or_url}")
|
199
217
|
|
200
|
-
controller.expects("edit_house_owner_#{path_or_url}").with(:house).once
|
218
|
+
controller.expects("edit_house_owner_#{path_or_url}").with(:house, {}).once
|
201
219
|
controller.send("edit_resource_#{path_or_url}")
|
202
220
|
|
203
|
-
|
221
|
+
# With options
|
222
|
+
# Also tests that argument sent are not used
|
223
|
+
controller.expects("house_owner_#{path_or_url}").with(:house, :page => 1).once
|
224
|
+
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
204
225
|
end
|
205
226
|
end
|
206
227
|
|
@@ -230,12 +251,16 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
230
251
|
controller.expects("edit_house_bed_#{path_or_url}").with(house, bed).once
|
231
252
|
controller.send("edit_resource_#{path_or_url}")
|
232
253
|
end
|
233
|
-
|
234
|
-
#
|
235
|
-
controller.expects("
|
254
|
+
|
255
|
+
# With options
|
256
|
+
controller.expects("house_bed_url").with(house, bed, :page => 1).once
|
257
|
+
controller.send("resource_url", :page => 1)
|
258
|
+
|
259
|
+
# With args
|
260
|
+
controller.expects("polymorphic_url").with([house, :arg], {}).once
|
236
261
|
controller.send("resource_url", :arg)
|
237
262
|
|
238
|
-
controller.expects("edit_polymorphic_url").with([house, :arg]).once
|
263
|
+
controller.expects("edit_polymorphic_url").with([house, :arg], {}).once
|
239
264
|
controller.send("edit_resource_url", :arg)
|
240
265
|
end
|
241
266
|
|
@@ -268,11 +293,15 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
268
293
|
controller.send("edit_resource_#{path_or_url}")
|
269
294
|
end
|
270
295
|
|
271
|
-
#
|
272
|
-
controller.expects("
|
296
|
+
# With options
|
297
|
+
controller.expects("house_table_dish_url").with(house, table, dish, :page => 1).once
|
298
|
+
controller.send("resource_url", :page => 1)
|
299
|
+
|
300
|
+
# With args
|
301
|
+
controller.expects("polymorphic_url").with([house, table, :arg], {}).once
|
273
302
|
controller.send("resource_url", :arg)
|
274
303
|
|
275
|
-
controller.expects("edit_polymorphic_url").with([house, table, :arg]).once
|
304
|
+
controller.expects("edit_polymorphic_url").with([house, table, :arg], {}).once
|
276
305
|
controller.send("edit_resource_url", :arg)
|
277
306
|
end
|
278
307
|
|
@@ -302,10 +331,15 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
302
331
|
|
303
332
|
controller.expects("edit_house_table_center_#{path_or_url}").with(house, table).once
|
304
333
|
controller.send("edit_resource_#{path_or_url}")
|
305
|
-
|
306
|
-
# With arg
|
307
|
-
assert_raise(ArgumentError){ controller.send("resource_#{path_or_url}", :arg) }
|
308
334
|
end
|
335
|
+
|
336
|
+
# With options
|
337
|
+
controller.expects("house_table_center_url").with(house, table, :page => 1)
|
338
|
+
controller.send("resource_url", :page => 1)
|
339
|
+
|
340
|
+
# With args
|
341
|
+
controller.expects("polymorphic_url").with([house, table, :center], {}).once
|
342
|
+
controller.send("resource_url", :arg)
|
309
343
|
end
|
310
344
|
|
311
345
|
def test_url_helpers_on_optional_polymorphic_belongs_to
|
@@ -332,11 +366,15 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
332
366
|
controller.send("edit_resource_#{path_or_url}")
|
333
367
|
end
|
334
368
|
|
335
|
-
#
|
336
|
-
controller.expects("
|
369
|
+
# With options
|
370
|
+
controller.expects("bed_url").with(bed, :page => 1).once
|
371
|
+
controller.send("resource_url", :page => 1)
|
372
|
+
|
373
|
+
# With args
|
374
|
+
controller.expects("polymorphic_url").with([:arg], {}).once
|
337
375
|
controller.send("resource_url", :arg)
|
338
376
|
|
339
|
-
controller.expects("edit_polymorphic_url").with([:arg]).once
|
377
|
+
controller.expects("edit_polymorphic_url").with([:arg], {}).once
|
340
378
|
controller.send("edit_resource_url", :arg)
|
341
379
|
end
|
342
380
|
|
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:
|
4
|
+
version: 0.4.1
|
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-
|
12
|
+
date: 2009-02-17 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|