This seems like a very good idea.
Month: October 2007
-
Where is the logic in this?
There are few things I hate worse then trying to call a business, getting put on hold, and then having to listen to ads for that business while I’m on hold. A happy customer I am not.
-
Use helpers in your Rails PDF templates
The Rails PDF plugin is a dandy little thing. It wraps the PDF::Writer library for Ruby, allowing PDF templates to be defined like any other Rails view (with a .rpdf extension, of course).
One thing it doesn’t do is allow access to the parent controller’s helper from within a PDF template. It’s easy enough to patch the plugin to accomplish this, though.
First, open up
vendor/plugins/railspdf/lib/railspdf.rband take a gander. This is the section we’re interested in:... class PDFRender < ActionView::Base def initialize(action_view) @action_view = action_view end ...PDF templates get rendered in the context of this class. All we need to do to gain access to the controller's helper here is include the helper from inside the class initializer, like so:
... class PDFRender < ActionView::Base def initialize(action_view) @action_view = action_view prefix = action_view.controller.class.to_s.gsub(/Controller/, '') self.class.send(:include, "#{prefix}Helper".constantize) end ...We first calculate the prefix of the helper based on the class of the controller assigned to the view. Then we include the helper in the current class by constantizing the resulting string.
We're almost done. Since ApplicationHelper should be available to every controller and helper in the system, let's include it here for good measure:
... class PDFRender < ActionView::Base include ApplicationHelper def initialize(action_view) @action_view = action_view prefix = action_view.controller.class.to_s.gsub(/Controller/, '') self.class.send(:include, "#{prefix}Helper".constantize) end ...And we're done! Piece of cake. Now we can reference methods from inside our controller's helper. We can, of course, modify this hack somewhat so that the same helper (something like "PdfHelper" maybe) is included for all of our templates. This enables us to isolate our PDF-specific helper methods in a single module.
And before you ask, yes, it's nasty to be modifying the plugin directly. There is a way to inject this patch into the PDFRender class without touching anything in the plugin... but I've gotta leave something for you to figure out! :)
-
Hey, flexmock and unit_record, play nice!
So you’re savvy with flexmock, a fine Rails plugin that lets you create mock objects in your tests. You’ve been coding up a boatload of fine tests that are elegant in their isolation thanks to your super mocks. They all run fine, but they’re kind of slow so you go Googling for techniques to speed them up.
You stumble across a wonderful plugin called unit_record that can dramatically speed up your unit tests by disconnecting them from the database. You install it, run your tests, and wham, you get a barrage of error messages like this:
ActiveRecord is disconnected; database access is unavailable in unit tests.
There could be one or two things going on here. The first possibility is that one or more of your unit tests are still attempting to access the database via a finder or a fixture. This is the most likely case if you’re only getting a few errors. If you’re getting an error for every single unit test in your suite, though, then something else is going on: flexmock is not playing nicely with unit_record.
The root of the problem is that unit_record attempts to turn off transactional fixtures for your unit tests when you run them, but flexmock hijacks unit_record by defining its own alias chains for setup/teardown.
I got around this problem by updating my unit_test_helper.rb file to include the following:
class Test::Unit::TestCase self.use_transactional_fixtures = false end
It’s unfortunate this has to be done, but it’s a better option than digging into the flexmock plugin itself… especially if you plan on upgrading to a new version of flexmock anytime soon. Don’t forget that you should be disconnecting ActiveRecord in your unit_test_helper.rb as well. This is what my entire file looks like:
require File.dirname(__FILE__) + '/../test_helper' require 'unit_record' ActiveRecord::Base.disconnect! class Test::Unit::TestCase # Unit_record is trying to do this for us, but since we're # using flexmock's test case it patches the wrong class. self.use_transactional_fixtures = false end
Hope this helps someone who was as equally stumped as I was. Happy testing!
Update on 12/1/07: After chatting with Jim Weirich (the creator of flexmock) and doing some more tweaking, it became obvious that the problem is not with flexmock but lies somewhere else. I removed all references to flexmock from my code and still had trouble. The transactional fixture switch seems to be getting set between test runs, but I can’t locate where it’s happening. If anyone else finds a solution to this problem, please let us know by posting a comment here.
-
Streamlined turns 0.9
The latest and greatest version of Streamlined was released a few weeks ago. Lots of new features and bug fixes are included in this release, including a handy (and fully configurable) breadcrumb trail implemented in part by yours truly.
For those who aren’t familiar with it, Streamlined is sort of like Rails scaffolding on steroids. It lets you quickly and declaratively setup an interface for your ActiveRecord models.
I continue to be amazed by the amount of code I don’t have to write when I’m using Streamlined. In fact, it’s starting to warp me. Whenever I work on a standard Rails project now, I inevitably begin asking myself why I’m having to write all of these actions to do basic CRUD stuff. Streamlined provides it for free.
Streamlined is quickly approaching a 1.0 release (hopefully this month) so there has never been a better time to jump onboard. One good way to get rolling is with this post that I made a few months ago.
-
$100 laptop debuts this November
Watch the inimitable David Pogue for a nice demo. OLPC’s $100 laptop debuts in the U.S. during a two-week window in November. It’s a buy one, get one deal. $400 sends one laptop to a poor kid in a third world country, and one laptop to you.
Am I getting in on this deal? You betcha. Consider the hackability factor here. Pogue already snuck in a reference to Python in his video. Imagine the cool things that can be done with this device once Ruby is running on it. Not to mention the fact that it’s pretty indestructible.
An extra $200 to send one to a needy child? What a deal. Count me in. What about you?
-
Quote of the Week: J. Boyd Nicholson
“The gospel is not a tranquilizer for worried weaklings to help them sleep at night.
“It is not a mass of dead dogmas, deep frozen in some ancient cathedral to be carried as a burden through life and thawed out five minutes before death.
“The gospel is not a list of religious rules and regulations to be strung around the soul like a lucky charm in case of accidents.
“No, the gospel of our Lord Jesus Christ is a message — and what a message! It is a living message from the living God for living people, just like us, for people with sins just like us, for people with sorrows and heartaches just like us.
“It is the only message on the face of the earth with concrete promises and absolute assurances of an eternal inheritance that will withstand the impact of death and the collapse of the universe.”
— J. Boyd Nicholson