vizres renders the body of an HTTP response from inside a Rails functional test. It used to be a plugin, but now it’s a gem. Why? Because Rails gems are the new hotness. Now go get it.
vizres renders the body of an HTTP response from inside a Rails functional test. It used to be a plugin, but now it’s a gem. Why? Because Rails gems are the new hotness. Now go get it.
I’m at the Ruby Hoedown in Huntsville this weekend. Being around so many brilliant geeks encouraged me to release a gem I’ve had sitting in the hopper for several weeks.
finder_filter encapsulates a pattern I find myself using quite frequently in Rails. Namely, looking up an instance variable before an action. For example:
class UsersController < ActionController::Base before_filter :find_user, :only => [:show, :edit] def show # do something with @user end def edit # do something with @user end def find_user @user = User.find(params[:id) end end
Sticking the finder in a before filter keeps the code DRY, but it still takes several lines to do this. finder_filter reduces this to a single line of code:
class UsersController < ActionController::Base finder_filter :only => [:show, :edit] def show; end def edit; end end
There are other options to customize the column and param used in the lookup. Check out the README for full details.
To install the gem on your system:
gem sources -a http://gems.github.com sudo gem install pelargir-finder_filter
Then open environment.rb in your Rails app and add the gem as a dependency in your initializer block:
Rails::Initializer.run do |config| config.gem "pelargir-finder_filter" ... end
If you have any comments or suggestions, I’d love to hear from you. Contact me through the finder_filter project on GitHub.
BarCampRDU 2008 came and went. It was quite enjoyable. There wasn’t as much grub as last year, but I thought the topics were more interesting.
I gave a talk on pair programming during the afternoon. It really became more of a group discussion, which was exactly what I was hoping for. Some attendees have requested the slides so I’ve attached them to this post as a PDF.
The slides have been edited somewhat. I gave this same presentation at Agile ITX last month and it was an hour and a half long. I had to cut out a few things for BarCamp. But the central ideas are still there.
Want to pick a certain set of key/value pairs from a Ruby hash? You might do this:
hash = {:foo => "bar", :bar => "baz", :baz => "boo"}
hash.select { |k,v| [:foo, :bar].include?(k) }
# returns [[:foo, "bar"], [:bar, "baz"]]
Kind of messy. We can do better by reopening the Hash class this way:
class Hash
def pick(*values)
select { |k,v| values.include?(k) }
end
end
Now our selection works like this:
hash = {:foo => "bar", :bar => "baz", :baz => "boo"}
hash.pick(:foo, :bar)
# returns [[:foo, "bar"], [:bar, "baz"]]
Ruby is a wonderful language. This is one small example of how having access to existing classes can be incredibly powerful. With this power comes great responsibility. Wield your power wisely.
I’ll be speaking at the Agile ITX conference in Reston, Virginia on June 27th. My presentation is titled Pragmatic Pair Programming and is based on the many diverse pairing experiences I’ve had over the past six years. Mention pair programming in any crowd of programmers and you’ll get two responses: adoration or outright hatred. Why is pairing so controversial? Does it have any tangible benefits? That’s what we’ll be exploring together. Agile ITX is shaping up to be a great conference with many top-notch presentations. I’m excited about getting the chance to participate. Hope to see you there.
I’m a freelance software developer which means I generally work from home unless a client needs me to be on-site. I don’t mind being alone to a certain extent, but after a few straight weeks it can get pretty lonely.
Recently, I’ve started doing what I’ve termed “nomadic programming.” Namely, spending the day roaming between various wi-fi hotspots instead of working from home. This has worked really well for me. So well, in fact, that I think the concept needs to start spreading.
Now I realize there are many freelancers out there who already do something similar to what I’m describing. I think it’s worth “formalizing” the process, though, by laying out the pros and cons of nomadic programming, and then giving some advice on how to actually go about doing it.
nomad [noh-mad]: (1) a member of a people or tribe that has no permanent abode but moves about from place to place, usually seasonally and often following a traditional route or circuit according to the state of the pasturage or food supply. (2) any wanderer; itinerant.
Why be a nomad? Why not stay at home? While staying at home has its benefits, here are some reasons why a nomadic lifestyle might be a better fit for you:
These are just a few of the many positive aspects of nomadic programming. What about the downsides, though? It can’t all be a stroll through the flower bed, can it? No, it can’t. There are some bees waiting to sting us:
While the downsides to being a nomad are real, they can definitely be managed. Wi-fi is getting fairly common, even in more rural areas, so travel time can often be limited. Expenses can be kept down by ordering cheaper items and putting in an extra hour of billable work to make up for what you’ve spent. Distraction is a harder nut to crack, but it can definitely be overcome with a dose of self discipline and some verbal accountability to your family and friends.
When balanced against the advantages, it’s clear that nomadic programming is a good thing overall. So once we decide the benefits are worth it, how do we actually go about being a nomad? There are a few simple, easy guidelines to follow and several tools that can make the process easier and more fun. We’ll find out what to do (and just as importantly, what not to do) in my next post.
In the meantime, drop a comment and let us know if you’re doing nomadic programming.
After spending nearly an hour tracking down where a particular TextMate bundle was coming from, I have surmised that there are no less than four places on my Mac’s hard drive where bundles may be lurking. In no particular order, they are:
~/Library/Application Support/TextMate/Pristine Copy/Bundles ~/Library/Application Support/TextMate/Bundles /Applications/TextMate.app/Contents/SharedSupport/Bundles /Library/Application Support/TextMate/Bundles
I just ran into a problem configuring a Rails app to deliver email through Google’s SMTP servers. ActionMailer doesn’t support TLS/SSL which is required by Google. Fortunately, the action_mailer_optional_tls plugin provides this functionality.
I wanted to host my SMTP settings in an external YAML file so I wouldn’t end up checking my username and password into the repository. (The YAML file is placed on the production server and a softlink is created during each deploy.) For some reason, I kept getting “connection refused” messages whenever I tried to send email with this configuration:
# smtp.yml
production:
address: smtp.gmail.com
port: 587
tls: true
domain: foo.com
authentication: :plain
user_name: someone@foo.com
password: secret
# production.rb
smtp = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml"))
ActionMailer::Base.smtp_settings = smtp[Rails.env]
The problem was the hash returned by YAML. The keys were strings, whereas ActionMailer was expecting the keys to be symbols. The fix was to make the hash use indifferent access:
... ActionMailer::Base.smtp_settings = smtp[Rails.env].with_indifferent_access
That cleared up the “connection refused” problem. Now my app is sending email like a champ.
railspdf is a plugin that adds templating support for the pdf-writer gem to Rails. The project was last updated in January of 2007, but worked fine up until the 2.1.0 release of Rails which broke several things.
I’ve forked the project on GitHub and tweaked it to run under 2.1.0. I also added support for custom helpers. Your PDF templates can now call methods in helpers with the same name as your controller (which is the expected behavior anyway). I’ve also started maintaining a CHANGELOG.
OS X seems to get confused when I disconnect my MacBook from an external monitor. This confusion sometimes leads to the hot corners feature not working. This is definitely a bug in Leopard that needs fixing. Until that happens, restarting the Dock is a quick solution to the problem. This can be done through the Activity Monitor, but I think it’s easier to just run this script which I stole and modified:
#!/bin/sh
DOCKPID=`ps aucx| grep Dock | awk '{ print $2; }'`
kill $DOCKPID