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 |
Resources can be retrieved by id using bracket syntax:
Rails.application.resource_definitions['post_comments']
Controllers get a resource_definition
instance method to controllers returning the resource they are handling, if any.
Resources have a to_xml method that makes it trivial to expose an application’s resource as a resource in turn:
# 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
Lastly, resource_awareness adds a rake resources
tasks that lists an application’s resources, just like rake routes
does for routes
Please note that resource_awareness is experimental and interfaces are likely to change. I very much welcome suggestions or comments!
Installation
gem install resource_awareness
Copyright © 2010 Ingo Weiss, released under the MIT license