Recipe 2.11. Creating Full-Featured CRUD Applications with Streamlined


Problem

Many Rails applications require an administrative area that allows you to operate on the data in your models and on the relationships of that data. To avoid repeating yourself from one project to the next, you want a way to construct full-featured CRUD applications for each new project. Because these are only administrative applications, they don't need to be pretty, so Rails' standard scaffolding would be almost adequatebut not quite. Scaffolding really is ugly. More to the point, it really doesn't help when you have to manage a set of tables, with interrelationships between those tables. Is there anything better?

Solution

Use the Streamlined framework to create a customizable administrative interface for your application.

Start by downloading and installing the streamlined_generator gem:

$ wget http://streamlined.relevancellc.com/streamlined_generator-0.0.5.gem $ sudo gem install streamlined_generator-0.0.5.gem             

We'll assume you already have an existing database schema. For example, you have tables named galleries and paintings where a painting can belong to a gallery. Create a Rails application, if you haven't already, with:

$ rails art_gallery             

Then move into your application directory, and generate a Streamlined application using the Streamlined generator:

$ cd art_gallery/ $ ruby script/generate streamlined gallery painting             

You pass the generator all of the models that you want included in the resulting Streamlined interface. You can now start up your application and point your browser at /galleries or /paintings to access your Streamlined interface.

What makes Streamlined much more powerful than the default Rails scaffolding is that it detects relationships that you set up between your models and then adds widgets for controlling those relationships to the interface.

To set up a one-to-many relationship between galleries and paintings, add the following to your model class definitions:

app/models/gallery.rb:

class Gallery < ActiveRecord::Base   has_many :paintings end

app/models/painting.rb:

class Painting < ActiveRecord::Base   belongs_to :gallery end

In development mode you'll see the added column in both the Gallery and Painting views with information about how one model is related to the other.

Discussion

Many of the Rails applications you work with require administrative interfaces, such as those created by Streamlined. This section of a site is often not accessible to normal users and really doesn't need to be visually polished, but it does need to work; usually performing basic CRUD operations. Whereas Rails scaffolding is meant to be a temporary structure that you eventually replace with your own code, a Streamlined interface is designed to be production-ready code.

Figure 2-5 shows the resulting Streamlined interface displaying the Paintings list view.

Figure 2-5. A Streamlined administrative interface for Paintings and Galleries


One of the stated goals of the Streamlined developers is to replace the default scaffolding of Rails with more robust, useful, and meaningful management screens. As you can see, the resulting interface is much more presentable than the default scaffolding. While you might not want to use it for the customer-facing side of your web site, it's certainly good enough for a demo and more than adequate for a management interface. Some of the features it provides are links to all the models you passed to the Streamlined generator, sortable columns, a search filter, and an interface for editing the relationships between model objects.

Another goal of the project is to let you customize the application's views using a declarative syntax that's similar to Active Record (e.g., belongs_to :gallery). For example, to change the way the Gallery view displays the paintings that belong to it, you would add the following to the GalleryUI model definition:

app/streamlined/gallery.rb:

class GalleryUI < Streamlined::UI   # relationship :paintings, :summary => :count # the default   relationship :paintings, :summary => :list, :fields => [:name] end module GalleryAdditions end Gallery.class_eval {include GalleryAdditions}

This class displays a list of paintings for each gallery under the Paintings column. The commented-out declaration displays the total number of associated painting records in a column.

You can edit and show operations in the Streamlined interface open windows in the browser. These windows are based on code inspired by the script.aculo.us effects library. Edits made in these windows update the page that spawned them using Ajax.

Pass the --help option to the Streamlined generator for more information on usage options. Also visit the Streamlined wiki: http://wiki.streamlinedframework.org/streamlined/show/HomePage.

See Also

  • Streamlined Project home page, http://streamlined.relevancellc.com

  • Streamlined Project wiki, http://wiki.streamlinedframework.org

  • Section 1.9"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net