deep_send

If you’re Ruby metaprogramming and you need to call send several levels deep, give this little utility method I made a try:

class Object
  def deep_send(s)
    s.split('.').inject(self) { |memo, obj| memo ? memo.send(obj) : nil }
  end
end

Then you can say some_object.deep_send(’this.that.whatever’), instead of a bunch of inject/Proc messiness.

Read More      No Comments »

TextMate Beach Balls

TextMate rules!… until you hit Command+Shift+F and try to do a ‘Find in Project’

It beach balls because it’s searching huge log files. You can fix this by going to Preferences -> Advanced -> Folder References and entering this value for ‘Folder Pattern:

'!.*/(.[^/]*|log|.svn|CVS|_darcs|_MTN|{arch}|blib|.*~.nib|.*.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$

I just added log and .svn to it (.svn may not be necessary, but I suck at regular expressions and like to err on the safe side). After I did this, my searches became way faster, but the downside (or maybe not such a downside) is that you won’t see the log folder in the project drawer.

Read More      No Comments »

Gravatars on Rails

Patrick showed me a very cool site: gravatar.com.  At this site, you can upload an image of yourself (or whatever) and tie it to an email address.  Then, any time you post a comment on a Gravatar-enabled blog, your image will show up next to your comment.

This would be a great feature to add to greenisus, so I visited the implementation page at gravatar.com.  Unfortunately, there was no help for using gravatars with a Ruby on Rails site.  So, I coded a solution. 

First,  go into application_helper.rb for your rails app, and add the following: 

require 'digest/md5'

 

Then, add the following helper method:  

def gravatar_url_for(email, options = {})    
  url_for({ :gravatar_id => Digest::MD5.hexdigest(email), :host => 'www.gravatar.com',      
               :protocol => 'http://', :only_path => false, :controller => 'avatar.php'    
            }.merge(options))  
end

 

Now, you can use Gravatar URLs in your views like this:  

# plain old gravatar url  
<%= gravatar_url_for 'greenisus@gmail.com' %>    

# gravatar url with a rating threshold   
<%= gravatar_url_for 'greenisus@gmail.com', { :rating => 'R' } %>    

# show the avatar   
<%= image_tag(gravatar_url_for 'greenisus@gmail.com')%>   

# show the avatar with size specified, in case it's served slowly  
<%= image_tag(gravatar_url_for('greenisus@gmail.com'), { :width => 80, :height => 80 }) %>    

# link the avatar to some/url  
<%= link_to(image_tag(gravatar_url_for 'greenisus@gmail.com'), 'some/url')%> 

Enjoy! And look out for gravatars at greenisus soon!

Read More      1 Comment »

Performance On Rails

So, you just finished reading your copy of Agile Web Development with Rails, and you’re ready to launch an Internet startup.

You get everything set up: hosting, database, subversion…you put the finishing touches on your capistrano deployment recipe. Your app is now online, ready for the whole wide world to see.

You tell all your friends about it, and they tell a few friends and before you know it you have about 50 people playing around on your new website. It’s exciting, and you think you may be on to something that a lot of people will want to use.

Then, all of a sudden, your web site slows to a crawl. Maybe there are too many hops between your users and the servers (in my case, it was Tennessee and Mississippi users with servers in California). But it’s not. It’s your code. You have a scaling problem.

My scaling problem came pretty quickly, and I thought it was a hosting problem. So, to prepare to change hosting plans, I downloaded a backup of my production database. Since I had it, I figured it wouldn’t be a bad idea to install the data into the development database for my Rails app. I launched my app, and much to my surprise it was as slow on my iMac as the production website! (even though my iMac has much more RAM and I’m hitting localhost)

Why was it so slow? Too much database activity. Some people would see it and blame Active Record. But it’s not Active Record’s fault. Active Record provides a lot of nice conveniences for developers, but in a production environment they can often turn into performance nightmares if not used cautiously.

So, here is a set of guidelines I’ve discovered to help improve the performance of your Rails app:

Lesson 1. Download YSlow and Follow Its Suggestions

YSlow is a performance report card tool for Firebug. If you don’t already have Firebug, install it, and then install YSlow and pay attention to what it tells you. For most web apps, the performance problems are on the front end and YSlow will tell you exactly what you need to do to fix those problems. The only problem I’ve encountered is that I can’t yet move my Javascript reference to the bottom because it breaks the in_place_editor helper supplied by Rails.

