Monday, September 3, 2007

Active Scaffold

Getting Started

So you heard about this awesome new plugin called ActiveScaffold and you want to try it out. Good because it is as easy as 1 – 2 – 3.

1. Install the latest version of the plugin:

./script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold 

or you can install from trunk (http://activescaffold.googlecode.com/svn/trunk) and get the latest updates as the core team submits them. However, the trunk version can be highly unstable (perhaps not even full tested) and is not recommended.

2. Add this to your layout:

<%= javascript_include_tag :defaults %> <%= active_scaffold_includes %>

3. Add this to your controller:

active_scaffold :<your_model_name>

.. for example:

class UserController < ApplicationController   active_scaffold :user end

3a. Make sure that you don’t have AjaxScaffold installed in your project. Since ActiveScaffold evolved out of AjaxScaffold they share some common method names and are incompatible with each other.

DONE!

Too Easy?

OK, maybe some context would help on the insert steps.

The <head> tag in your layout should look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />  <title>Generic ActiveScaffold Layout</title>  <%= javascript_include_tag :defaults %>  <%= active_scaffold_includes %> </head> <body>  <%= yield  %>  </body> </html>

And your controller should look something like this:

class CompaniesController < ApplicationController   layout "admin"   active_scaffold :company end

If you named your controller the same as your model you can even simplify it to:

class CompaniesController < ApplicationController   layout "admin"   active_scaffold end

Is that concise or what?

A Little More Flexibility Please

First let’s introduce the global config block. You probably noticed that the active_scaffolding includes everything in the table. Let’s remove a few of these columns with one easy config block. Put this one in your ApplicationController.

class ApplicationController < ActionController::Base   ActiveScaffold.set_defaults do |config|      config.ignore_columns.add [:created_at, :updated_at, :lock_version]   end end

Let’s have a look at the local config block. This block goes in the model’s corresponding controller. The config block for the Company model goes in the CompaniesController. ActiveScaffold restricts one model per controller. Now for an example:

class Admin::CompaniesController < ApplicationController    active_scaffold :company do |config|     config.label = "Customers"     config.columns = [:name, :phone, :company_type, :comments]     list.columns.exclude :comments     list.sorting = {:name => 'ASC'}     columns[:phone].label = "Phone #"     columns[:phone].description = "(Format: ###-###-####)"   end  end

You did request more flexibility and here you have it. Change the label of the ActiveScaffold, decide which columns to include in the ActiveScaffold, control the columns included at the action-level, define a default sort order, specify a column label and a column description.

Some Words to the Wise

AjaxScaffold (in)compatibility

AjaxScaffold and ActiveScaffold are incompatible. They can not both function at the same time. The problem is that they share some method names but don’t share the complete method signature (e.g. different arguments). So whichever plugin loads last will work.

If you need to keep AjaxScaffold around while you migrate your application, then you should be able to switch between which one works by specifying the plugin loading order in Rails 1.2 (or renaming the plugin directories in Rails 1.1). For example, in your environment.rb, you can define a set of plugins which will also be used for the loading order. Try something like this:

Rails::Initializer do |config|   config.plugins = ["ajax_scaffold", "active_scaffold", "other_plugin_a", "other_plugin_b"] end

Generating scaffold_resources

If you are starting up a new application and want to use controller resources and RESTful routing, then you should be aware that script/generate scaffold_resources requires some tweaking to operate with ActiveScaffold. After running this generator, you’ll want to open up the generated controller, empty out all the built-in Rails scaffolding, and replace it with the active_scaffold :my_model call. Then, you’ll want to open up config/routes.rb and edit the new map.resources line and add an :active_scaffold => true argument.

No comments:

analytics