[typo] Sidebars

Piers Cawley pdcawley at bofh.org.uk
Mon Mar 20 12:00:34 EST 2006


I love sidebars, sidebars are cool.

I'm less enamoured of our current implementation of 'em, because
they're components and components are slow and a pain in the bum to
test.

With that in mind I've been playing with turning them into plugins
that delegate to a controller for almost everything. The basic parent
might look something like (with lots of handwaving)

class PluggableSidebar
  @@subclasses = {}
  cattr_accessor :controller
  class << self
    # Keep track of our subclasses and have them keep track of theirs
    def inherited; ...; end
    def subclasses; ... ; end

    def available_sidebars
      subclasses.select {|sidebar| sidebar.subclasses.empty?}
    end
  end

  class SetupSidebarFilter
    class << self
      def before(controller)
        PluggableSidebar.controller = controller
      end
      def after(controller)
        PluggableSidebar.controller = nil
      end
    end
  end

  def method_missing(*args)
    if @@controller
      @@controller.send(*args)
    else
      raise NoMethodError, "undefined method #{args[0]} for #{self.class}"
    end
  end

  def render(*args)
    @performed = true
    @@controller.render_to_string(*args)
  end
  
  def performed?
    @performed || false
  end

  def configuration_wrapper
    with_include_path(self.class.view_path) do
      configure
      render :partial => "configure", :locals => {sidebar => self} unless peformed?
    end
  end

  def index
    with_include_path(self.class.view_path) do
      content
      render :partial => "content", :locals => {sidebar => self} unless peformed?
    end
  end
end

Lots of stuff missing, obviously, but the basic goal is to turn
the Sidebar parent class into something that *feels* like a controller
to someone writing one, but which can be called with a helper without
having to go through the rather heavyweight process of rendering a
component. 

Also, I want to be able unify the current Sidebar and
SidebarController classes in such a way that a Sidebar can be
serialized to YAML and stored on the blog object, and in a
:staged_sidebars key in the flash.

Thoughts?

-- 
Piers Cawley <pdcawley at bofh.org.uk>
http://www.bofh.org.uk/


More information about the Typo-list mailing list