Lesson 2. Denormalize Your Database Schema

greenisus revolves around asking questions in an online poll form and voting on questions submitted by other people. When you’re looking at a list view at greenisus, you can see how many votes and comments have already been submitted for a question.

greenisus list counts

Figure 1. List view vote and comment counts at greenisus.

At first, I coded this in a way that was more developer-friendly than database-friendly. I have a model for my questions and model for the votes and comments. The questions model had lines like this:

has_many :votes
has_many :comments

And in my view, I had code like this to show the counts:

So far... <%= pluralize question.votes.size, 'vote' %> and <%= pluralize question.comments.size, 'comment' %>

The problem with this is that question.votes does an individual SELECT for each vote for the question. This is terribly inefficient, so I changed my question model to have two new fields: vote_count and comment_count. Now, I reference situation.vote_count instead, and there are no extra SELECTs slowing the site down. The data is redundant, but it’s much faster to load, and that’s more important in the real world.

Lesson 3. Add Indexes To Your Models

If you’re showing items newest-first, don’t say :order => 'creation_date desc', say :order => 'id desc'. It means the same thing, and id is indexed, so your database doesn’t have to sort the results before returning them to you. If you’re showing items by a user_id, you can’t change your :conditions => ['user_id = ?', some_user_id], but you can add an index and get a great speed boost from your database. Just do it in a migration:

def self.up
  add_index :my_table, :whatever_column
end

def self.down
  remove_index :my_table, :whatever_column
end

Enzyte guy

Figure 2. Smilin’ Bob (The Enzyte Guy) Used These Tips To Enhance His Ruby on Rails Performance

I’m sure I’ll talk about performance again, but the two biggest sources of performance woes that I’ve seen have always been:

  1. Inefficient front-end decisions
  2. Inefficient use of the database layer

So remember to get YSlow, and watch your development logs with large amounts of data. You won’t see how complicated your site is when you only have a few pages worth of data in your development database.

Read More      2 Comments »

first week of greenisus

It’s been a wild first week for me after launching greenisus!  I’ve spent most of my free time this week talking about it and coding new features.  There are more on the way, but for now we’re going to work on a few small ones and make our Ruby on Rails test suite as comprehensive as possible.  We’re also going to be asking questions and voting!  Mario on the Grill 

Figure 1. I left Super Mario Galaxy out in the cold to work on greenisus. That’s how much I care. 

So check it out! It’s running great on the grid with a little extra RAM at MediaTemple.

Read More      No Comments »

greenis.us is live! great first day!

I turned on greenis.us last night and it has been a very exciting first day!  We already have 23 users, 42 questions, and 154 votes!  That’s not bad for a first day online!  We’ve had a very smooth experience with our mediatemple hosting! Thanks (mt)! I upgraded the memory after deployment and there was no interruption at all!  I’m so happy that I’m using too many exclamation marks!!!mission_accomplished.jpg 

Figure 1. Mission Accomplished!

 We released with a pretty simple site, and we’ve quickly discovered many areas that could use improvement. So, keep voting, and we’ll keep making greenis.us a nice place to spend your time on the Internet!

Read More      1 Comment »

Subversion Hosting - Follow-up

The winner is wush.net!  All of the Subversion hosts replied to me before the weekend was over, but wush got to me only a few hours after I emailed them.  The only downside to their plan is that I only get one repository.  Hopefully, I can work something out with them later without paying a ton of money for it (especially since all of my projects are small in file size, and I’d rather not host all projects in one repository).    Clockwork Orange - Alex Committed

Figure 1. This man has been committed, just like my code has been committed to Subversion at wush.net.

Wush has been great so far.  I made a dump of my repository and emailed them, asking to have it loaded into the new repository, and they had finished within five minutes!  So, at this point, wush is the best subversion host for me so far….  I’ll let you know if that changes. 

Read More      No Comments »

Subversion Hosting

I’m using mediatemple for Ruby on Rails hosting, and I’ve been very happy with them so far. I host my subversion repositories there, and they have run smoothly. The problem, however, is if you have a Grid Server account, you can only access subversion with the svn+ssh protocol. https is not available.I have two problems with this:

  1. scplugin (which is essentially TortoiseSVN for Mac) doesn’t seem to work
  2. a greenis.us developer is stuck behind a firewall that won’t allow outgoing traffic on port 22

