Scaffolding Extensions and Ramaze - Getting off the ground
September 15th, 2008
Prelude
I want to start out this post with a big thank you to Jeremy Evans, the author of Scaffolding Extensions (SE) and the maintainer of the Sequel ORM. He is an exemplar of an OSS project leader. I've contacted him regarding both SE and Sequel via forums, bug trackers, irc, mailing lists, and on every single occasion, he's been extremely responsive and helpful. These are big reasons to use software he's involved with, especially in the often "just read the source and specs" Ruby documentation culture. (In fact, he should publish what he does to run his projects so that other OSS project owners can learn from him).
Scaffolding Extensions rock
Scaffolding Extensions rock for developing apps and for building admin interfaces (or even for allowing users to edit only their own data). They are very ORM and web framework agnostic, and offer powerful and flexible customization options. There are, however, a few pitfalls when you start using SE.
Require your ORM before Scaffolding Extensions
require 'sequel'
require 'scaffolding_extensions'
This may seem obvious, but it's one of those "duh" mistakes to avoid. SE adds a number of methods to Sequel classes and therefore those need to exist before SE.
Create a controller for your scaffolding
For Ramaze this looks like this:
class AdminController < Ramaze::Controller
map "/admin"
helper :aspect
layout :layout
scaffold_all_models :only => [Post, Comment]
before do
# some authentication/authorization
end
end
The most important line here is the call to scaffold_all_models. You can call it without parameters, and SE will try to find your model classes automatically. This can fail, however, if your code layout doesn't follow the ramaze --create prototype code layout. Adding all the model classes explicitly via :only => [Class1, Class2] takes care of that. This option can also useful to exclude the users table for example.
Secondly, we're setting up a layout for later styling of our scaffolded views. This layout file will be located at view/admin/layout.rhtml (you have to create it), according to Ramaze's default configuration.
Lastly, before you put your app out in the wild, you should add some authentication/authorization to keep people away from your database.
Customize your scaffolding
SE allows you to override a lot of methods and properties. How exactly that's done depends on the particular properties. Some use method overrides, for others you simply set instance variables. Look at the ScaffoldingExtensions::MetaModel documentation. It offers a great overview of available options and how to set them.
Let's say you have a Post class with a title and body field, but in the scaffold the body field by default displays as an html input type=text instead of a textarea. No problem, the code below fixes exactly this problem.
class Post < Sequel::Model
@scaffold_column_types = { :body => :text }
end
Customize your styling
As mentioned above, you probably want to improve the default styling of your scaffolding. I would recommend taking the default SE layout.rhtml file and adding your own styles to it. You can get it from github or via gem unpack. For example, you can then add a textarea style to better suit your post body sizes.
Read the documentation
Read the README file, and read the files in the 'doc' folder. You can either find the source on github, or you simply do a quick gem unpack scaffolding_extensions and browse everything locally.
Conclusion
I hope this helped you get off the ground quickly. If you have any Scaffolding Extensions tips or tricks, please post them (or a link) in the comments.
Leave a Reply