Skip to content
Newer
Older
100644 512 lines (370 sloc) 16.9 KB
f0b92a4 @josevalim First commit! :)
authored
1 Inherited Resources
2 License: MIT
c25da84 @josevalim Bump tiny.
authored
3 Version: 0.9.1
f0b92a4 @josevalim First commit! :)
authored
4
5bba068 @josevalim Wrap everything before new release.
authored
5 == Description
f0b92a4 @josevalim First commit! :)
authored
6
7 Inherited Resources speeds up development by making your controllers inherit
8 all restful actions so you just have to focus on what is important. It makes
9 your controllers more powerful and cleaner at the same time.
10
9263dee @josevalim Added more information on README.
authored
11 Plus, making your controllers follow a pattern, it helps you to write better
12 code by following fat models and skinny controllers convention.
13
5e2b8d4 @josevalim Adding to README that it is tested and compatible with Rails 2.2.x an…
authored
14 Inherited Resources is tested and compatible with Rails 2.2.x and Rails 2.3.x.
0e34348 @josevalim Changing version for 0.1.1, which is compatible with Rails 2.3.0.
authored
15
2aa1762 @josevalim Updated README
authored
16 keywords: resources, controller, singleton, belongs_to, polymorphic, named_scope and I18n
f0b92a4 @josevalim First commit! :)
authored
17
5bba068 @josevalim Wrap everything before new release.
authored
18 == Installation
f0b92a4 @josevalim First commit! :)
authored
19
20 Install Inherited Resources is very easy. It is stored in GitHub, so just run
21 the following:
22
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
23 gem sources -a http://gems.github.com
24 sudo gem install josevalim-inherited_resources
f0b92a4 @josevalim First commit! :)
authored
25
26 If you want it as plugin, just do:
27
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
28 script/plugin install git://github.com/josevalim/inherited_resources.git
f0b92a4 @josevalim First commit! :)
authored
29
5bba068 @josevalim Wrap everything before new release.
authored
30 == Basic Usage
f0b92a4 @josevalim First commit! :)
authored
31
32 To use Inherited Resources you just have to inherit (duh) it:
33
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
34 class ProjectsController < InheritedResources::Base
35 end
f0b92a4 @josevalim First commit! :)
authored
36
37 And all actions are defined and working, check it! Your projects collection
38 (in the index action) is still available in the instance variable @projects
39 and your project resource (all other actions) is available as @ project.
40
41 The next step is to define which mime types this controller provides:
42
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
43 class ProjectsController < InheritedResources::Base
44 respond_to :html, :xml, :json
45 end
f0b92a4 @josevalim First commit! :)
authored
46
47 You can also specify them based per action:
48
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
49 class ProjectsController < InheritedResources::Base
50 respond_to :html, :xml, :json
51 respond_to :js, :only => :create
52 respond_to :iphone, :except => [ :edit, :update ]
53 end
f0b92a4 @josevalim First commit! :)
authored
54
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
55 For each request, it first checkes if the "controller/action.format" file is
56 available (for example "projects/create.xml") and if it's not, it checks if
57 the resource respond to :to_format (in this case, :to_xml). Otherwise returns 404.
f0b92a4 @josevalim First commit! :)
authored
58
59 Another option is to specify which actions the controller will inherit from
60 the InheritedResources::Base:
61
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
62 class ProjectsController < InheritedResources::Base
63 actions :index, :show, :new, :create
64 end
f0b92a4 @josevalim First commit! :)
authored
65
66 Or:
67
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
68 class ProjectsController < InheritedResources::Base
69 actions :all, :except => [ :edit, :update, :destroy ]
70 end
f0b92a4 @josevalim First commit! :)
authored
71
960671b @josevalim Added parents helpers when using polymorphic controllers.
authored
72 In your views, you will get the following helpers:
73
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
74 resource #=> @project
75 collection #=> @projects
76 resource_class #=> Project
960671b @josevalim Added parents helpers when using polymorphic controllers.
authored
77
78 As you might expect, collection (@projects instance variable) is only available
79 on index actions.
80
e9df22c @josevalim Allow inherited resources to work just calling inherit_resources insi…
authored
81 If for some reason you cannot inherit from InheritedResources::Base, you can
cf4b234 @josevalim Complete refactoring of respond_to.
authored
82 call inherit_resources in your controller class scope:
e9df22c @josevalim Allow inherited resources to work just calling inherit_resources insi…
authored
83
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
84 class AccountsController < ApplicationController
85 inherit_resources
86 end
e9df22c @josevalim Allow inherited resources to work just calling inherit_resources insi…
authored
87
5bba068 @josevalim Wrap everything before new release.
authored
88 == Overwriting defaults
f0b92a4 @josevalim First commit! :)
authored
89
d201f1f @josevalim More README cleanup.
authored
90 Whenever you inherit from InheritedResources, several defaults are assumed.
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
91 For example you can have an AccountsController to account management while the
92 resource is an User:
f0b92a4 @josevalim First commit! :)
authored
93
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
94 class AccountsController < InheritedResources::Base
95 defaults :resource_class => User, :collection_name => 'users', :instance_name => 'user'
96 end
f0b92a4 @josevalim First commit! :)
authored
97
a3a735a @josevalim Decoupled routes names from collection name and instance name to allo…
authored
98 In the case above, in your views you will have @users and @user variables, but
99 the routes used will still be accounts_url and account_url. If you plan also to
100 change the routes, you can use :route_collection_name and :route_instance_name.
101
7def86b @josevalim Namespaced controllers now work out of the box (no configuration need…
authored
102 Namespaced controllers work out of the box, but if you need to specify a
103 different route prefix, you can do the following:
b18bcd9 @josevalim Added support to namespaced routes with defaults :route_prefix.
authored
104
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
105 class Administrators::PeopleController < InheritedResources::Base
106 defaults :route_prefix => 'admin'
107 end
b18bcd9 @josevalim Added support to namespaced routes with defaults :route_prefix.
authored
108
d201f1f @josevalim More README cleanup.
authored
109 Then your named routes will be: 'admin_people_url', 'admin_person_url' instead
110 of 'administrators_people_url' and 'administrators_person_url'.
b18bcd9 @josevalim Added support to namespaced routes with defaults :route_prefix.
authored
111
d201f1f @josevalim More README cleanup.
authored
112 If you want to customize how resources are retrieved you can overwrite
113 collection and resource methods. The first is called on index action and the
114 second on all other actions. Let's suppose you want to add pagination to your
115 projects collection:
f0b92a4 @josevalim First commit! :)
authored
116
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
117 class ProjectsController < InheritedResources::Base
118 protected
119 def collection
120 @projects ||= end_of_association_chain.paginate(params[:page]).all
121 end
122 end
f0b92a4 @josevalim First commit! :)
authored
123
d201f1f @josevalim More README cleanup.
authored
124 The end_of_association_chain returns your resource after nesting all associations
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
125 and scopes (more about this below).
f0b92a4 @josevalim First commit! :)
authored
126
d201f1f @josevalim More README cleanup.
authored
127 InheritedResources also introduces another method called begin_of_association_chain.
128 It's mostly used when you want to create resources based on the @current_user and
0f7bfd5 @josevalim Allow interpolation options to be set in the application controller.
authored
129 you have urls like "account/projects". In such cases, you have to do
249d2ce @josevalim Added support for "success/failure blocks" (thanks grosser for the id…
authored
130 @current_user.projects.find or @current_user.projects.build in your actions.
d201f1f @josevalim More README cleanup.
authored
131
249d2ce @josevalim Added support for "success/failure blocks" (thanks grosser for the id…
authored
132 You can deal with it just doing:
f0b92a4 @josevalim First commit! :)
authored
133
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
134 class ProjectsController < InheritedResources::Base
135 protected
136 def begin_of_association_chain
137 @current_user
138 end
139 end
f0b92a4 @josevalim First commit! :)
authored
140
5bba068 @josevalim Wrap everything before new release.
authored
141 == Overwriting actions
f0b92a4 @josevalim First commit! :)
authored
142
143 Let's suppose that after destroying a project you want to redirect to your
144 root url instead of redirecting to projects url. You just have to do:
145
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
146 class ProjectsController < InheritedResources::Base
147 def destroy
148 super do |format|
149 format.html { redirect_to root_url }
150 end
151 end
152 end
153
154 You are opening your action and giving the parent action a new behavior. On
155 the other hand, I have to agree that calling super is not very readable. That's
156 why all methods have aliases. So this is equivalent:
157
158 class ProjectsController < InheritedResources::Base
159 def destroy
160 destroy! do |format|
161 format.html { redirect_to root_url }
162 end
163 end
164 end
f0b92a4 @josevalim First commit! :)
authored
165
3b41204 @josevalim Allow blocks with arity equal zero to be given as redirect_url
authored
166 Even more, since most of the times when you change a create, update or destroy
a99ce3b @josevalim More documentation.
authored
167 action is because you want to to change to where it redirects, a shortcut is
168 provided. So you can do:
169
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
170 class ProjectsController < InheritedResources::Base
171 def destroy
172 destroy!{ root_url }
173 end
174 end
a99ce3b @josevalim More documentation.
authored
175
f0b92a4 @josevalim First commit! :)
authored
176 Now let's suppose that before create a project you have to do something special
177 but you don't want to create a before filter for it:
178
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
179 class ProjectsController < InheritedResources::Base
180 def create
181 @project = Project.new(params[:project])
182 @project.something_special!
183 create!
184 end
185 end
f0b92a4 @josevalim First commit! :)
authored
186
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
187 Yes, that simple! The nice part is since you already set the instance variable
188 @project, it will not build a project again.
f0b92a4 @josevalim First commit! :)
authored
189
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
190 Before we finish this topic, we should talk about one more thing: "success/failure
191 blocks". Let's suppose that when we update our project, in case of failure, we
192 want to redirect to the project url instead of re-rendering the edit template.
193
194 Our first attempt to do this would be:
195
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
196 class ProjectsController < InheritedResources::Base
197 def update
198 update! do |format|
199 unless @project.errors.empty? # failure
200 format.html { redirect_to project_url(@project) }
201 end
202 end
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
203 end
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
204 end
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
205
d201f1f @josevalim More README cleanup.
authored
206 Looks to verbose, right? We can actually do:
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
207
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
208 class ProjectsController < InheritedResources::Base
209 def update
210 update! do |success, failure|
211 failure.html { redirect_to project_url(@project) }
212 end
213 end
214 end
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
215
2aa1762 @josevalim Updated README
authored
216 Much better! So explaining everything: when you give a block which expects one
579fc66 @josevalim New gem version 0.2.0 and pushed documentation about success/failure …
authored
217 argument it will be executed in both scenarios: success and failure. But If you
218 give a block that expects two arguments, the first will be executed only in
219 success scenarios and the second in failure scenarios. You keep everything
220 clean and organized inside the same action.
221
5bba068 @josevalim Wrap everything before new release.
authored
222 == Some DSL
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
223
224 For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
225 your success/failure blocks straight from your class binding. For it, you just
226 need to add a DSL block to your application controller:
227
228 class ApplicationController < ActionController::Base
229 include InheritedResources::DSL
230 end
231
232 And then you can rewrite the last example as:
233
234 class ProjectsController < InheritedResources::Base
235 update! do |success, failure|
236 failure.html { redirect_to project_url(@project) }
237 end
238 end
239
5bba068 @josevalim Wrap everything before new release.
authored
240 == Flash messages and I18n
f0b92a4 @josevalim First commit! :)
authored
241
242 Flash messages are powered by I18n api. It checks for messages in the following
243 order:
244
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
245 flash.controller_name.action_name.status
246 flash.actions.action_name.status
f0b92a4 @josevalim First commit! :)
authored
247
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
248 If none is available, a default message in english set. In a create action
249 on projects controller, it will search for:
f0b92a4 @josevalim First commit! :)
authored
250
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
251 flash.projects.create.status
252 flash.actions.create.status
f0b92a4 @josevalim First commit! :)
authored
253
254 The status can be :notice (when the object can be created, updated
255 or destroyed with success) or :error (when the objecy cannot be created
256 or updated).
257
258 Those messages are interpolated by using the resource class human name, which
259 is also localized and it means you can set:
260
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
261 flash:
262 actions:
263 create:
264 notice: "Hooray! {{resource_name}} was successfully created!"
f0b92a4 @josevalim First commit! :)
authored
265
89fd71b @josevalim Deprecating {{resource}}. Use {{resource_name}} instead.
authored
266 It will replace {{resource_name}} by the human name of the resource class,
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
267 which is "Project" in this case.
f0b92a4 @josevalim First commit! :)
authored
268
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
269 But sometimes, flash messages are not that simple. Sometimes you want to say
270 the title of the project while updating a project. Well, that's easy also:
f0b92a4 @josevalim First commit! :)
authored
271
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
272 flash:
273 projects:
274 update:
275 notice: "Hooray! The project "{{project_title}}" was updated!"
f0b92a4 @josevalim First commit! :)
authored
276
f6fb96a @josevalim Updated README and moving to 0.7.0
authored
277 Since :project_title is not available for interpolation by default, you have
f0b92a4 @josevalim First commit! :)
authored
278 to overwrite interpolation_options.
279
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
280 def interpolation_options
281 { :project_title => @project.title }
282 end
f0b92a4 @josevalim First commit! :)
authored
283
284 Then you will finally have:
285
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
286 "Hooray! The project "Plataforma" was updated!"
f0b92a4 @josevalim First commit! :)
authored
287
0f7bfd5 @josevalim Allow interpolation options to be set in the application controller.
authored
288 By default, resource name is capitalized. If you want to make it lower case, you
289 can add to your application controller:
290
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
291 def interpolation_options
292 { :resource_name => resource_class.human_name.downcase }
293 end
0f7bfd5 @josevalim Allow interpolation options to be set in the application controller.
authored
294
295 Finally, if your controller is namespaced, for example Admin::ProjectsController,
296 the messages will be checked in the following order:
c11f334 @josevalim Added support to namespaced flash messages.
authored
297
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
298 flash.admin.projects.create.notice
299 flash.admin.actions.create.notice
300 flash.projects.create.notice
301 flash.actions.create.notice
c11f334 @josevalim Added support to namespaced flash messages.
authored
302
5bba068 @josevalim Wrap everything before new release.
authored
303 == Has Scope
dd394d7 @josevalim Remove useless features and bump tiny.
authored
304
305 InheritedResources tries to integrate nicely with your model. In order to do so,
306 it also is named_scope fluent. Let's suppose our Project model with the scopes:
307
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
308 class Project < ActiveRecord::Base
309 named_scope :featured, :conditions => { :featured => true }
310 named_scope :by_methodology, proc {|methodology| { :conditions => { :methodology => methodology } } }
311 named_scope :limit, proc{|limit| :limit => limit.to_i }
312 end
dd394d7 @josevalim Remove useless features and bump tiny.
authored
313
314 Your controller:
315
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
316 class ProjectsController < InheritedResources::Base
317 has_scope :featured, :boolean => true, :only => :index
318 has_scope :by_methodology
319 has_scope :limit, :default => 10
320 end
dd394d7 @josevalim Remove useless features and bump tiny.
authored
321
322 Then for each request:
323
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
324 /projects
325 #=> acts like a normal request, but returning 10 projects
dd394d7 @josevalim Remove useless features and bump tiny.
authored
326
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
327 /projects?featured=true
328 #=> calls the featured named scope and bring 10 featured projects
dd394d7 @josevalim Remove useless features and bump tiny.
authored
329
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
330 /projects?featured=true&by_methodology=agile&limit=20
331 #=> brings 20 featured projects with methodology agile
dd394d7 @josevalim Remove useless features and bump tiny.
authored
332
333 You can retrieve the current scopes in use with :current_scopes method.
334 In the last case, it would return:
335
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
336 { :featured => "true", :by_methodology => "agile", :limit => "20" }
dd394d7 @josevalim Remove useless features and bump tiny.
authored
337
e094deb @josevalim Allow procs to be given as default values to has scope.
authored
338 Finally, let's suppose you store on the session how many projects the user sees
339 per page. In such cases, you can give a proc as default value:
340
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
341 has_scope :limit, :default => proc{|c| c.session[:limit] || 10 }
e094deb @josevalim Allow procs to be given as default values to has scope.
authored
342
5bba068 @josevalim Wrap everything before new release.
authored
343 == Belongs to
f0b92a4 @josevalim First commit! :)
authored
344
345 Finally, our Projects are going to get some Tasks. Then you create a
346 TasksController and do:
347
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
348 class TasksController < InheritedResources::Base
349 belongs_to :project
350 end
f0b92a4 @josevalim First commit! :)
authored
351
352 belongs_to accepts several options to be able to configure the association.
c1eb780 @josevalim First scratch at scopes implementation.
authored
353 For example, if you want urls like /projects/:project_title/tasks, you can
354 customize how InheritedResources find your projects:
f0b92a4 @josevalim First commit! :)
authored
355
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
356 class TasksController < InheritedResources::Base
357 belongs_to :project, :finder => :find_by_title!, :param => :project_title
358 end
f0b92a4 @josevalim First commit! :)
authored
359
49ff641 @josevalim Telling more about belongs_to options in the README
authored
360 It also accepts :route_name, :parent_class and :instance_name as options.
c1eb780 @josevalim First scratch at scopes implementation.
authored
361 Check the lib/inherited_resources/class_methods.rb for more.
f0b92a4 @josevalim First commit! :)
authored
362
5bba068 @josevalim Wrap everything before new release.
authored
363 == Nested belongs to
f0b92a4 @josevalim First commit! :)
authored
364
365 Now, our Tasks get some Comments and you need to nest even deeper. Good
366 practices says that you should never nest more than two resources, but sometimes
367 you have to for security reasons. So this is an example of how you can do it:
368
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
369 class CommentsController < InheritedResources::Base
370 nested_belongs_to :project, :task
371 end
f0b92a4 @josevalim First commit! :)
authored
372
872ffaf @josevalim More typos fixed in the README (Thanks to Alexei again!)
authored
373 If you need to configure any of these belongs to, you can nest them using blocks:
f0b92a4 @josevalim First commit! :)
authored
374
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
375 class CommentsController < InheritedResources::Base
376 belongs_to :project, :finder => :find_by_title!, :param => :project_title do
377 belongs_to :task
378 end
379 end
f0b92a4 @josevalim First commit! :)
authored
380
d201f1f @josevalim More README cleanup.
authored
381 Warning: calling several belongs_to is the same as nesting them:
382
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
383 class CommentsConroller < InheritedResources::Base
384 belongs_to :project
385 belongs_to :task
386 end
d201f1f @josevalim More README cleanup.
authored
387
388 In other words, the code above is the same as calling nested_belongs_to.
389
5bba068 @josevalim Wrap everything before new release.
authored
390 == Polymorphic belongs to
f0b92a4 @josevalim First commit! :)
authored
391
dd394d7 @josevalim Remove useless features and bump tiny.
authored
392 We can go even further. Let's suppose our Projects can now have Files, Messages
393 and Tasks, and they are all commentable. In this case, the best solution is to
394 use polymorphism:
f0b92a4 @josevalim First commit! :)
authored
395
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
396 class CommentsController < InheritedResources::Base
397 belongs_to :task, :file, :message, :polymorphic => true
398 # polymorphic_belongs_to :task, :file, :message
399 end
f0b92a4 @josevalim First commit! :)
authored
400
401 You can even use it with nested resources:
402
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
403 class CommentsController < InheritedResources::Base
404 belongs_to :project do
405 belongs_to :task, :file, :message, :polymorphic => true
406 end
407 end
f0b92a4 @josevalim First commit! :)
authored
408
dd394d7 @josevalim Remove useless features and bump tiny.
authored
409 The url in such cases can be:
410
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
411 /project/1/task/13/comments
412 /project/1/file/11/comments
413 /project/1/message/9/comments
dd394d7 @josevalim Remove useless features and bump tiny.
authored
414
f0b92a4 @josevalim First commit! :)
authored
415 When using polymorphic associations, you get some free helpers:
416
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
417 parent? #=> true
418 parent_type #=> :task
419 parent_class #=> Task
420 parent #=> @task
f0b92a4 @josevalim First commit! :)
authored
421
5bba068 @josevalim Wrap everything before new release.
authored
422 == Optional belongs to
8b5b2c5 @josevalim Added :optional information on README
authored
423
dd394d7 @josevalim Remove useless features and bump tiny.
authored
424 Later you decide to create a view to show all comments, independent if they belong
425 to a task, file or message. You can reuse your polymorphic controller just doing:
8b5b2c5 @josevalim Added :optional information on README
authored
426
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
427 class ProjectsController < InheritedResources::Base
428 belongs_to :task, :file, :message, :optional => true
429 # optional_belongs_to :task, :file, :message
430 end
8b5b2c5 @josevalim Added :optional information on README
authored
431
432 This will handle all those urls properly:
433
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
434 /comment/1
435 /tasks/2/comment/5
436 /files/10/comment/3
437 /messages/13/comment/11
8b5b2c5 @josevalim Added :optional information on README
authored
438
439 This is treated as a special type of polymorphic associations, thus all helpers
440 are available. As you expect, when no parent is found, the helpers return:
441
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
442 parent? #=> false
443 parent_type #=> nil
444 parent_class #=> nil
445 parent #=> nil
8b5b2c5 @josevalim Added :optional information on README
authored
446
5bba068 @josevalim Wrap everything before new release.
authored
447 == Singletons
f0b92a4 @josevalim First commit! :)
authored
448
449 Now we are going to add manager to projects. We say that Manager is a singleton
450 resource because a Project has just one manager. You should declare it as
451 has_one (or resource) in your routes.
452
453 To declare an association as singleton, you just have to give the :singleton
454 option.
455
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
456 class ManagersController < InheritedResources::Base
457 belongs_to :project, :singleton => true
458 # singleton_belongs_to :project
459 end
f0b92a4 @josevalim First commit! :)
authored
460
461 It will deal with everything again and hide the action :index from you.
462
5bba068 @josevalim Wrap everything before new release.
authored
463 == URL Helpers
f0b92a4 @josevalim First commit! :)
authored
464
5e9177d @josevalim Going to 0.4.1!
authored
465 When you use InheritedResources it creates some URL helpers.
466 And they handle everything for you. :)
f0b92a4 @josevalim First commit! :)
authored
467
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
468 # /posts/1/comments
469 resource_url # => /posts/1/comments/#{@comment.to_param}
470 resource_url(comment) # => /posts/1/comments/#{comment.to_param}
471 new_resource_url # => /posts/1/comments/new
472 edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
473 edit_resource_url(comment) #=> /posts/1/comments/#{comment.to_param}/edit
474 collection_url # => /posts/1/comments
5bba068 @josevalim Wrap everything before new release.
authored
475 parent_url # => /posts/1
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
476
477 # /projects/1/tasks
478 resource_url # => /projects/1/tasks/#{@task.to_param}
479 resource_url(task) # => /projects/1/tasks/#{task.to_param}
480 new_resource_url # => /projects/1/tasks/new
481 edit_resource_url # => /projects/1/tasks/#{@task.to_param}/edit
482 edit_resource_url(task) # => /projects/1/tasks/#{task.to_param}/edit
483 collection_url # => /projects/1/tasks
5bba068 @josevalim Wrap everything before new release.
authored
484 parent_url # => /projects/1
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
485
486 # /users
487 resource_url # => /users/#{@user.to_param}
488 resource_url(user) # => /users/#{user.to_param}
489 new_resource_url # => /users/new
490 edit_resource_url # => /users/#{@user.to_param}/edit
491 edit_resource_url(user) # => /users/#{user.to_param}/edit
492 collection_url # => /users
5bba068 @josevalim Wrap everything before new release.
authored
493 parent_url # => /
5e9177d @josevalim Going to 0.4.1!
authored
494
495 Those urls helpers also accepts a hash as options, just as in named routes.
496
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
497 # /projects/1/tasks
498 collection_url(:page => 1, :limit => 10) #=> /projects/1/tasks?page=1&limit=10
f0b92a4 @josevalim First commit! :)
authored
499
cf4b234 @josevalim Complete refactoring of respond_to.
authored
500 In polymorphic cases, you can also give the parent as parameter to collection_url.
501
5e9177d @josevalim Going to 0.4.1!
authored
502 Another nice thing is that those urls are not guessed during runtime. They are
f0b92a4 @josevalim First commit! :)
authored
503 all created when your application is loaded (except for polymorphic
504 associations, that relies on Rails polymorphic_url).
505
c25da84 @josevalim Bump tiny.
authored
506 == Bugs and Feedback
f0b92a4 @josevalim First commit! :)
authored
507
4776927 @josevalim Added class method DSL, fixed polymorphic helpers message and updated…
authored
508 If you discover any bugs, please send an e-mail to jose.valim@plataformatec.com.br
f0b92a4 @josevalim First commit! :)
authored
509 If you just want to give some positive feedback or drop a line, that's fine too!
510
35467fa @josevalim Updated README
authored
511 Copyright (c) 2009 José Valim http://blog.plataformatec.com.br
Something went wrong with that request. Please try again.