This site provides the following access keys:

Brandan Lennox's

Articles : Page 2

Rails, Sprockets, and Resource Controller

I had an annoying bug recently while using Sprockets on a couple of Rails projects. Every time I restarted my app server, I’d end up with an exception on the first page load:

ActionView::TemplateError (undefined local variable or method `sprockets_include_tag' for #<ActionView::Base:0x1035fa6a0>)...

But everything was fine after that first request until I restarted the server again, obviously making it a pain in the ass to debug. Eventually, I ended up creating a fresh Rails project with all my gem requirements and removing gems until the error disappeared.

It turned out to be Resource Controller, or more specifically, the way I was invoking Resource Controller. I chose to inherit from ResourceController::Base rather than simply calling resource_controller in my controllers, which meant that I was no longer inheriting from ActionController::Base, and that’s where Sprockets injects itself:

# vendor/sprockets-rails/init.rb
class ActionController::Base
  helper :sprockets
end

My solution was to create an initializer to include the Sprockets helper in ApplicationController, since ResourceController::Base subclasses it:

# config/initializers/sprockets.rb
ApplicationController.class_eval do
  helper :sprockets
end

And that was that!

Removing Previous Replies in Mail.app Messages

Among the numerous subtle improvements Apple included in Snow Leopard:

Selecting a large block of replies in a single click Single-click anywhere in a previous reply to highlight it…

Superfluous replies have been removed et voilĂ !

While you’re composing a reply, clicking a paragraph in a previous reply highlights it (and any replies nested below it) allowing you to remove the whole block with one more click. It does leave two lines you have to delete manually, but it’s still pretty handy for cleaning up long threads or removing part of a conversation.

Update: It’s been brought to my attention that this is not new in Snow Leopard, and that it requires rich text formatting in the original and in the reply to work. So it’s not quite as useful as it first seemed.

A TextMate Bundle for Perch

I’ve recently been working with Perch on a couple of PHP projects. It’s exactly what they claim: a really little content management system. It stays out of your way (mostly), letting you integrate bits of user-managed content only when you need them. You get to design your pages in the languages you’re used to rather than designing an entire theme around your CMS. Right nice for small projects.

If you’re sick of typing <?php perch_content('Dancing Bunny'); ?>, and you’re savvy enough to be using TextMate, check out my Perch bundle on GitHub. It’s essentially the same as Ad Taylor’s Perch bundle, except the snippets are a little smarter and there’s a grammar included.

See the docs on GitHub for installation instructions (you’ll need to change the HTML bundle’s grammar as well).

A Better Way to Manage Pages in WordPress

One of my major gripes with all CMSes is editing my page content in a textarea. It makes me appreciate working in a native text editor like TextMate: syntax highlighting, keyboard shortcuts, code completion, fast saves, undo, etc. Not to mention working without any version control. Managing page content through a CMS back end is crippling.

So it’s surprising that it took me this long to come up with a better method of managing static pages. All it took was a one-line change to my page template:

// page.php
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <section class="page-content clearfix">
    /* <?php the_content(); ?> */  // no more!
    <?php include "pages/{$post->post_name}.html.php"; ?>
  </section>
<?php endwhile; endif; ?>

Now all my static pages live under a single subdirectory (under version control) with full access to both PHP and WordPress capabilities, and I can edit them just like I edit any other PHP file. Quite a drastic improvement for a one-liner.

WordPress's 800-line Function

This is the PHP ethos:

WordPress's get_posts() function is nearly 800 lines long WordPress’s get_posts() function

The get_posts() function is 796 lines long. They just kept adding more and more conditions and filters and callbacks, and now it’s absolutely unmanageable. (I was trying to debug a faulty plugin and ended up in here dumping variables every few lines because it was impossible to tell who was changing what when.)

It reminds me of PHP in general, tacking on support for object-orientedness, closures, and other high-level abstractions to an initially simple procedural language that wasn’t ever meant to power something as complex as WordPress.

← Newer articles