The Caboose Sample Rails Application v3

Courtenay : April 11th, 2007

Update: Please see http://sample.caboo.se/ for the latest version of our sample apps.

Get it here:

svn://caboo.se/plugins/court3nay/empty_apps/restful_auth_rspec

Contact the author (you'll need to add .com to the email):

<court3nay+sra@gmail>

Requirements:

You'll need a few gems.

  • rspec
  • image_science OR rmagick

Changeset:

  • removed 'with_scope' and replaced it with protocool's awesome polymorphic controller code.
  • added his resource_fu plugin for restful helper methods

The most recent changes are due to Trevor/protocool, the author of the resource-fu plugin. It seems like nested resources in a polymorphic form are extremely difficult to implement in a clean manner.

Here's a snippet of code:

delegate :images, :to => '@attachable'
helper_method :images

def load_image
  @image = images.find(params[:id])
end

Beautiful. It relies on the images controller being an abstract class, defining @attachable in the inherited class, and delegating "images" helper method to that @attachable object.

From the readme:

Caboose Sample App

This is a sample application containing a bunch of useful plugins and code to help you get started quicker.
It is intended to be an example of "better practices", but with the intention of ultimately being "best practice" through collaboration and iterative improvement.

The application implements many of the basic and advanced features I find myself using in every new project:

  • a user model with a few extra fields (last login, timezones)
  • restful authentication
  • simple authorization (can't edit other's profiles)
  • full timezone support
  • basic views implemented with Yahoo! YUI CSS grids
  • behavioral testing with rspec, including good example of mocking in controller specs
  • exception notification (remember to change the email address in environment.rb)
  • nested resources (user/images) which can easily be extended (e.g. product/images)
  • polymorphic image model
  • around_filters to set and clear request-specific data

Many of the enhancements to the stock plugins are ripped from my private codebase, as yet unseen by the world :)

TODO:

Sample controllers and models More useful plugins Good examples of ajax Better list of requirements (required gems, etc)

TODONE:

x Polymorphic Image model using attachment_fu

10 Responses to “The Caboose Sample Rails Application v3”

  1. antonio Says:

    I would love to see open-id not in your agenda .. I mean integrated with restfull authentication..

    It is really really great that experienced developers like you care about newbies to develop better coding practice.

    Thanks again for your effort!

  2. Danger Says:

    Ahoy! Courtenay, this is fantastic. I’m in the middle of a writeup about this on http://6brand.com and I just realized that the sample app has a ‘sessions’ controller. Could you switch that to a ‘session’ controller and route it with map.resource :session ? In it’s current form we’d need to use sessionspath(1), :method => ‘delete’ just to logout. It’d be nicer to use sessionpath, :method => ‘delete’.

    Again, this is a major timeesavor. I’m starting a new project based on it today and I’m LOVING it already.

    A couple other wishlist items:

    • remove the components folder?
    • Uh… I guess I just had one.

    Thanks Courtenay!

  3. Trevor Squires Says:

    Danger - good point about the session resource - it’s done.

  4. Larry Kluger Says:

    Hi,

    Thanks for your time on this. Some comments: 1) Gem tzinfo is also required by the app. 2) Note: database.yml needs to be created before trying the sample app 3) Note: a fresh RoR environment is needed. gem install rails and gem update –system
    will help if there are startup errors. 4) Note: run rake db:migrate to create the db tables

  5. Fortuity Says:

    So very useful! Thanks! I’m looking forward to further iterations and elucidations of best practices.

    But you might want to look at this. I tried a simple replacement of a link with an image in the app/views/users/index.html file:

    It was: <%= linkto 'Edit', edituser_path(user) %>

    I tried: <%= linkto imagetag('pencil.png', :alt=>‘Edit’), edituserpath(user) %>

    and it failed with: userimageurl failed to generate from {:controller=>”userimages”, :action=>”show”, :id=>”add.png”}, expected: {:controller=>”userimages”, :action=>”show”}, diff: {:id=>”add.png”}

    because of the line in routes.rb: user.resources :userimages, :opaquename => :images

    What’s up with that?

  6. Trevor Squires Says:

    Fortuity: there is an unfortunate side-effect of having a resource called ‘image’ - namely a conflict with Rails’ in-built image_path() helper.

    Cour3nay and I spoke about it a few days back and at the time we both agreed that the robust solution would be to submit a patch to remove any _path() helpers from Rails (and probably also to provide a plugin that’s compatible with the existing releases).

    A more immediate solution would be to rename the Image model and resource to be something that doesn’t conflict with image_path.

    Trevor

  7. Fortuity Says:

    Thanks Trevor and Cour3nay! Namespace collisions are a bitch.

    I took a shot at renaming the Image model and ended up with a big broken mess. How about releasing a new rev of the sample app with the Image model renamed to Graphic (or Picture)?

    Loving what you’ve done so far, though.

    Fortuity

  8. quellhorst Says:

    I noticed that in the users controller that the xml for the show action gives the value of the crypted_password and salt fields.

    A quick fix would be to change: format.xml { render :xml => @user.to_xml } to format.xml { render :xml => @user.toxml(:except => [:cryptedpassword, :salt]) }

    -Dan

  9. court3nay Says:

    good catch dan. I’ve done this in the user model instead.

  10. graste Says:

    Nice sample application. A quick question though: as timezone awareness is already built in - is it planned to include something like ‘globalize’ to support multilingual applications? Or let’s say completely internationalized applications. I would say, that it could be important as nowadays more and more apps are being developed for several languages.

Sorry, comments are closed for this article.