Rails RESTful 控制器抽象插件。
= Resource Controller
resource_controller makes RESTful controllers easier, more maintainable, and super readable. With the RESTful controller pattern hidden away, you can focus on what makes your controller special.
== Get It
Install it as a plugin:
script/plugin install git://github.com/giraffesoft/resource_controller.git
Or grab the source
git clone git://github.com/giraffesoft/resource_controller.git
= Usage
Creating a basic RESTful controller is as easy as...
class PostsController "show.rjs" } end
Or you can create a whole new block. This syntax destroys everything that's there, and starts again...
class ProjectsController "show.rjs" } end end
If you have a nested resource and want to redirect to the parent after create/update and destroy you can do this in the object controller
class CommentsController "show.rjs" }
failure.wants.js { render :template => "display_errors.rjs" }
end
destroy do
flash "You destroyed your project. Good work."
failure do
flash "You cannot destroy that project. Stop trying!"
wants.js { render :template => "display_errors.rjs" }
end
endend
== Singleton Resource
If you want to create a singleton RESTful controller inherit from ResourceController::Singleton.
class AccountsController :options # /account/options, account is a singleton parent map.resources :users, :has_one => :image # /users/1/image, image is a singleton child
If you have the :has_many association with a singleton parent remember to override parent_object for your :has_many controller as it returns nil by default in this case.
class OptionsController {:size => 10, :current => params[:page]}) end end
Or maybe you used a permalink...
class PostsController true end
class CommentsController :comments map.resources :products, :has_many => :comments map.resources :users, :has_many => :comments
All you have to do is that, and r_c will infer whichever relationship is present, and perform all the actions at the scope of the parent object.
=== Parent Helpers
You also get some helpers for reflecting on your parent.
parent? # => true/false is there a parent present? parent_type # => :post parent_model # => Post parent_object # => @post
=== Non-standard resource names
resource_controller supports overrides for every non-standard configuration of resources.
The most common example is where the resource has a different name than the associated model. Simply overriding the model_name helper will get resource_controller working with your model.
map.resources :tags ... class PhotoTag "somethings" ... class Tag "taggings" ... class Taggings /posts/1/comments/#{@comment.to_param} object_url(comment) # => /posts/1/comments/#{comment.to_param} edit_object_url # => /posts/1/comments/#{@comment.to_param}/edit collection_url # => /posts/1/comments
object_url # => /products/1/comments/#{@comment.to_param} object_url(comment) # => /products/1/comments/#{comment.to_param} edit_object_url # => /products/1/comments/#{@comment.to_param}/edit collection_url # => /products/1/comments
object_url # => /comments/#{@comment.to_param} object_url(comment) # => /comments/#{comment.to_param} edit_object_url # => /comments/#{@comment.to_param}/edit collection_url # => /comments
Or with namespaced, nested controllers...
object_url # => /admin/products/1/options/#{@option.to_param} object_url(option) # => /admin/products/1/options/#{option.to_param} edit_object_url # => /admin/products/1/options/#{@option.to_param}/edit collection_url # => /admin/products/1/options
You get the idea. Everything is automagical! All parameters are inferred.
== Credits
resource_controller was created, and is maintained by {James Golick}[http://jamesgolick.com].
== License
resource_controller is available under the {MIT License}[http://en.wikipedia.org/wiki/MIT_License]
暂无开放 Issues,或尚未同步最近议题。