These are the steps I followed when re-writing my beloved Bloglines Blog Roll sidebar. I didn’t write it originally, but this is the second time I’ve re-written it, so I’ve become attached.
Step 1
Throw out the old component based sidebar. There’s very little code re-use. You might want to save the controller, but that’s it.Step 2
Generate a rails plugin. This sets up a nice skeleton. I followed the convention of ending the plugin name with _sidebar. I don’t think it’s absolutely necessary.ruby script/generate plugin bloglines_sidebar
this will create a directory tree in vendor/plugins
create vendor/plugins/bloglines_sidebar/lib
create vendor/plugins/bloglines_sidebar/tasks
create vendor/plugins/bloglines_sidebar/test
create vendor/plugins/bloglines_sidebar/README
create vendor/plugins/bloglines_sidebar/Rakefile
create vendor/plugins/bloglines_sidebar/init.rb
create vendor/plugins/bloglines_sidebar/install.rb
create vendor/plugins/bloglines_sidebar/lib/bloglines_sidebar.rb
create vendor/plugins/bloglines_sidebar/tasks/bloglines_sidebar_tasks.rake
create vendor/plugins/bloglines_sidebar/test/bloglines_sidebar_test.rb
Step 3
Go to vendor/plugins/bloglines_sidebarStep 4
Perform the magic that is necessary in init.rb.NB: I should disclaim that I am no programmer, I am especially not a ruby programmer. There is stuff that I did, that I have no idea what, if anything, it does.
cat init.rb require 'bloglines_sidebar' BloglinesSidebar.view_root = File.dirname(__FILE__) + '/views'
Step 5
go to the lib directory, and build up your sidebar controller.In the old component sidebar there were two rhtml files to display, and they were called from a standard controller, with the typical
class Foo def configure end def content end endstyle
This controlled:
1. How the sidebar configuration looked in the admin console.
2. How the sidebar looked in the pages.
Then all of the other work was done in the view.
Now, all of the configuration view stuff is done in the bloglines library, using the setting function.
My vendor/plugins/bloglines_sidebar/lib/bloglines_sidebar.rb looks like this:
The first six lines of the class set up the configuration. display_name and description are the same, then you just use setting function to do two things.
1. Set any default configuration.
2. Set up the html for the sidebar configuration.
My example doesn’t actually illustrate point 2 very well, but you can actually make each setting a different type of html form, like radio button. or text_area. I’ll put up an example here soon.
class BloglinesSidebar < Sidebar
display_name "Bloglines"
description 'Display your Bloglines Blogroll in your sidebar'
setting :title , nil, :label => 'Title'
setting :user , nil, :label => 'User'
setting :folder, nil, :label => 'Folder'
Then, all I have to do is setup a bloglines method that I can call from the view. This one is simple. Notice how folder and user are referenced from the settings above
def bloglines
if folder.nil? || folder.empty?
feed = "http://rpc.bloglines.com/blogroll?html=1&id=#{user}"
else
feed = "http://rpc.bloglines.com/blogroll?html=1&id=#{user}&folder=#{folder}"
end
open(feed).read
end
end
Step 6
Now that we have the controller all set up, we can just add a view. Go back to `vendor/plaugins/bloglines_sidebar` and add a `views/` directory, then add a content.rhtml file with the following contents<h3><%= sidebar.title %></h3> <%= sidebar.bloglines %>
