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.
Trackbacks
Use the following link to trackback from your own site:
http://blog.uberweiss.net/trackbacks?article_id=sidebars-are-better-than-components&day=12&month=02&year=2008
Comments
-
I'm glad you like the Typo sidebar architecture - that's about version 4 of the API but it's been pretty solid for a while now. I remember being rather pleased with myself when I worked out how to do it.
-
Yeah, the idea is great -- I was pretty happy about how easy it was to extend the concept to fit what we needed.