Two Years Working with Rails 0

Posted by matt
on Sunday, October 12
This week marks the two year anniversary when I delivered my first professional Rails app. Today, I decided to take a look back at the code of that first project and see what's improved over the past two years. The result - not bad, but there were a couple areas that stood out. Here are a few:
  1. The controllers in that first project were out of control. Not sure why, maybe coming from the J2EE world, my first instinct was to cram everything into the controller. It was apparent that the concept of REST wasn't completely baked into my brain yet. Not to mention the concept of fat models, skinny controllers.
  2. Second, the views and Javascripts were a bit unorganized. I noticed excessive conditional logic and messy Javascript code in a few of the pages. If I was to re-write the app today, most of it could be cleanup with rendering partials with collections and using Low Pro to clean up the Javascript.
  3. Finally, the sheer lack of plugins for that first project was surprising. It's true that the amount and quality of plugins have grown in two years, but I believe the lack of awareness was the main cause.

So this made me think of what I would say if I were to advise newcomers writing their first Rails app. I would have to say first, if you find yourself writing code in the controller, then ask yourself "Can I put this logic in the model?". And also be sure to familiarize yourself with the available plugins with sites such as Agile Web Development and Github. They can save you a ton of work.

erubycon 2008 0

Posted by matt
on Sunday, August 24
Last week I made a short drive to Columbus for the erubycon conference presented by the EdgeCase crew. The three day conference, hosted by Microsoft, was an event to demonstrate Ruby's role in the enterprise. Having worked in the Enterprise for six years, I was very excited to learn more about how Ruby can change the Enterprise.
What is legacy code and how do we avoid it? Stuart Halloway addresses these questions with his talk Ending Legacy Code In Our Lifetime. This was my favorite talk on the first day because the Enterprise is "full of it" and much my time was spent trying to avoid it. It's nice to see somebody calling attention to it.
Very interesting talk on JRuby by it's creator Charles Nutter on the second day. I went back to the hotel room that evening and setup JRuby on my laptop. I wrote a quick rails app and with Warbler, had a working war file ready to be deployed to Java application server. Very impressive.
Perhaps my favorite session at the conference was Jim Weirich's talk on concurrent software development. Something I don't think is talked about enough and is certainly important in the Enterprise. I've written multi-threaded apps in Java and it can be very difficult especially avoiding deadlocks. Is Ruby the answer? According to Jim, not really. Perhaps a Erlang or Clojure offers a better solution.
And Finally, Chris Wanstrath, co-creator of GitHub talks about Git, GitHub, and a little about side projects. Oh, and did I mention I really love GitHub? It's really changing how we develop software.
* All the photos above are provided by EdgeCase, LLC and more are available on their Flickr page.

Quick git add, commit, push, and deploy

