Reasons to be edgy...
I’ve been busy with a new experimental branch of typo that plays well with edge rails, in part because I wanted to move to a more RESTful URL scheme for Typo.
But then I took a look at some of the things going on in Rails Core and my nipples exploded with delight. For instance, there’s now a ResourceFeeder helper that pretty much magically deals with Atom and RSS feeds for Rails Resources, which would allow us to eliminate a whole pile of ugliness in the Typo source. So that’s a win.
But then, I just discovered that Rails now has ‘real’ around filters which allow you to implement a filter as a block which calls yield. Which means that it’s now easy to write filters that clean up their resources properly, even in the face of exceptions being thrown further down the filter chain; you just catch the exception, do your cleanup and rethrow. Which allows us to eliminate a whole category of possible typo memory leaks and simplify a few things that are currently causing us pain. Result.
Applying design patterns unconsciously 1
I just realised something about the workings of new style Typo Sidebars: it’s just an application of the Parameter Object design pattern; the render_sidebar helper method takes a Sidebar parameter object and produces a chunk of HTML. The fact that we persist the parameter object using ActiveRecord is almost beside the point – the persistence is more important to the render_sidebars method than anything else.
Old style sidebars couldn’t really be called an application of any design pattern. Maybe that’s what bugged me about them, they lacked the Quality Without a Name. I’m not sure that the new sidebar architecture has the Quality either, but it’s much nearer to having it.
When I was learning to play Go someone told me that it’s easier to remember professional games than those of amateurs because the moves in a professional game are generally more ‘right’ than those of weaker players. To a good go player, the ‘right’ move has an obvious purpose (or, more likely, purposes). Bad moves don’t.
Maybe something similar applies to code. In good code it’s clear what each piece does – maybe it can be seen as an example of a design pattern, maybe not – and the pieces relate well to each other. In bad code, the intent and relationships are muddied.
Do Design Patterns Help?
Continuing the Go analogy, Design patterns can be likened to joseki – ‘standard’ patterns of good play in the opening of the game. It’s useful to know them, but what’s really important, and what takes time, practice and deep study to get good at, is using and adapting them well in the fuseki – the opening and its wider context. You can play a ‘perfect’ joseki in one little corner, but its influence radiates across the board and if you don’t pay attention to what that influence is and use or adapt it well, you’re screwed.
I think the analogy to the use of design patterns in programming is fairly obvious. Don’t you?
Dogfood time
I’ll let you into a secret. You can tell when I’ve done a big svn up on this blog simply by looking at the theme. If it’s all black and white and sans serif, then it’s a racing certainty that I’ve just done an upgrade which has broken my usual custom theme.
So, later today I shall be sitting down with my local theme repository and bringing things up to speed with all of Scott’s changes. Who knows, I might cheat and put back a few helper methods.
Update
Oops, it seems that there was a bug in the Scribbish theme which meant you couldn’t read articles at their permalink…
Cunning tricks with the new typo sidebars
I was going to save this until I’d rejigged my blog to a three column layout and had left everyone mystified by how I’d managed to get sidebars on both sides. But, let’s be honest here, it’s going to be ages before I do that. So, I shall spill the beans preemptively.
Actually, you’ve been able to do this, or something like it for ages, but new style sidebars make it easier.
Imagine you’re editing the layout of your new, three column theme, and you’ve got a left-sidebar div, which you want to populate with a tag cloud and the categories sidebar.
<div id='left-sidebar'>
<%= render_sidebar TagSidebar.new :maximum_tags => 30 %>
<%= render_sidebar CategorySidebar.new :show_empty_categories => false %>
</div>Then it’s just a matter of styling to get the sidebar in the right place.
This works because the vast majority of sidebars don’t hold any state apart from their settings (Certainly none of the core sidebars do) and their position in the sidebar list. So, if you want to render particular sidebars in your layout, you don’t need to go to the database at all.
Of course, I can conceive of sidebars that will need to manage persistent state, but now I’ve realised that you can manage sidebars like this without having to go to admin/sidebar at all, I shall be working on making things a little bit friendlier.
If nothing else, I should probably write a helper that lets you do:
render :sidebar => TagSidebar, :maximum_tags => 15to make the interface a little more consistent with the rest of rails’ suite of render helpers.
Enjoy.
Update
- Now that extended content is working properly again, I’ve moved the details back below the fold.
- Dan Milliron has another neat way of doing this.
Victory! For now
Typo Sidebar’s are now controller free!
Which probably means nothing to the vast majority of people reading this. But it makes me very happy.
Until a couple of revisions ago, Typo sidebars were rendered through a convoluted route, which involved n + 1 calls to render_component, where n is the number of items on the sidebar. Calling render_component isn’t something you really want to do even once if you can help it. It’s not as slow as it used to be, but it’s still unpleasant.
So, now a typo sidebar looks pretty much like any other Rails plugin. It lives in vendor/plugins with a model, a view, a test and an only mildly clever init.rb.
For the MVC purists among you, there’s a small amount of shenanigans involved because the model carries information about where to find the sidebar’s template, but I shall plead necessity and have done with it. Besides, the model never makes direct use of the information, it just holds onto it for the controller.
The upshot of this, and Scott’s changes is that Typo’s components directory is now empty. And you can call me Mr Happy!
You don’t have to call me Mr Happy of course, but I reserve the right to be happy anyway.
Expect a quick introduction to writing your own sidebars soon.
Pulling a surprise all nighter
Guess who couldn’t sleep?
So, I’ve been productive and have ported 9 Typo sidebars to the new, controller free, sidebar architecture. Sadly, it’s not something I can write a program to do, but the work involved is still fairly minor.
It’s still a spike though; once I’ve done the other 20, I need to work out how to get their views into the plugin directory as well.
Still, progress is good.
Other viewpoints, and why they are important
Many thanks to Paul Ingles who has just written a fantastic article about writing a Typo sidebar in a test driven way.
For too long we’ve been assuming that Typo sidebars are, in fact, detestable (“If it’s not testable, it’s detestable” – someone at the Sydney XP group, by way of Martin Fowler). Paul neatly demonstrates that sidebars are perfectly testable, we just haven’t been testing them. Bad us.
So, I’m rolling back the changes in my current sidebar rejig and starting again by writing some tests for a few of the core sidebars, and then I can start my refactoring without quite so much of The Fear.
So, once again, thanks Paul. It’s really great to occasionally see the curious outsider’s view of what we’re working on – it makes us question our assumptions and often shows us better ways.
Working round the Rails showstopper
So, it turns out that the rush released Rails 1.1.5 doesn’t actually fix the security problem. Worse, it seems that the problem lies somewhere in the nest of serpents that is the routing system. It turns out that some of the magic that lets everything work in nice ways doesn’t do enough to make sure that malicious people can make everything work in nasty ways.
The problem lies in the route that everyone has in their routes.rb:
map.connect ':controller/:action/:id'
Which is, frankly, a wide open door.
The fix, which I’m busy applying to Typo, is to get rid of the default route and replace it with an explicit set of routes for your controllers. So at the end of your routes.rb you’ll have something like:
%w{articles pages xml other controller names}.each do |c|
map.connect "#{c}/:action/:id", :controller => c
endIf you’re lucky, that’ll be all you need to do. If you’re less lucky, you’ll have a whole host of calls to url_for, link_to and their brethren that implicitly rely on the default route. In particular, url_for :controller => '/' will stop working. In typo that means grepping through the source and replacing that with url_for :controller => 'articles', but for your particular project you’ll need to change that to whatever your default controller is. You’ll probably find other painful spots too. Such spots are often your code’s way of telling you not to be quite so sloppy, but the pain still exists. For instance, we’ve had to add a route to a phantom ‘files’ controller.
With any luck, once I’ve made the routes explicit the problem of evil URLs will go away. I’ll let you know when I reach the point where I can test things, but I have high hopes of this approach. Expect a new Typo release soon.
Update 1
Yay! It seems to work. Changes have been checked into the typo repository, grab the head if you need it. Scott will be releasing 4.0.2 when he’s made sure I’ve not done anything stupid.
Update 2
Turns out, I had done something stupid. But I fixed that now.
Update 3
And, in a single stroke, I fixed things so that themes that use url_for :controller => '/' (which is most of ‘em) will continue to work. Now I’m off out for a few hours. Don’t break the internet while I’m gone.
A weekend in August 3
If you were to ask me what my current preoccupations were, the top three would probably be breadmaking, ruby and folk music. This last week has been a pretty decent week on all three fronts.
On Friday, I drove down to Shipton Mill near Tetbury, one of the finest millers in the country, and picked up around 40 kilos of interesting flour at splendidly wholesale prices (substantially cheaper than I was paying at my local suppliers and with far greater variety). I expect to have fun experimenting with a few new bread formulae as I work through that lot. After that, I took a quick trip to the VSCC’s Prescott meeting, where my brother was selling tyres
uninitialized constant Flickr::XmlSimple
Prescott’s really lovely, a beautiful site, some spectacular cars and a great atmosphere on the campsite. There were that many family members and Longstone staff on the site there was a virtual Longstone compound. I was sorry I couldn’t stay for the barbecued legs of lamb but I was spending the night with friends in Bath.
On Saturday morning it was off to the Sidmouth Folk Week. Sidmouth is one of the great institutions of British folk music – a week long festival that’s been running for over 50 years. These last couple of years have been run by a new set of organizers and things have been scaled back a little. It’s still Sidmouth though. I last went in 1998 (I think), so it’s been a while, but it still felt just like I remembered. I was crashing with a friend of mine who’s a Sidmouth virgin and acted as a semi-native guide. I think she might be hooked on it too.
I spent a fair amount of time with the Anchor Middle Bar Singers, a festival fringe institution that, whilst not quite as old as the festival, has been running for some time (they recently retired from competing in the “who can raise the most money for festival funds” stakes having been undefeated in this respect since about 1981). The Middle Bar is a twice daily singaround concentrating exclusively on unaccompanied singing, preferably with a chorus. It’s hot, loud, and has its own set of traditions for How Things Are Done.
For Instance, some songs have a ‘standing chorus’; when the singing reaches the chorus of, say, Thousands or More everyone stands for the choruses so, if you’re not completely au fait with which songs have standing choruses, it pays to keep your eyes open.
Monday night was my last night in Sidmouth and when it looked like the twig wasn’t going to make it around the room more than once (a twig is passed around the room to signify who’s singing next) I mentioned that I would really appreciate getting a second song to one of the people on the bench (the people who run the session and who start and finish the singing) and, bless him, he swapped places with me at the end of the night so I was one of the last three singers.
I sang Si Kahn’s song Here is my home, a secular hymn about the fellowship of singing in harmony. It’s a great song with plenty of opportunity for the chorus to join in (it doesn’t just have a chorus between verses, it has them within the verses too) and the singers in the bar were on top form that night, they were sounding wonderful. What nearly stopped me singing though was when the last chorus came around. I’d closed my eyes as I went into it and when I opened them again the whole bar was on its feet belting it out with me. Not something I’ll forget in a hurry.
So, that’s bread making and folk music attended to
On Tuesday I spent the morning in another singaround in the theatre bar before heading off to London for the London Ruby Users’ Group meeting at Skills Matter. A couple of cracking talks (about Domain Specific Languages and tips on working well with front end types) both excellent, one of which was very much last minute after Geoffrey Grossenbach had to cancel when a proposed London workshop he was planning to give fell through. Once the technical stuff was out of the way (I might write more about them when I’ve mulled them over a bit more) we retired to the pub and spent the rest of the evening talking about Ruby, Rails, Smalltalk, Perl 6 and probably a bunch of other stuff. I shall have to make it down to London more often.
Estimating driving time from Devon to London is never going to be an exact science, so I arrived in Clerkenwell about an hour before the meeting and, not being one for sitting in a pub by myself, I repaired to a nice shady bench, pulled out my powerbook and did a bit of light hacking on stuff. I was just starting to get a bit of flow going when someone came into the park and recognized me. Which is just weird. This is the first time I’ve ever been recognized by a stranger. Admittedly, I was wearing the same shirt as I’m wearing in the photo in the sidebar, but still. Embarrassingly, I’ve forgotten the chap’s name – with any luck he’ll comment here and jog my memory.
So, in all a jolly good extended weekend. Flour, songs, and my first microfame moment. Now, if I could just work out how to do that for a living…
Typo 4
Yay! Not only have we released Typo 4.0.0 at last (big news: massively improved feedback spam protection and much, much easier installation), but I’ve brought this blog back to the bleeding edge (complete with further improved feedback management).
I was about to say that most of the changes are only visible to the blog administrator, but that’s probably because I’ve been developing it; you don’t really notice the changes when you’re making them in fairly small steps. There are reader visible changes, mostly in the sidebars over on the right, all sorts of goodies there compared to the last release.
Bulking up
One change which can’t really be said to be for the better, is that our memory footprint has shot up. We’re no longer the lean, mean, aggressively caching machine we were I’m afraid. However, my fellow maintainers and I are keen to get the footprint back down in order to play nicely with everyone’s hosting service. (There have been revisions on the trunk that were, unusable on several hosting services because of a memory leak that we took far too long to track down.)
Still, you live and learn.
Architectural changes
4.0.0 caught us halfway through a change in the way the state of articles, comments and trackbacks are managed. In the old days, content was either published, or it wasn’t. The trouble with that idea is that it’s hard/impossible to ensure that notifications and pings only get sent when content is first published. So just before 4.0.0 I switched to using the State Pattern, so now content could be either New, Draft, JustPublished, PendingPublication or Withdrawn.
Which was okay so far as it went, but it didn’t play that nicely with the new feedback spam protection. So the Typo head has a new set of states for feedback – Unclassified, PresumedHam, PresumedSpam, Ham and Spam – and the feedback administration pages are starting to reflect it. My current goal is to rejig the feedback admin page to take advantage of these changes and stop it overwhelming the administrator with information.
