# Introducing ResourcefulViews 0.1

**URL:** <https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861>\
**Category:** Rails\
**Created:** [May 6, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861 "2008-05-06T22:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 6, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861/1 "2008-05-06T22:31:09Z")

</div>

Hi all,

today I am releasing ResourcefulViews 0.1, a Rails plugin that I have  
been working on lately. ResoursefulViews started as a simple naming  
convention for CSS classes that we began using at [twinity.com](http://twinity.com) a while  
ago, and that was aimed at taking RESTful conventions far beyond  
controllers. It eventually became a plugin.

Please check it out, and please let me know what you think!

[http://svn.ingoweiss.com/plugins/resourceful\_views/tags/REL\_0\_1](http://svn.ingoweiss.com/plugins/resourceful_views/tags/REL_0_1)

Ingo W.

From the README:

= 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:
 Delete 

Note the auto-generated class names, allowing you to use ‘resourceful’  
naming conventions beyond the view, for example in CSS sheets and  
JavaScript scripts:

==== CSS

form.destroy\_table button { background-image: url(/images/buttons/  
remove.png) }

==== JavaScript

# LowPro behavior:

Event.addBehavior({  
‘form.destroy’: ConfirmDestroyResource()  
});

==== Tests

# rspec story step

Then(/the page should have a(n?) ([a-z\_]+) ([a-z\_]+) (form|link)/)  
do |_, crud\_action, resource\_name, tag|  
response.should have\_tag("#{tag}.#{crud\_action}_#{resource\_name}")  
end

# rspec text story

Then the page should have a create comment form

Nice and consistent, isn’t it? And compact, too:

\<% create\_table(:type =\> ‘DiningTable’) %\>

# will render:
 Add 

\<% search\_tables(:parameters =\> {:order =\> ‘name’}) %\>

# will render:
 Search 

This is just to give you a taste of what is possible. For more  
examples please refer to the rdoc for the individual ‘build\_[CRUD  
action name]\_helper’ methods (`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</
code> 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 W., released under the MIT license`

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 8, 2008, 9:37am UTC](https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861/2 "2008-05-08T09:37:57Z")

</div>

Hi all,

I changed the repo structure a bit, so the actual command to install  
the plugin is:

script/plugin install  
[http://svn.ingoweiss.com/plugins/resourceful\_views/tags/REL\_0\_1/resourceful\_views](http://svn.ingoweiss.com/plugins/resourceful_views/tags/REL_0_1/resourceful_views)

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 8, 2008, 10:22am UTC](https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861/3 "2008-05-08T10:22:01Z")

</div>

Very useful!. Why not use github?

Ingo W. wrote:

> Hi all,
> 
> I changed the repo structure a bit, so the actual command to install  
> the plugin is:
> 
> script/plugin install  
> [http://svn.ingoweiss.com/plugins/resourceful\_views/tags/REL\_0\_1/resourceful\_views](http://svn.ingoweiss.com/plugins/resourceful_views/tags/REL_0_1/resourceful_views)
> 
> Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 8, 2008, 11:18am UTC](https://www.ruby-forum.com/t/introducing-resourcefulviews-0-1/136861/4 "2008-05-08T11:18:11Z")

</div>

Thanks for the suggestion! I will look into it! Ingo
