Posts Tagged ‘Rails’

Testing Your Apps.

Saturday, August 23rd, 2008

I’ve been really putting off using Rspec. Unfortunately ( and like the vast majority or programmers out there) I have not been writing enough tests for my apps. When I did write them, they were using Rail’s built in Test::Unit library, which, many will say… sucks. Plus, honestly, they were half assed.

So, I’ve taken it upon myself to dive into Rspec. Most everyone agrees it is the way to go, and it gets you into BDD, which gives me another bussword I can throw on the resume (/sarcasm).

The part about writing tons and tons of tests to cover all your app that didn’t make sense was that you would end up writing so much more code. In reality, I think it boiled down to being lazy, which all programmers are. Since the Rails community prides itself on keeping things DRY and keeping the amount of code written to a minimum, why the hell would I want to write tons of tests for stuff I don’t think are going to break?

In the long run, for any programmer, it is really not about how much code you actually have to write, its more about how much time you send making the app work. If you’re like me, there have been many nights spent debugging some controller that totally broke and you’re stuck refreshing your browser, creating a user, deleting a user, and checking the log over and over until you solve it. I hate it and have probably wasted days doing this. If you had written tests for your app, starting with that first controller, and continued writing quality tests, used autotest, you would avoided this. You would be confident that you code was rock solid.

What about if you’re absolutely sure you’re code won’t break? Well, it will. Maybe not all of it, but you’re gonna have problems eventually. I don’t think that writing tests for trivia stuff (like validating that validates_presence_of is working) is really worth while, but other stuff is fair game.

The list point I’d like to make is that I think since really writing tests and using Rspec, my thinking has changed. I know think writing code, not just as how do I make this work, but also, how to I ensure this WILL work. You’ll find yourself trying look for ways that your code will break, looking for more flaws, and consequently, leaning from those flaws. I’ve found myself refactoring much more code and that end up making my code more flexible and cleaner.

I don’t want to pretend to be a captain awesome when it comes to writing tests yet, but I really think I’ve been coming along very well and am so glad I’ve been investing so much time in it. Give it a try ya’ll.

Mongrel + Nginx: Deploying to a subdirectory

Tuesday, May 27th, 2008

Though using subdomains is all the rage right now, there are certianly instances where you may want to deploy your rails application to a subdirectory such as:

http://www.johnyerhot.com/myrailsapp

Of course Nginx makes it super easy to do so. If you need to get your webserver ready with Nginx, PHP running as a FCGI instance, and Rails check out my other how to.  

Now, onward!

First create a new virtual host.  In my case, for yerhot.org it would look like this:
server {
listen 80;
server_name yerhot.org;
access_log /var/www/yerhot.org/logs/access.log;
error_log /var/www/yerhot.org/logs/error.log;
location / {
root /var/www/yerhot.org/;
index index.html;
}
}

Pretty simple setup, telling Nginx to listen on port 80 for requests for yerhot.org, where to store logs, and finally setting up the site root at /var/www/yerhot.org.

All we would have to do to have Nginx redirect to our Rails app when looking for yerhot.org/myrailsapp is make another location block (in other words, place this right before the last curly brace).

location /myrailsapp {
proxy_pass http://localhost:8000;
}

Now, all requests for /myrailsapp will get proxied to port 8000. Now fire up your Rails app on port 8000.
mongrel_rails start -e production -p 8000 -d
Restart Nginx:
/etc/init.d/nginx stop
/etc/init.d/nginx start

And….

Crap. Rails is looking for a ‘myrailsapp’ route, which there is none.  No, no, don’t create one – we’ll need to use a little known feature of Mongrel to fix the problem.. the prefix.
Stop Mongrel…
mongrel_rails stop
And try this:
mongrel_rails start -e production -p 8000 -d --prefix=/myrailsapp

And… your app should fire right up. Pretty neat, if you wanted to you could use it forward to a Mongrel Cluster, a FCGI instance of PHP (from my other post), or lots of stuff.

Integrating Google Maps in Your Rails 2.0 App.

Saturday, January 5th, 2008

NOTE:  THIS IS AN OLD POST AND THE INSTRUCTIONS MAY BE OUTDATED. YMMV

In my first real post in a year, I thought I’d share some Ruby stuff I’ve recently done – Integrating your Rails 2.0 app with Google Maps.

So, here is what we are aiming to accomplish: You are going to provide an address, say 123 Foobar St, Anywhere, MN 55812. We are going to send it to a geocoder, get the longitude and latitude, send that to Google, and get back the map information.

whew. Actually, its not nearly as hard as it sounds.

First, install the google-geocode gem:

sudo gem install google-geocode

The install the ym4r_gm, which will talk to Google for us.

script/plugin install svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm

Alright, finally, include the google-geocode gem in your controller.

require 'rubygems'
require 'google_geocode'

Now, you will need to get an API key from Google, and then put that key in the newely created config/gmaps_api_key.yml (as Drew Bushaw points out) or hard code it (Which I do in the example). There should be three keys already in there, and they will work when using your local machine, but will not with anything other than localhost. Alternativly, if you want to hardcode it in, place it where I have “your api key here” in the next section.

Add to your page header (prolly your layout and view) to include the required API javascripts, etc…
<%= GMap.header %>
<%= @map.to_html %>

Ok, now heres the fun. To get the geocode information, simply:

gg = GoogleGeocode.new "your api key here" #hard coded
#not hardcoded
#gg = GoogleGeocode.new YAML.load_file(RAILS_ROOT +‘/config/gmaps_api_key.yml’)[ENV['RAILS_ENV']]

loc = gg.locate @property.full_address
Now, I have @property and full_address is its full address (123 Foobar, Somecity, MN 55812 for example). You can also try it without some vitals, such as zip code.

Now we have to send Google Maps the correct information:

@map = GMap.new("map_div")
@map.control_init(:small => true) #add :large_map => true to get zoom controls
@map.center_zoom_init([loc.latitude, loc.longitude],14)
@map.overlay_init(GMarker.new([loc.latitude, loc.longitude],:title => @property.name, :info_bubble => loc.address))

Notice, “map_div”, which will come into play in the next step. Some useful tid bits here include the :title, which will be the title of the pin on the map, :info_bubble which will appear when you hover over the pin, and where I specified “14″. Here you specify the altitude the map will be at. Experiment for whatever your needs are.

Now, the final thing you need to do is put this to work in your view.

<%= @map.div(:width => 493, :height => 300) %>

As you can see, I’ve specified a :width and :height. Now, fire everything up and you should get something similar to…

screen_grab_google_maps_1_5_07

Now in my case I had people inputing address and if/when they enter one that is non-existent or incorrect, I had to catch it since a google-geocode will throw a big error if you give it bad information.

Enjoy!