resource_awareness
resource_awareness copied to clipboard
Makes information about a Rails application's resources (as defined via the 'resource(s)' routing DSL methods) available via Rails.application.resources
h1. resource_awareness
resource_awareness makes information about a Rails application's resources (as defined via the 'resource/s' routing DSL methods) available via @Rails.application.resource_definitions@. Each resource object encapsulates information such as the resource's name, home route, path and the controller that is handling it:
|. attribute |. example | | id | post_comments | | name | comments | | singular_name | comment | | home_route | [the 'post_comments' named route] | | path_prefix | /posts/:post_id | | path | /posts/:post_id/comments | | prefix_parameters | [:post_id] | | controller_path | comments | | controller | CommentsController |
p. Resources can be retrieved by id using bracket syntax:
bc(ruby). Rails.application.resource_definitions['post_comments']
p. Controllers get a @resource_definition@ instance method to controllers returning the resource they are handling, if any.
p. Resources have a to_xml method that makes it trivial to expose an application's resource as a resource in turn:
bc(ruby).. # config/routes.rb resources :resources
app/controllers/resources_controller.rb
respond_to :xml
def index respond_with Rails.application.resource_definitions end
At this point, resource information is available at /resources.xml
p. Lastly, resource_awareness adds a @rake resources@ tasks that lists an application's resources, just like @rake routes@ does for routes
p. Please note that resource_awareness is experimental and interfaces are likely to change. I very much welcome suggestions or comments!
h3. Installation
gem install resource_awareness
Copyright (c) 2010 Ingo Weiss, released under the MIT license