Sidebars are better than components 2
This article made it across my RSS reader today. I ran into my own problem with this while writing a custom CMS for work. We wanted to have reusable components that could be added to CMS pages, which could take various parameters, could be cached, and could be viewed in different ways given a size. I investigated Rails components at work, but noticed that using those is discouraged by the Rails community.
My investigation brought me to Typo’s sidebar model, which I used as the basis for the model we ended up using for the prototype of the project. The ultra-simplified version of the model works like this:
We have a Sidebar base class, which inherits from ActiveRecord::Base. Sidebars inherit from this Sidebar class.
Which gives us something like this:
class Sidebar < ActiveRecord::Base
serialize :config
class << self
def params
@params ||= []
end
def param(name, type, options = {})
params << options.merge({:name => name, :type => type})
self.send(:define_method, name) do
self.config[name] || options[:default]
end
self.send(:define_method, "#{name}=") do |value|
self.config[name] = value
end
end
end
end
class StaticTextSidebar < Sidebar
param :content, :text, :default => "Hello, World!"
endSo now we have a way of defining sidebars and their parameters. The metaprogramming in the Sidebar base class allows us to programatically query the parameters declared in a Sidebar. This will be important later. For now, we still need to declare the view of a sidebar, so we do it in _static_text_sidebar.rhtml:
<%= sidebar.content %>Now, we add a helper to application_helper.rb to render the sidebar:
def render_sidebar(sidebar)
render :partial => sidebar.class.name.underscore, :locals => { :sidebar => sidebar }
endand then we can call render_sidebar in any of our views on an instance of a sidebar to render it. It’s not perfect, but it’s good enough for a prototype!
From here, we have a very basic reusable model-view framework that we can include in any of our pages. Sidebar instances can be associated with content on a page to be displayed, and their configuration can be serialized to the database along with the items they display with.
Creating and configuring sidebars can be done programmatically, by generating a form based on the parameters a sidebar takes and placing that form data into the sidebar, the same way one would with a standard ActiveRecord object. Their parameters can be validated using standard Rails validations and the result of the render_sidebar call can be cached.
This basic idea, with a little bit of work, can easily form the basis for a simple reusable component architecture, and we’ve been having a ton of success with it so far.
RailsConf 2006 Keynotes
It's almost exactly a year late, but I stumbled upon the video for the RailsConf 2006 keynotes. I'm hoping (and expecting) that they do something like this again for the RailsConf that just occurred last weekend, since I wasn't able to go. The keynotes for last year's conference were really good. I've never really been a huge fan of Paul Graham, but his keynote was excellent, as was Martin Fowler's keynote on "Why Ruby?"
It's hard to find the time to watch hour long videos, but I do have an hour-long bus ride to work. I figured transcoding the video into audio would be a good idea, so that I could listen to the talks on my Shuffle. This turned out to be much easier than I expected. It was trivial to get Quicktime Pro to export the audio to WAV, after which I could just add the WAV to iTunes, right-click, and convert to AAC. Checking the "Remember Position" option for the AAC track is crucial, of course, for an hour-long talk. A few minutes after having the idea to listen on my iPod, I had a few hours of smart people to keep me busy on the long traffic jam we call the 520.