Building Tempo with Rails, Part IV
Posted by Brian Sam-Bodden

In the last three installments of this series we concentrated our efforts on building the most important entities of the Tempo domain. If you are anything like me you need visual feedback to truly get a sense of progress, if anything for the psychological effect of seeing something working (other than on the command line)
Before we jump into building the visual aspects of the application we should finish configuring the restful_authentication system we installed in installment #1.
Configuring Restful Authentication
The first step is to protect all controllers in our application by applying a before filter that will trigger the authentication logic. In Rails the ApplicationController is the ideal place to do so since is the default parent class for all controllers in an application:
class ApplicationController < ActionController::Base include AuthenticatedSystem session :session_key => '_tempo_session_id' before_filter :login_required end
In the file application.rb we include the AuthenticatedSystem and apply the before filter :login_required which will cascade down to any new controllers we create.
The restful_authentication plugin creates two controllers, the UsersController; a typical CRUD controller for User objects and the SessionsController which is the controller responsible the login/session management logic.
Taking a peek at the Session controller we see that it is set to skip the :login_required filter:
class SessionsController < ApplicationController skip_before_filter :login_required ... end
In the Views/sessions directory we customize the new session view (a.k.a the login screen):
Welcome to Tempo, please log in
<% form_tag session_path do -%>
- Username:
- <%= text_field_tag 'login' %>
- Password:
- <%= password_field_tag 'password' %> (I forgot my password/username)
- <%= check_box_tag 'remember_me' %>Remember me on this computer
- <%= submit_tag 'Log in' %>
<% end -%>
Permanent Scaffolding?
To give us a jump start in the right direction I'm going to make use of a Rails scaffolding plug-in that should allow us to get some fairly decent web pages up and running in no time. There seems to be two contenders in the area of "permanent" Rails scaffolding plugins, one is the Streamlined Framework produced by the brilliant folks at Relevance the other one is ActiveScaffold created by the same team that built the now deprecated AjaxScaffold.
Both frameworks attempt to provide you with a way to generate ActiveRecord-driven scaffolding screens that are configurable and flexible enough that you theoretically won't have to later tear them down to build the "real" pages of you application. I've briefly looked at both frameworks previously and they both seem fairly even in terms of features. So just for the sake of trying something new I'm gonna use ActiveScaffold for the Tempo time tracking application.
ActiveScaffold
The ActiveScaffold website has quite a bit of documentation that should help you get started. I in particular found their "How to Approach Active Scaffold" article useful in understanding where it would be a good idea to use the framework.
To get started with ActiveScaffold in the Tempo project you'll need to first install the plug-in using Piston:
piston import http://activescaffold.googlecode.com/svn/tags/active_scaffold vendor/plugins/active_scaffold Exported r633 from 'http://activescaffold.googlecode.com/svn/tags/active_scaffold' to 'vendor/plugins/active_scaffold'
Once you have installed the plug-in all that you need to do to enable the scaffolding for one of you model objects is to add a minimal amount of code to your controllers:
class UsersController < ApplicationController active_scaffold :user end
In your application layout head section you also need to include:
<%= javascript_include_tag :defaults %> <%= active_scaffold_includes %>
That will give you the basic ActiveScaffold CRUD functionality.
Read more...

