Snippets
has_many_polymorphs and Rails 3
May 11th
I’m gradually porting a number of my older Rails apps over to Rails 3. The main motivation is a chance to really put the new version through its paces, get a better sense of how it’s working, where plugins are at, etc; but it’s also rather nice to get some of the performance improvements and cleaner code along the way.
Catapult relies on Evan Weaver’s has_many_polymorphs plugin quite extensively so it was important to be have a Rails 3 compatible version. I couldn’t find any evidence that anyone else was working on it, so I’ve forked the github project and made a few alterations. I’ve set it up to work as a gem (so I can pull in the latest version using bundler) and adjusted to fit the new rails initialization process. It’s rather hacky, but it’s working for me so far.
Evan informs me that he has no plans to work on compatibility, so I’m going to see what time I can find to tidy it up a bit more, make sure the tests are passing, etc. If anyone else is so inclined, I’d love some help with that. You can find my fork on github.
Faster Rails development with bundler and rvm
Apr 18th
If you’re anything like me, you’ve found the rails server and console taking longer and longer to launch lately. Even switching to Rails 3 for most active projects hasn’t really helped. But I finally found a solution a couple of weeks ago.
I found it reading Mikel Lindsaar’s Bundle Me Some Sanity where he outlines the way that bundler coupled with rvm allows a rather different and much cleaner way to manage gems in a ruby application.
I’d been aware of rvm as a way to cleanly manage different versions of ruby on a single machine, but had missed their (recent addition in 0.1.25 of) Named Gem Sets. With Named Gem Sets you can specify a version of ruby and a separate gem library for each app, reducing dependency resolution time considerably.
I already can’t imagine switching away from this.
Cucumber, wordpress and database_cleaner
Aug 7th
I’m up to my usual using-ruby-tools-to-test-other-environments tricks, using cucumber and my wordpress activerecord classes to do acceptance testing against a highly-customised wordpress install.
I’m hoping to write a bit more about that soon, once I’ve put it through its paces a little more and cleaned up some of the code, but I wanted to quickly mention one of the key pain-points and an extremely handy solution.
One of the things I enjoy most about testing in rails is the handy tasks to prepare a blank database and the transactions that ensure the database is returned to that state after each test. Obviously that wasn’t going to work cleanly with wordpress since it doesn’t use ActiveRecord, but Matt kindly pointed me in the direction of Ben Mabey’s database_cleaner gem.
With that installed it’s as simple as adding:
require 'database_cleaner' require 'database_cleaner/cucumber' DatabaseCleaner.strategy = :truncation, {:except => %w[wp_options]}
to my
features/support/env.rb
file and my database is reset on each step. Combined with a few hooks to manage the configuration and load in the schema at the appropriate point, it’s turning into quite a nice little testing environment.
WordPress and “Pathless Categories”
Apr 29th
I’m working on a wordpress project at the moment, and pushing that blogging engine quite a bit further than I have before. We’re going to be using categories very extensively and one of the first tasks has been to allow category paths without any preceding /category/ or the like. By default, wordpress wants the category with a slug of ‘case-studies’ to live at:
/category/case-studies
but we want it to be simply:
/case-studies
So far, so straightforward. I installed the pathless-category-links plugin and all was well. Until I started using subcategories. All subcategory links started returning 404s. It seems I’m far from alone in that problem, but I’ve not yet seen a solution offered, so a little digging was required.
What was happening with subcategories (at least in my setup) was that the wordpress method that parses the query string was identifying the top-level category name as the category name and the subcategory name as the ‘name’ (ie. the post slug to look for). What was needed was a check to see whether that ‘name’ maps to a category slug and if so, correct wordpress’ assumption.
The code I’m using is:
if (! function_exists('jys_pathless_category_links_query_string')) { function jys_pathless_category_links_query_string($qs) { parse_str($qs, $query_vars); if (isset($query_vars['name']) && get_category_by_slug($query_vars['name'])) { $res = array('category_name' => $query_vars['category_name'] . "/" . $query_vars['name']); return http_build_query($res); } return $qs; } } add_filter('query_string', 'jys_pathless_category_links_query_string');
(NB: This will break if you have permalinks set up as recommended by the plugin author (‘/%category%/%postname%’) and a post in your top-level category with the same slug as your subcategory. But hopefully that’s rare enough that we’ll be okay!)
Hacking wordpress to support per-post banner images
Feb 23rd
I seem to be spending a lot of time with wordpress at the moment. It’s become so ubiquitous that it often makes far more sense to set it up and integrate with an existing app than to set up some other blogging system and re-train users. As a result I’ve been writing a few wordpress plugins. Most of them are too specialised to be worth sharing, but one seemed worth opening up…
Implementing a (not quite public yet) design recently I had need of a way to specify a banner image for each post. While wp has pretty good support for adding various media into the body of posts, this needed to sit outside the post body.
I whipped together a quick plugin to handle uploading a banner and storing its details in the metadata for the post. It was a simple process, nicely self-contained, except that the post edit form doesn’t have the appropriate enctype=”multipart/form-data”. I looked around for any hooks that would allow me to cleanly add attributes to the form tag, but in the end resorted to editing wp-admin/edit-form-advanced.php to add it.
I’d hoped that there’d be time to find a cleaner way to do all this before telling people about it—perhaps some javascript that hooks into the existing media selector but allows it to populate a custom data field?—but it hasn’t, so I’m throwing it out there to the wider world as-is. The code is at github. Feel free to take it and use it as-is, to fork it and update it to be a better wordpress citizen, to email me patches to apply to my copy, or even to employ me to spend more time cleaning it up! Either way, it deserves to be out in the open and hopefully it’ll be of us to somebody besides me.