super_resources 1.0.0.rc3 → 1.0.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -8
- data/lib/super_resources/controller.rb +4 -6
- data/lib/super_resources/url_helpers.rb +62 -19
- data/lib/super_resources/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -37,7 +37,7 @@ SuperResources provides helper methods that you can use directly in the controll
|
|
37
37
|
collection
|
38
38
|
|
39
39
|
For member actions, `resource` answers a single object that the RESTFUL is operating on.
|
40
|
-
For collection actions (i.e `
|
40
|
+
For collection actions (i.e `index`), `collection` answer a scoped collection of objects.
|
41
41
|
|
42
42
|
SuperResources does away with specifically named instance variables, such as those created by standard scaffolds. For example, in the example above, SuperResources does not give you `@organization_unit` or `@organization_units` for free. You won't need them in the common cases. Using `resource` and `collection` in every controller makes for easier coding and maintenance.
|
43
43
|
|
@@ -61,7 +61,7 @@ have a resource that is nested inside multiple other objects. SuperResources dyn
|
|
61
61
|
|
62
62
|
## Nested Resource
|
63
63
|
|
64
|
-
Let's face it:
|
64
|
+
Let's face it: dealing with nested resources has always been a pain. All that fiddling about, getting the path names and inputs right. Change the nesting and you have to go through and change all your path calls too. Then, if you have a resource that can be nested within multiple other resources, such as when you have an associative object and you want to navigate to it from any of it associations, the permutations become very complex.
|
65
65
|
|
66
66
|
It should be much easier, especially when you take routes into account. If I have an action that is matched to this route:
|
67
67
|
|
@@ -100,9 +100,9 @@ For example, given this route:
|
|
100
100
|
|
101
101
|
you can make these calls:
|
102
102
|
|
103
|
-
|
104
|
-
|
105
|
-
|
103
|
+
author #=> Author with an id of :author_id
|
104
|
+
note #=> Note with an id of :note_id
|
105
|
+
page #=> Page with an id of :id
|
106
106
|
|
107
107
|
An example of a place where you will want these, is in a layout template that presents information about the nesting objects. For example, you may want to show a page inside an author layout template. The author layout template will present information about the author. In this case, the layout will be used in the context of `PagesController`, so you can't refer to `resource` in the layout, since it will answer the page, not the author. You need some way to arbitrarily refer to the author. Being able to call `author` provides this.
|
108
108
|
|
@@ -140,7 +140,7 @@ If that looks strange, bear in mind it's been designed so that you don't need to
|
|
140
140
|
|
141
141
|
Before being able to use the result of `resource`, SuperResources may need to find it. The canonical way to do this is to do:
|
142
142
|
|
143
|
-
|
143
|
+
resource_class.find(:params[:id])
|
144
144
|
|
145
145
|
While SuperResource uses the `find` method as the default, you can choose another finder method by redefining `finder_method`. For example:
|
146
146
|
|
@@ -169,13 +169,13 @@ SuperResources extracts the construction of a new resource into the `build_resou
|
|
169
169
|
|
170
170
|
Yes, you can. All actions defined by SuperResources use responders and accept parameters to pass to `respond_with`, so customizing these parameters is a common adaptation.
|
171
171
|
|
172
|
-
For example, suppose that after creating a comment, you want to redirect to an index of comments that apply to the same parent. Adapt
|
172
|
+
For example, suppose that after creating a comment, you want to redirect to an index of comments that apply to the same parent. Adapt the action like this:
|
173
173
|
|
174
174
|
def create
|
175
175
|
super :location => polymorphic_url([ parent, :comments ])
|
176
176
|
end
|
177
177
|
|
178
|
-
Anything you can pass to `respond_with`, you can pass to
|
178
|
+
Anything you can pass to `respond_with`, you can pass to the super call, including a block.
|
179
179
|
|
180
180
|
You could, of course, completely redefine an action:
|
181
181
|
|
@@ -2,11 +2,9 @@ module SuperResources
|
|
2
2
|
module Controller
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
include URLHelpers
|
10
|
-
end
|
5
|
+
include Actions
|
6
|
+
include Nesting
|
7
|
+
include HasScope
|
8
|
+
include URLHelpers
|
11
9
|
end
|
12
10
|
end
|
@@ -4,23 +4,6 @@ module SuperResources
|
|
4
4
|
|
5
5
|
included do
|
6
6
|
helpers = %w(collection resource new_resource edit_resource)
|
7
|
-
|
8
|
-
helpers.each do |helper|
|
9
|
-
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
10
|
-
def #{helper}_path(*args)
|
11
|
-
url_for(hash_for_#{helper}_path(*args))
|
12
|
-
end
|
13
|
-
|
14
|
-
def #{helper}_url(*args)
|
15
|
-
url_for(hash_for_#{helper}_url(*args))
|
16
|
-
end
|
17
|
-
|
18
|
-
def hash_for_#{helper}_path(*args)
|
19
|
-
hash_for_#{helper}_url(*args).merge(:only_path => true)
|
20
|
-
end
|
21
|
-
RUBY_EVAL
|
22
|
-
end
|
23
|
-
|
24
7
|
helper_methods = helpers.map do |helper|
|
25
8
|
[ :"#{helper}_path", :"hash_for_#{helper}_path",
|
26
9
|
:"#{helper}_url", :"hash_for_#{helper}_url" ]
|
@@ -37,27 +20,87 @@ module SuperResources
|
|
37
20
|
path_parameters.except(:id, :action)
|
38
21
|
end
|
39
22
|
|
40
|
-
|
23
|
+
def super_path(chain, options={})
|
24
|
+
polymorphic_url(chain, options)
|
25
|
+
rescue NoMethodError => e
|
26
|
+
object = chain.pop
|
27
|
+
|
28
|
+
chain.empty? ?
|
29
|
+
raise(e) : super_path(chain.slice(0...-1) << object, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def super_url(chain, options={})
|
33
|
+
super_path(chain, options.merge(:routing_type => :url))
|
34
|
+
end
|
35
|
+
|
36
|
+
# collection route helpers .................................................
|
37
|
+
|
38
|
+
def collection_url
|
39
|
+
super_url(with_chain(resource_class))
|
40
|
+
end
|
41
|
+
|
42
|
+
def collection_path
|
43
|
+
super_path(with_chain(resource_class))
|
44
|
+
end
|
41
45
|
|
42
46
|
def hash_for_collection_url(options={})
|
43
47
|
route_hash.merge(options).merge(:action => 'index')
|
44
48
|
end
|
45
49
|
|
50
|
+
# resource route helpers ...................................................
|
51
|
+
|
52
|
+
def resource_path(*args)
|
53
|
+
options = args.extract_options!
|
54
|
+
super_path(with_chain(args.first || resource), options)
|
55
|
+
end
|
56
|
+
|
57
|
+
def resource_url(*args)
|
58
|
+
options = args.extract_options!
|
59
|
+
super_url(with_chain(args.first || resource), options)
|
60
|
+
end
|
61
|
+
|
46
62
|
def hash_for_resource_url(*args)
|
47
63
|
options = args.extract_options!
|
48
64
|
route_hash.merge(options)
|
49
65
|
.merge(:action => 'show', :id => args.first || resource)
|
50
66
|
end
|
51
67
|
|
68
|
+
# new resource route helpers ...............................................
|
69
|
+
|
70
|
+
def new_resource_path(options={})
|
71
|
+
options.merge! :action => :new
|
72
|
+
super_path(with_chain(resource_class), options)
|
73
|
+
end
|
74
|
+
|
75
|
+
def new_resource_url(options={})
|
76
|
+
options.merge! :action => :new
|
77
|
+
super_url(with_chain(resource_class), options)
|
78
|
+
end
|
79
|
+
|
52
80
|
def hash_for_new_resource_url(options={})
|
53
81
|
route_hash.merge(options).merge(:action => 'new')
|
54
82
|
end
|
55
83
|
|
84
|
+
# edit resource route helpers ..............................................
|
85
|
+
|
86
|
+
def edit_resource_path(*args)
|
87
|
+
options = args.extract_options!
|
88
|
+
options.merge! :action => :edit
|
89
|
+
|
90
|
+
super_path(with_chain(args.first || resource), options)
|
91
|
+
end
|
92
|
+
|
93
|
+
def edit_resource_url(*args)
|
94
|
+
options = args.extract_options!
|
95
|
+
options.merge! :action => :edit
|
96
|
+
|
97
|
+
super_url(with_chain(args.first || resource), options)
|
98
|
+
end
|
99
|
+
|
56
100
|
def hash_for_edit_resource_url(*args)
|
57
101
|
options = args.extract_options!
|
58
102
|
route_hash.merge(options)
|
59
103
|
.merge(:action => 'edit', :id => args.first || resource)
|
60
104
|
end
|
61
105
|
end
|
62
|
-
|
63
106
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: super_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc4
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-04-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|