I can live without subclipse (viva la bash!), but when I have to deal with connectivity problems, that’s a real issue. Nevermind that closing outbound traffic on port 22 is just plain silly; after all, most IT departments stifle people more than they help people.Winning For Dummies

Figure 1. This is definitely “out of the box.”  This was not designed by your IT department.

So, I’m looking for a nice subversion host. I have emailed three hosts, and I’ve been waiting for a few hours for a reply. Whoever responds first gets my business. I emailed wush, svnrepository, and codespaces (not linking to them and boosting their PageRank when they haven’t earned it yet).

Read More      2 Comments »

Bleeding EDGE

So, I’m stuck at the Golden Moon Hotel in Philadelphia, Mississippi (a small casino town about halfway between Starkville and Meridian). Katie’s at the annual Mississippi Counselor’s Association conference, and my job is the Internet . . . so I can work from anywhere, right?

The Golden Moon is a clean, modern facility, but it has one huge problem: there is no Internet access in the hotel rooms. I’m guessing this is so you’ll be discouraged from staying in your room, and instead spend your time on the casino floor. There is a business center, but it’s terrible. It’s very cramped (tiny cubicles) and the chairs are uncomfortable and not adjustable. To top it off, the lady at the desk has an attitude and there is practically no air circulation. The business center also keeps very short hours (shorter than regular business hours in corporate America).

The only other alternative for Internet access is a bar/coffee shop on the casino floor called The Whiskey Bean. It’s attractive, but noisy because it’s next to a sea of slot machines. The Whiskey Bean closes at 6 PM.

Last night, Katie had to go to something for about three hours, so I had two choices:

  1. Gamble
  2. Sit in the hotel room and write code for greenis.us

Needless to say, I chose option 2. But without Internet, it was quite annoying, because I couldn’t connect to my Subversion server and get Ryan’s contributions to the code base.

Also, we are using tadalist to keep up with our tasks for the site. Tadalist is a wonderful site. It’s a simple to-do list tool that’s very easy to use.

Thank God I have an iPhone. A lot of people complain that Internet access is slow on the iPhone while on the EDGE network, and that’s true, but tadalist takes a smart approach and delivers a very responsive site, even with a slow connection.

The Edge

Figure 1. The Edge

If you look at tadalist in Firefox, it’s a big page and it’s pretty and colorful. But on the iPhone, it’s very simple. There’s only one image, and all user interface elements are simply solid-color divs. The page weight of the regular home page is 136.6K, but the page weight of the iPhone home page is only 5.7K (5K of which is just the logo). That’s very smart, and if they got rid of the logo, the site would be even faster on EDGE.

I’ve learned a valuable lesson from this. I’ve been working on making my 9 to 5 site faster, and I’ll blog about that later, but for greenis.us I’m going to take it a step further and make an iPhone view that is blazing fast, even on the EDGE.

Read More      1 Comment »

coming soon - greenis.us

On most online forums and chat software, I go by the screen name greenisus.  I once had it on my license plate, and I currently use it to host my resume.  When people ask what greenisus means, I usually tell them it’s a long story and that they really don’t want to know. Owning a domain name just to serve my resume is not a very efficient use of a DNS entry, so I’m going to host a web app I’m working on at this domain soon.   Why not just find a new domain name for my app?  Well, it’s pretty hard to find anything decent that’s not taken, and I’m having a difficult time coming up with a name for my app anyway.When I was a kid, I spent most of the time playing around and goofing off with my friend Gary.  Gary had (and still has) one of the most vivid imaginations I’ve ever witnessed.  One day, we were helping my dad install a pool liner, and while we were holding it up, Gary told me he had invented three new colors:

  1. Greenisus
  2. Bluedica
  3. Orangicide

This idea was so funny to me that I dropped the pool liner and effectively wasted half an hour of our hard work. Gary and I kept talking about these colors, and they were hard to describe. The best I can imagine is that they are all some strange shade of brown that’s outside of the visible light spectrum. (how is it possible for it to look brown and be outside of the visible light spectrum? because Gary invented it)The word greenisus is pronounced “green-ih-sus,” but when I tell people my gmail email address over the phone, they usually think it’s “green is us.”So how does this fit my new web app? My app is a simple tool to ask for advice from the masses, and have them all vote on your options and discuss the problem. So, in a sense, when we ask the community a question, we’re “green” about the topic. Green is us.

Read More      No Comments »