Generating URLs with ActionMailer
ActionMailer is a very handy class for sending emails from your rails application. However, the problem that typically arises is the sending of links to your site via rails using the url_for method. Acording to the API documentation, this is because the mailer instance does not have any context about the incoming request. The API documentation recommends passing the host to the url_for parameter, like this:
url_for(:host => "www.myproductionsite.com", :controller => :home, :action => :index)
However, when using multiple url_for calls in a model, this can lead to a violation of DRY. Therefore, the typical resolution to this has been to add some declarations to your class, like this:
class Notification < ActionMailer::Base default_url_options[:host] = "www.myproductionsite.com" end
I dont really care for this approach, as it locks my model into operating under the assumptions of a particular environment. I am unable to verify the links in my development or test environments without some string manipulation. A little effort led me to this approach, which so far seems to be much better. Instead of declaring the default_url_options in the ActionMailer model, I’m including them in config/environments/*.rb in this way.
# development.rb and test.rb class ActionMailer::Base default_url_options[:host] = "localhost" default_url_options[:port] = 3000 end # production.rb class ActionMailer::Base default_url_options[:host] = "www.myproductionsite.com" end
I am taking advantage of the environments file to give me the benefits of custom url options based on the environment that I am currently executing in. This is precisely what these files were designed for.
Lazy Web Request: Google Reader and comments
I have been using Google Reader for quite some time now and thoroughly enjoy the feed synchronization that comes from this hosted solution. Additionally, I very much enjoy being able to zip through my feeds. I find myself trying to j and k’ing through other lists of data as well. Unfortunately, it doesnt always work. But, I digress…
The real point of this post is to ask how other people keep track of the comments that they have posted to articles. Currently, what I’m doing is starring the article, and then I go back through to locate the ones I’ve commented on to see if there’s further discussion. This seems *much* too manual, and surely there is a better way to do this.
Here’s where I solicit your help, dear reader… Let me know in the comments how you keep track of articles that you’ve posted comments to and how you keep the conversation going. Thanks in advance for your ideas.
Blasting open source
In the latest installment of the Elegant Code Cast, David Starr talks with Darrel Carver, a local .NET developer and member of the Elegant Code group. I found the talk quite fascinating, but do want to pick on Darrel for one of his comments.
Repeatedly through the podcast, Darrel mentioned that open source is guilty of continuing the re-invention of software, simply because someone may not like the project owner, or that just one additional feature is needed. After all, as Darrel puts it, why do we need 10 IOC containers? While this may certainly be true, I do not think that it was fair for Darrel to state that this was purely a trait of open source.
Microsoft itself is guilty of continuing and enabling this behavior. Rather than reach to the open source community and leverage the great tools that are already there, Microsoft insists on rebranding tools that have been created in the open source space (NAnt/MSBuild, MonoRail/System.Web.MVC, NUnit/MSUnit, NDoc/Sandcastle, etc). They do this because it is far easier to have enterprises adopt a toolset when it is supported by a company like Microsoft. What I fail to see is why enterprises that use Java do not have the same problems. I also fail to see how the open source community can be faulted without mention of the identical behavior from the platform vendor. After all, had we been blindly following the tools given to us by a vendor, many of us would have never even been exposed to IOC, xUnit or many of the other principles that ALT.NET developers hold dear.
The primary difference is that the majority of different open source projects add their own value in ways. Take for example StructureMap vs Castle Windsor. The essence of both is the same; they are both IOC containers. StructureMap has been around a bit longer, but Castle Windsor has many more features, including interceptors, facilities and such. These diverse feature sets are where the value of multiple projects comes to bear. However, simply re-branding the best of breed projects does nothing to add value.
It is these re-branded projects that make Darrel’s point: many projects that do not add value do much to clutter up the developers toolbox and should be eliminated.
Ruby strings and is_int?
Lately, I’ve found myself needing to do some parameter checking to ensure that appropriate data makes it back to my models when doing finds. Using the will_paginate plugin allows me to very easily do paging on my model data. I simply have to provide the page to the options hash.
class Model < ActionRecord::Base def self.search(parameter, page) paginate :per_page => 10, :page => page, :conditions => ['model.parameter = ?', parameter] end end
The problem here is that some people are trying to pass in URLs as the page parameter, causing exceptions to be raised. I’ve installed Jamis Buck’s ExceptionNotifier plugin and this is informing me via email of every exception encountered in the application. On a side note, I cannot recommend this plugin enough if you want to see where your application is being used incorrectly. I’ve found numerous edge cases with this plugin and urge every Rails installation to take advantage of it.
I wrote a spec for what I was after that looked like this:
describe Model do it "should convert the page number to one if an integer is not passed" do Model.should_receive(:paginate).with(hash_including(:page => "1")) Model.search("parameter", "some_invalid_page_number") end end
Of course this test failed, so I set out to make it pass. What I really needed in this case was a way to reset the page parameter to an integer value if it was not an integer. I scoured the Ruby and Rails documentation but did not find anything built in to the framework.
Having seemingly no other option but to monkey-patch the string class, I ended up with this:
class String def is_int? self =~ /^[-+]?[0-9]*$/ end end
and the first line of the search method in my model looks like this:
page = "1" if not page.is_int?
My specification passed, but surely there’s a better way. I really do not want to monkey patch classes if I dont need to.
Investigating RSpactor
RSpactor is a command line tool for OSX Leopard that does basically the same thing as autotest, which is to re-run your specs any time one of your application files changes.
The reason that it only works on Leopard is the same thing that makes it better than autotest at this point. It uses the filesystem events (FSEvent on Leopard) exposed by RubyCocoa. This makes it much, much faster than using the file notification system used by autotest. It’s saved my MBP fan from spinning up when I go through a development session. Additionally, Growl support is built in so you have very nice notifications available for when your tests pass or fail.
Since I rebuilt the version of Ruby that was included with Leopard, I ended up having to manually install the RubyCocoa libraries. Thankfully though, this was relatively straightforward as well. If you’re running a stock Leopard install, you wont need to do anything other than:
superbia:~ matt$ sudo gem install rspactor superbia:~ matt$ cd /path/to/your/app && rspactor
If you’re using autotest currently and are on OSX Leopard, do yourself a favor and check this tool out.
Has it really been five years?
Five years ago today, I launched this blog as a way to work on my writing skills as well as give back a little to the community that I’ve learned so much from. It’s been a very fun and interesting ride. I’ve much enjoyed being able to provide technical information to my audience.
A recent review of my Google Analytics account showed the following posts as the most popular on this site. Interestingly enough, the single most popular post on this site has no technical nature whatsoever. The rest I’ve noticed are very tactical in nature and are probably a result of having almost 75% of my traffic driven by search engines.
- Escape from the Crimson Room
- Submitting ASP.NET form with the ENTER key
- Date validation in ASP.NET
- Replacing notepad in Windows Vista
- Serializing an IDictionary object
- Windows Vista and the screen saver
- Executing a SQL script using ADO.NET
Over the past five years, I’ve created 757 posts that have generated 1378 comments. I’ve had visits from 183 countries/territories across the globe. This has far surpassed anything that I originally thought would happen with this site. After all, who would want to listen to a geek from Boise, Idaho? My goal for this site for the next five years is to continue to provide technical, how-to information. I’d also like to increase the social aspects of the site and look forward to posting things that cause conversations to occur on this site as well as others.
Here’s to five more years. Thank you to all of my readers. You mean more to me than I can ever express. Keep reading and I will keep writing.
A while back I posted that I didn’t really get Twitter. However, recently, my synapses started firing around this concept. I’m just starting to look at it as a very intriguing public form of instant messaging.
Im mattberther on twitter.com. Drop by and say hello.
Is there anything you need help with?
A daily scrum meeting involves asking each participant of the scrum team three questions:
- What did you do yesterday?
- What will you do today?
- Is there anything in your way?
I’ve found that very rarely do people offer anything to the “is there anything in your way” question. After all, we are all engineers and there should be nothing in our way that we can not solve.
I’ve started asking this question slightly differently. The question I ask now is: “Is there anything you need help with?” I’ve noticed that this question tends to spawn a discussion among the team members much more than the previous question. I am not quite sure why this is, but so far it seems to be working.
How I use Leopard Spaces
OSX 10.5 (Leopard) introduced one feature that has fundamentally enhanced my productivity more than any other tool or feature before. Spaces, for those unfamiliar, are virtual desktops for your computer. The key problem with the initial implementation of Spaces was that the desktops were designed to be centered on the application, rather than on tasks, which is my preference.
With the application-centric focus, I am only able to have one instance of the application open and when I click on the icon in the dock, I’m instantly transported to the space containing that application. However, the way I’ve decided to utilize Spaces (with a task-centric focus), I have a need to have Safari or TextMate open on many different windows.
In Leopard 10.5.2, the doc has some preferences that you can set on the commandline that will not switch spaces when you hit CMD-TAB. Thanks to Mac OSX Hints for the tip.
superbia:~ mattb$ defaults write com.apple.dock workspaces-auto-swoosh -bool NO superbia:~ mattb$ killall Dock
So, my space layout? I have 2 rows of 3 spaces. Email occupies space 1. Feed reading is occupied by space 2. IM and Twitter activity goes on in space 3. Space 4 is my Parallels VM (with 4gb of RAM on my MBP, I can afford to run a Windows VM all the time). Space 5 is occupied with my development tools. Space 6 is open for other things.
I’ve mapped my hot corners with Expose as well. Top right shows my spaces, top left shows windows on the space. Bottom left shows my desktop and bottom right shows the screen saver (or locks the computer).
I really enjoy the Spaces feature and highly recommend some variation of this for people to become increasingly productive with OSX.
Rails applications and 500 errors
The default scaffolding generated for a model has the capability of throwing a number of exceptions, resulting in a 500 level response status being sent to the client. The largest cause of these exceptions is the default behavior of ActiveRecord::Base.find. If find can’t locate a record that matches the user-supplied identified, then the method throws an ActiveRecord::RecordException. Take a look at the default code generated for a controller utilizing a User model.
def show @user = User.find(params[:id]) end
Fairly straightforward, and without looking at any edge cases, it’s very clear, clean and concise code. As you probably expected, Rails created something that lets you easily work around this. The dynamic finder methods do not throw exceptions, rather they return nil if a record is not found. If you haven’t learned about the dynamic finder methods, you should really learn more about them. By rewriting the generated scaffold code slightly, we are able to present a friendly message to the end user letting them know that a record was not found.
def show @user = User.find_by_id(params[:id]) render(:file => 'public/404.html', :layout => true, :status => 404) and return unless @user end
As you can see, the render method has the capability of setting the layout, so you can have a nice consistent look and feel across your 404 page, as well as the status code which signifies to the browser that the resource was not found.
This new code is validated by these rspec tests:
describe "Given a request to show an invalid user id" do setup do User.stub!(:find_by_id) get :show, :id => "invalid" end it "should not assign a user" assigns[:user].should == nil end it "should return a status of 404" do response.headers["Status"].should == "404 Not Found" end it "should render a 404 file" do response.should render_template("public/404.html") end end
This little technique will certainly help keep the size of the production log file down as well as present a nice interface to your users should a request for an invalid record id come in.


