Jonathan Leighton

(dot com)

  • Home
  • Blog
  • Projects
  • Photography
  • Music
  • Portfolio
  • CV

Restflection

Restflection is a Ruby on Rails plugin designed for use in applications which follow RESTful conventions. It allows you to extract information about resources without necessarily knowing what they are called.

For example, instead of writing article_path, you could write restflect(:member).path. Why would you do this? You may have shared code, for instance layouts or library files, which need to make use of a resource, but because they operate on any number of different resources, they cannot refer to them by name. The plugin could also be of use to other plugins.

Installation instructions

The Restflection source code is available at http://svn.turnipspatch.com/public/restflection/. There are a few methods available to get it into your Rails project:

  • SVN Export: script/plugin install http://svn.turnipspatch.com/public/restflection/
  • SVN Externals item: script/plugin install -x http://svn.turnipspatch.com/public/restflection/
  • Piston: piston import http://svn.turnipspatch.com/public/restflection/ vendor/plugins/restflection

Conventions

As mentioned above, Restflection assumes you follow “RESTful conventions”. What does this mean exactly? Well, if we are talking about an article resource:

  • The article will be stored in the instance variable @article
  • A collection of articles will be stored in the instance variable @articles
  • The controller will be named ArticlesController
  • The model will be named Article

How Restflection exposes itself

Once installed, you can use Restflection from the controller or the view via the restflect method:

restflect(on)

Returns an instance of Restflection::ResourceReflection

  • on should be one of:
    • :collection – reflects on the collection of resources
    • :member – reflects on a single resource

Two convenience methods are also provided by the view:

  • member – Returns the same as restflect(:member).it
  • collection – Returns the same as restflect(:collection).it

What ResourceReflection provides

Pretend we are reflecting on an speckled_hen resource…

  • it – @speckled_hen
  • it=(foo) – @speckled_hen = foo
  • name – "speckled_hen"
  • human_name – "speckled hen"
  • titleized_name – "Speckled Hen"
  • instance_variable_name – "@speckled_hen"
  • klass – SpeckledHen

And now for my last trick, the one that you’ve all been waiting for…

You can call path and it will call speckled_hen_path, you can call edit_path and it will call edit_speckled_hen_path, you can call formatted_edit_url and it will called formatted_edit_speckled_hen_url, you can call hash_for_formatted_my_god_this_is_long_path and it will call hash_for_formatted_my_god_this_is_long_speckled_hen_path...

Etcetera.

One more thing: Restflection looks up to see whether the action you are calling a url helper for is a member action. If it is, Restflection will automatically pass the member to the url helper. In other words, path becomes speckled_hen_path(@speckled_hen). However, if you specify any arguments then you’re on your own, buddy.

Rolling your own

If you’re using Restflection outside of a view or controller, you might want to create your own ResourceReflection like so: Restflection::ResourceReflection.new(controller, name). You should probably read the Restflection source a bit for more detail.

Other notes of noteworthiness

  • If you have defined a :name_prefix on your route, Restflection will still work. However, this is a bit of a hack as I had to monkey-patch Rails core a bit, so it may break in future versions.
  • The code has 100% code coverage with RSpec, so I hope it works for you :)
  • The code is licensed under the MIT Licence
  • Please use my contact page to send bugs, patches and messages of undying love to me

Contact Me | © Copyright 2008 Jonathan Leighton