Posted by matt
on Monday, August 11
Last week, I was preparing a presentation and found myself doing a lot of quick fixes and deployments to prepare a web application for a demonstration. I thought instead of running the following four commands each time:
git add .
git commit -a -m 'A description of the change'
git push
cap production deploy
It would be nice if I could do all of the above with just one command. So I created this shell script:
push() {
	
	# Defaults
	MINLEN=25
	DIRTY=false
	DEPLOY="production deploy"
	REMOTE_REPO = "origin master"
	
	# Check if we have any untracked files
	if git status | grep -q "modified:"	
	then
		DIRTY=true
	fi
	
	# Make sure there is a message with the commit 
	if [ -z "$1" ] && (test $DIRTY == true)
	then
	  echo "You must specify a message with your commit"
	  return
	elif [ ${#1} -lt $MINLEN ] && (test $DIRTY == true)
	then
	  echo "Your message must have at least $MINLEN letters."
	  return
	fi
	
	# Commit all the changes by default
	if (test $DIRTY == true)
	then
		echo "Adding new files to Git repository"
		git add .
	
		echo "Commiting to local Git repository"
		git commit -a -m "$1"
		
		# Push changes if a remote repository exists
		if git remote | grep -q "origin"	
		then
			echo "Pushing changes to remote repository"
			git push $REMOTE_REPO
		fi
	fi
	
	# Deploy changes via Capistrano
	if ls | grep -q Capfile	
	then
		cap $DEPLOY
	fi
}
The 'push' function will first check to make sure you supplied a description if any recent changes were made. Second, it will commit all the code and push it to the remote repository (if one exists).

If none of the code was modified or added, it will skip the Git commands and simply run the Capistrano deploy command and not require a description for the changes.

To use this script, copy and paste the above function to the end of your ~/.bash_profile file. To run it, simply run the 'push' command.
push "The description for the committed changes."
That's it! All the code is added, commited, pushed, and deployed.

Relay outbound SMTP email to Gmail 5

Posted by matt
on Saturday, August 02
Sending emails with Rails via Gmail is a snap with Marc Chung's excellent plugin action_mailer_tls. However, sometimes our production environment isn't using Gmail as a mail server and/or we just need an easy way to send email from our development environment for testing or demonstrating purposes.

Instead of installing the action_mailer_tls plugin and configuring each of our Rails apps, we can do a one-time setup of our local Postfix client to relay all SMTP outbound emails to our Gmail account. If your running a Mac OS Leopard or Linux, Postfix should already be installed. With a little configuration, we should be up and running in a couple minutes.

First create /etc/postfix/relay_password file with the server name, email account name and password as shown below. This configuration works with Gmail accounts as well as with Google Apps email accounts. I'm personally using my company's Google Apps with a special email account setup for outbound emails only.
smtp.gmail.com	    example@yourdomain.com:yourpassword
Then tell Postfix about our google accounts information so it knows how and where to relay the email to. This can be done with the postmap command:
$ postmap /etc/postfix/relay_password
Since Gmail requires a TLS (Transport Layer Security) connection for certificate-based authentication, we'll need to download a free root certificate from Verisign https://www.verisign.com/support/roots.html to authenticate our remote SMTP client.
$ mkdir /etc/postfix/certs
$ cd /etc/postfix/certs
$ sudo cp roots.zip /etc/postfix/certs
$ sudo unzip -j roots.zip
$ sudo openssl x509 -inform der -in ThawtePremiumServerCA.cer -out  ThawtePremiumServerCA.pem
$ sudo c_rehash /etc/postfix/certs
Now we are ready to configure Postfix. Postfix needs to know what host to relay the email to, the username and password to authenticate the Gmail account, and the path to our certificates for the encrypted session. Add these lines to the bottom of /etc/postfix/main.cf
relayhost = smtp.gmail.com:587
# auth
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/relay_password
smtp_sasl_security_options = noanonymous
# tls
smtp_tls_security_level = may
smtp_tls_CApath = /etc/postfix/certs
smtp_tls_session_cache_database = btree:/etc/postfix/smtp_scache
smtp_tls_session_cache_timeout = 3600s
smtp_tls_loglevel = 1
tls_random_source = dev:/dev/urandom
Restart (or start) Postfix to pick up our new changes.
$ sudo postfix stop
$ sudo postfix start
That's it! Now we don't have to do any special installation or configuration to send email via Gmail for our Rails apps. We just need to set the delivery method to :smtp and we're ready to go.

Now running Phusion Passenger

Posted by matt
on Friday, July 25
Just finished making some major changes to this site. First thing you might noticed are the changes to the design. In effort to simplify things a bit, I modified a Scribbish theme to my own liking. Is it too geeky? The awesome portrait you see at the top right was created by Josh. Nailed it.

Here is a rundown of the changes made:
  • Upgraded to Ubuntu 8.04 Hardy Heron
  • Installed Apache and Phusion Passenger (mod_rails)
  • Change and tweaked Mephisto theme
I'm still in the process of moving my old blog posts over. More big news coming soon.

LinkedIn shares it’s communication architecture 0

Posted by matt
on Wednesday, June 04
I was just reading LinkedIn’s presentation on it’s architecture and it was like walking down memory lane. I worked with almost the same architecture at LexisNexis. We also used Java, Solaris, Tomcat (Websphere mostly), Oracle, JMS and the cached was implemented with C++. Ah, the good ol’ days.

Finally, MattSears.com

Posted by matt
on Friday, March 28
After a decade of waiting, I finally nabbed the mattsears.com domain name. I almost forgot about it until Godaddy sent me an email last week that said it was “Pending deletion”. Though, I have to admit that I’m not really excite about it as I thought I would be. I guess it’s because I’ve been using matthewsears.com for a couple years now and excepted using my full name instead, but it’s cool that I have both now.

SD West

Posted by matt
on Saturday, March 03
Really late on this, but here are some snapshots of the Software Development conference in Santa Clara, California. I had a really good time, except I was sick for a couple days. I concentrated mostly on design patterns, generics, C++, Java, and vomiting. :-)
I had the opportunity to attend a few good sessions with pros like Bjarne Stroustrup, Herb Sutter, Martin Fowler, Eric Evans, Stephen Dewhurst, Ken Pugh, Allen, Hollub, and Bruce Schneier to name a few. There really is no substitute from learning hands-on from experts like these. I left each session feeling like I just learned an entire year’s worth of knowledge. And of course, there were other things to do at the conference besides classes - Jolt awards, the expo, and Joel Spolsky was back to screen his documentary called Aardvarked.
This year, I made it point to get out of the hotel, convention center, and bar to explore Silicon Valley. I drove around town with no real plans except to search for better food. The hotel food is awful, which is ironic since it costs 3 times more. Twenty six dollars for a chicken sangwich, fries, and drink - get real. There are a lot more locally owned restaurants in Silicon Valley that are a lot cheaper. There are especially a lot of Thai restaurants, which is good, because I love Thai food. I think I had that like three times.
There are quite a few good restaurants in Sunnyvale too, but you have to know where to look.

Not going to make it back there this year. This is year is crazy busy and going to get busier and crazier, and any other ‘ziers I can’t think of right now.