josevalim-inherited_resources 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +13 -3
- data/lib/inherited_resources/base.rb +2 -1
- data/lib/inherited_resources/belongs_to.rb +1 -1
- data/lib/inherited_resources/class_methods.rb +1 -0
- data/lib/inherited_resources/polymorphic_helpers.rb +3 -2
- data/lib/inherited_resources/url_helpers.rb +2 -2
- data/test/polymorphic_base_test.rb +1 -1
- metadata +1 -1
data/README
CHANGED
@@ -38,7 +38,7 @@ Basic Usage
|
|
38
38
|
|
39
39
|
To use Inherited Resources you just have to inherit (duh) it:
|
40
40
|
|
41
|
-
class ProjectsController <
|
41
|
+
class ProjectsController < InheritedResources::Base
|
42
42
|
end
|
43
43
|
|
44
44
|
And all actions are defined and working, check it! Your projects collection
|
@@ -78,6 +78,15 @@ Or:
|
|
78
78
|
actions :all, :except => [ :edit, :update, :destroy ]
|
79
79
|
end
|
80
80
|
|
81
|
+
In your views, you will get the following helpers:
|
82
|
+
|
83
|
+
resource #=> @project
|
84
|
+
collection #=> @projects
|
85
|
+
resource_class #=> Project
|
86
|
+
|
87
|
+
As you might expect, collection (@projects instance variable) is only available
|
88
|
+
on index actions.
|
89
|
+
|
81
90
|
Overwriting defaults
|
82
91
|
--------------------
|
83
92
|
|
@@ -179,7 +188,8 @@ but you don't want to create a before filter for it:
|
|
179
188
|
@project = Project.new(params[:project])
|
180
189
|
@project.something_special!
|
181
190
|
create!
|
182
|
-
end
|
191
|
+
end
|
192
|
+
end
|
183
193
|
|
184
194
|
Yeap, that simple! The nice part is since you already set the instance variable
|
185
195
|
@project, it will not do it again and overwrite your @project with something
|
@@ -298,7 +308,7 @@ When using polymorphic associations, you get some free helpers:
|
|
298
308
|
parent? #=> true
|
299
309
|
parent_type #=> :task
|
300
310
|
parent_class #=> Task
|
301
|
-
|
311
|
+
parent #=> @task
|
302
312
|
|
303
313
|
Polymorphic controller is another great idea by James Golick and he also uses
|
304
314
|
that on resource_controller.
|
@@ -167,7 +167,8 @@ module InheritedResources
|
|
167
167
|
extend InheritedResources::ClassMethods
|
168
168
|
|
169
169
|
helper_method :collection_url, :collection_path, :resource_url, :resource_path,
|
170
|
-
:new_resource_url, :new_resource_path, :edit_resource_url, :edit_resource_path
|
170
|
+
:new_resource_url, :new_resource_path, :edit_resource_url, :edit_resource_path,
|
171
|
+
:resource, :collection, :resource_class
|
171
172
|
|
172
173
|
def self.inherited(base)
|
173
174
|
base.class_eval do
|
@@ -123,7 +123,7 @@
|
|
123
123
|
# parent? #=> true
|
124
124
|
# parent_type #=> :task
|
125
125
|
# parent_class #=> Task
|
126
|
-
#
|
126
|
+
# parent #=> @task
|
127
127
|
#
|
128
128
|
# This polymorphic controllers thing is a great idea by James Golick and he
|
129
129
|
# built it in resource_controller. Here is just a re-implementation.
|
@@ -52,7 +52,7 @@ module InheritedResources #:nodoc:
|
|
52
52
|
base.parents_symbols.map do |symbol|
|
53
53
|
if symbol == :polymorphic
|
54
54
|
polymorphic = true
|
55
|
-
resource_ivars << :
|
55
|
+
resource_ivars << :parent
|
56
56
|
else
|
57
57
|
config = base.resources_configuration[symbol]
|
58
58
|
resource_segments << config[:route_name]
|
@@ -90,7 +90,7 @@ module InheritedResources #:nodoc:
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def self.generate_url_and_path_helpers(base, prefix, name, resource_segments, resource_ivars, polymorphic=false)
|
93
|
-
ivars = resource_ivars.map{|i| i == :
|
93
|
+
ivars = resource_ivars.map{|i| i == :parent ? :parent : "@#{i}" }
|
94
94
|
|
95
95
|
# If it's not a singleton, ivars are not empty, not a collection or
|
96
96
|
# not a new hew helper, we can add args to the method.
|
@@ -277,6 +277,6 @@ class PolymorphicHelpersTest < TEST_CLASS
|
|
277
277
|
assert_equal :factory, @controller.send(:parent_type)
|
278
278
|
assert_equal Factory, @controller.send(:parent_class)
|
279
279
|
assert_equal new_factory, assigns(:factory)
|
280
|
-
assert_equal new_factory, @controller.send(:
|
280
|
+
assert_equal new_factory, @controller.send(:parent)
|
281
281
|
end
|
282
282
|
end
|