= ResourcefulViews 0.1
ResourcefulViews aims to take RESTful conventions beyond controllers and into views by extending the 'map.resources' method to install a comprehensive vocabulary of resource-oriented view helpers
== Why?
Rails, being it's opinionated self, establishes strong RESTful conventions for routes and controllers. In comparison, Rails appears to have few opinions about how views should be written, let alone CSS sheets or JavaScripts. I think this is some wasted RESTful momentum worth recouping, and ResourcefulViews is here to recoup it.
The vision is that once an application's resources are defined, developers and designers will have a rich, shared vocabulary of view helpers and CSS classes readily available, greatly reducing the need to come up with their own, potentially conflicting vocabularies, and freeing them to focus on higher level problems instead.
== How?
For every resource defined in routes.rb via 'resources' or 'resource', ResourcefulViews defines seven helper methods (plus some extra ones). For each of the seven 'CRUD' actions one helper is installed which:
* is named '[CRUD action]_[route name prefix][resource name]': 'create_table', 'destroy_table_leg', 'new_table_top', etc.
* renders either a link (index new show edit) or a form (create update delete), that, when clicked/submitted, will trigger an HTTP request which gets routed to the helper's CRUD action on the resource's controller
* equips the link/form with a set of standard class attributes composed of the resource name, the CRUD action name, plus the two joined with '_' to help IE ('create table create_table')
== Basic example:
Say you have an app for furnishing your dream aparmtment, with a 'tables' resource. With ResourcefulViews installed, map.resources :tables will not only install the standard named route helpers, but also the following ones:
index_tables, new_table, create_table, show_table, edit_table, update_table, destroy_table
Now let's look at a vertical slice through an application, with the deletion of a 'table' resource as an example:
==== Routes
map.resources :tables
==== Controller
class TablesController < ActiveRecord::Base
def destroy
@table = Table.find(params[:table])
@table.destroy
redirect_to :action => 'index'
end
end
==== View
So far so familar. However, here is where Rails drops the RESTful ball. With ResourcefulViews, however, you can write in your views:
<% destroy_table(@table) %>
# this will render:
rake doc:plugins PLUGIN=resourceful_views)
== Common options
* :label specifies the content of the link tag in link helpers, and of the button tag in form helpers. If :label is omitted, the helpers will use a default label
* :parameters allows for specifying additional parameters to send with the request, via url query parameters in link helpers, and via hidden fields in form helpers
* For block-less create_resource and update_resource helpers, the :attributes option is used to specify resource attributes to be sent with the request
* All other options(such as :id and :title) are applied to the form in form helpers and to the link in link helpers
* To apply options to the button tag instead of the form tag in form helpers, make the options hash the value of the :button option (:button => {:id => 'logout_button'})
* both the create_resource and the update_resource helpers support passing in a 'template' model as the last argument that is passed on to the 'fields_for' helper
== How about nested resources?
Not a problem. In fact, here is where it gets interesting:
<% edit_table_top(@table) %>
# will render:
Edit
== Helper name suffixes
If the helpers are crowding your namespace, or if you just don't like the short helper names, you can tell ResourcefulViews to use suffixes for naming the helpers. Just put this in an initializer:
# This will append '_link' to all link helpers, like 'edit_table_top_link'
ResourcefulViews.link_helpers_suffix = '_link'
# This will append '_form' to all link helpers, like 'search_tables_form'
ResourcefulViews.form_helpers_suffix = '_form'
== Tests
ResourcefulViews comes with en extensive rspec test suite: rake spec:plugins PLUGIN=resourceful_views
== Plans
ResourcefulViews 0.1 is an experimental release. If you decide to give it a spin, please be aware that future versions might introduce incompatible syntax changes.
The plugin is all about the power of conventions, and about maximizing their benefits. Consequently, I have no plans to give it the flexibiliy to handle every non-standard situation out there. There are always the standard Rails form/link helpers to fall back to if more flexibility is needed. However, here are a couple of features that I could see being useful:
* There are situations, especially in lists, where the auto-generated classnames seem very verbose, so it will likely be possible to turn them on/off for individual helpers, or in lists only.
* It will likely be possible to configure defaults for options like :label or :title
== Known problems
* ActiveMerchant seems to run 'routes.rb' before ResourcefulViews loads, resulting in ResourcefulViews helpers not being defined. To fix this, make sure ResourcefulViews loads before active_merchant by putting this in your environment file: config.plugins = [ :resourceful_views, :all ]
== License
Copyright (c) 2008 Ingo Weiss, released under the MIT license