Alfa Jango Blog Engineering, Software, and Entrepreneurship

Rails, Prototype, and JQuery in Harmony (or how to replace Prototype with JQuery)

It has become common practice to use JQuery as the javascript library of choice for Ruby on Rails applications. If you have a Rails app already using Prototype, it usually isn’t feasible to replace it in one swoop. And sometimes, you just simply need both to work at the same time. Whether you want to use both in unison (not recommended as a long-term strategy), or you want to gradually migrate from Prototype to JQuery, it’s not as scary as you may think.

The secret is that it is difficult to get Prototype to play nicely with JQuery, while it is rather trivial to get JQuery to play nicely with Prototype. This may seem like a minor distinction, but it is one that will save you a lot of headaches.

Use Prototype/Scriptaculous with JQuery

First, in our application layout, we’re going to get rid of <%=javascript_include_tag :defaults %> and include each javascript file explicitly just to make things a little less magicky and a little more straight forward.

Read the rest of this entry »


Printable Format for Any Webpage
(and the “Meat” Algorithm)

Last week, we added functionality to one of our web apps to show just the main content of any web-page, without all the other stuff. You may think of this as creating a printable view of any web-page, with all images, videos, ads, etc. removed. Here is an example of an original webpage vs. the printable view we create:

Feel free to skip straight to our “Meat” Algorithm, as we’ve so endearingly named it, if you’re not interested in the specifics of implementing it.

The Tools: Ruby and Nokogiri

Thanks to Ruby and a Ruby gem, called Nokogiri, it’s far easier to create this printable view than you may think. If you haven’t heard of it before, Nokogiri is a gem that reads and parses HTML, XML, and SAX, and allows you to easily search and manipulate these documents based on CSS selectors and XPATH.

Read the rest of this entry »


Make Sure Your Rails Application is Actually Caching (and not just pretending)

We recently worked on a Rails application that had page and action caching set up, only to find that it was not actually working. It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching. Searching through the interwebs for various Passenger/Apache configurations, such as this snippet on Github or this discussion on Google Groups, I found that many did not work with the most recent version of Phusion Passenger. What’s more, these configurations give the appearance that they are working.

For an introduction to caching with Rails, check out this post: Caching With Rails: An Overview

The Appearance That Caching Works

Here is what I mean by giving the appearance that caching is working:

  1. you add caches_page :index to your controller, for example
  2. configure and restart your server
  3. load the index page
  4. check your rails cache directory on your server, and see the index.html
  5. it works! (hint: this step is wrong, you’re not quite there yet)

Just because Rails is generating your cached page does not mean that your server is subsequently serving the cached .html file instead of sending the request to Rails again.

Ensure It’s Serving the Cached Page

To make sure it’s subsequently serving the cached page, the easiest method is to look at the created date on the cached file on your server, for instance:

Read the rest of this entry »


Performance Tuning for Phusion Passenger (an Introduction)

Phusion Passenger (aka mod_rails) allows for easy and scalable deployment of Ruby on Rails applications on Apache or Nginx servers. Part of what makes it so easy is that it comes with suitable default settings right out of the box, so that you don’t need to concern yourself with any of the details when deploying your application to production.

However, once you’ve launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I’ll address these settings with a few rules of thumb we’ve gathered from the Passenger documentation and simple trial-and-error.

Before getting started, we highly recommend employing Munin (instructions for Munin with Passenger here) and NewRelic Performance Monitoring for your Passenger/Rails application, so that you can fine-tune your settings for the best performance.

Note: the following settings are specific for Apache, you will need to modify the syntax/file-names for Nginx.

Per-server Settings

These settings are set per-server, meaning if you have multiple applications running on the server, these should be set only once throughout all of your .conf files. If you have only one Rails/Passenger app on the server, then you may set these in that application’s .conf file. On Ubuntu, this may be found at /etc/apache2/sites-available/yourapp.conf.

Read the rest of this entry »


How to Include Your Rails App Layout in Your WordPress Theme (or any PHP application)

Here’s the problem: You have a Ruby on Rails application and its WordPress blog. For the sake of consistency and branding, you’d like to use the same layout in your WordPress blog as you do for your Rails app. Perhaps your blog is one of the main links or tabs in your application’s header.

Why this doesn’t work: You proceed with creating a custom theme for your WordPress blog that looks exactly like your Rails application. Then over the next couple years, you begin to realize how tedious this solution is. You have to copy over your CSS stylesheets from your Rails app, your Javascript files (if your standard layout contains any Javascript elements, like a Twitter widget in the footer or whatever), and your layout images and assets. Then, every time you change or update the layout of your Rails app, you have to copy the changes in your custom WordPress theme. And you cannot just copy the changes, because if you have any Ruby/Rails functionality in the layout, you have to translate them into PHP functionality in your WordPress layouts.

And then there’s the impossible stuff… if anything in your Rails app layout loads information from the database in your Rails app, then you cannot have those elements in your WordPress layout, unless you do something like create an XML or JSON feed in your Rails app and then import it in your WordPress layout. But this is quite tedious still if you want to change any of it!

There’s an easier way.

Include Your Rails Layout Directly In Your WordPress Layout

The solution I found turns out to be quite simple. The basic idea is to create a :partial in your Rails app layout, make it publicly accessible via your config/routes.rb, have it generate and simply return the appropriate html using your controller method, and then import that html (and CSS) in your WordPress layout.

Read the rest of this entry »


How to Monitor Your Rails/Passenger App with Munin

Munin is a great tool to monitor resources on your server, showing graphs over time, so that you can analyze resource trends to find what’s killing your server before it causes major problems. It is also very configurable and can be made to profile and graph just about anything via plugins. And with a couple tricks, you can get it to monitor your Phusion Passenger application with ease.

Update: If you have RVM installed on your server and need Munin to work with RVM’s Passenger installation, follow all of the instructions below, and then make the changes described in Monitor Passenger with Munin when using RVM.*

* These changes will still use your server’s non-RVM-installed default Ruby to run the Munin plugin, which will in turn use your RVM-installed Ruby to run the passenger-status and passenger-memory-stats commands.

If you already have Munin installed and working, and just want to know how to get the Passenger Plugins working, you can skip directly to Install Munin Passenger Plugins, Configure Munin for Passenger Stats, and be sure to read the Gotcha.

Read the rest of this entry »


Google One-letter Suggestions

*the post below was originally written Dec 9, 2009, while this screenshot was captured on Dec 30, when the post was *published, which explains the slight discrepency... I didn't feel like going back and rewriting it

The Point

The other day, I went to search for something on Google and noticed some interesting results from Google’s suggest feature after typing just one letter. Wow, I thought. How presumptuous of Google to think it can guess my will and desires from one letter. Of course that then led to curiosity, and hence this post. What terms manage to garner the top 10 spots for each letter of the English alphabet? So without further ado, the top 10 terms for each letter on Google’s suggest feature (along with some of my comments).

Interesting Statistics

  • Brand names: 195 (75%)
  • Generic Search Terms: 44 (16.9%)
  • TV Shows / Movies: 6 (2.3%)
  • Famous People / Bands: 9 (3.5%)
  • Search Engines: 6 (2.3%)

Award of Awesomeness:

It’s a tie between Facebook and Yahoo, both for earning top spots in “w” for “www.facebook.com” and “www.yahoo.com” of all things. My hat is off to you both.

Multiple Entries

The winner for multiple entries is Yahoo, followed by Google (thought this is no doubt helped by the fact that most of Google’s competing services are directly linked to from Google’s home screen and thus don’t need to be searched for… not to mention the fact that there is less competition for the letter “y”).

  • at&t: 2
  • bank of america: 2
  • comcast: 2
  • ebay: 3
  • facebook: 3
  • google (including gmail): 6
  • itunes: 2
  • ikea: 2
  • mapquest: 2
  • myspace: 2
  • quotes: 4
  • ups: 2
  • verizon: 3
  • virgin: 2
  • yahoo: 8
  • zune: 2

Read the rest of this entry »


Cool Libraries for Ruby

I was just thinking about how often I come across cool projects and libraries people are developing for Ruby. We all know you can create cool web applications Ruby on Rails, but what about all of the other cool things you can do with Ruby. So, I decided to put together a short list with summaries to help me remember why these projects are in my bookmarks. In no particular order:

Nanoc

Nanoc’s website describes it as “a tool that runs on your local computer and ‘compiles’ documents written in HTML, Markdown, Textile, Haml, etc. into a static web site, ready for uploading to any web server.” Basically, it takes your dynamic Ruby code and turns it into static HTML which you can then upload to your server, meaning you don’t necessarily need Ruby installed on your server. Of course, this depends on what your site actually needs to do. But I could see this as a perfect fit for a Blog, for which you write posts on your local machine and deploy. Of course you’d need to implement comments with a third-party javascript widget like Disqus, but that’s just one example.

Mongoid

Mongoid is a Ruby ODM framework for mapping your Ruby application to use MongoDB for object storage. If you haven’t checked out MongoDB as an alternative to using MySQL for your storage needs, it’s worth a look. You may not even realize you have different needs until you discover there’s a database out there to fit those needs. For another cool Database-Ruby integration alternative, see Friendly, a gem that makes MySQL look like a document-store to your application.

Read the rest of this entry »


If You’re Nervous About Quitting Your Boring Job, You’re Sane

I recently stumbled upon an article called, “If You’re Nervous About Quitting Your Boring Job, Don’t Do It

As you can tell by the title of my post, I disagree. Let’s put aside the fact that I would not come to the conclusion that Lisa (the case study for the above post) should not have quit her job based purely on the fact that she failed the first time out. That’s completely normal, and I’d be excited to see what Lisa learned and how she applies it to her next endeavor. No, even more than that, I disagree with sentiment of the post.

Who knows what the future will bring, but I think I fall into the category of people “who have built up rare and highly desirable skills” and have accomplished a lot since I left my full-time engineering job to pursue my entrepreneurial passions.

My point here isn’t to brag, but simply to say that when I decided to pursue my passion and the desireable lifestyle of entrepreneurship, quitting my full-time job was one of the most nerve-racking and toughest decisions I’ve ever made, full of doubt and second-guessing. If you are having doubts or second thoughts about quitting your job, then congratulations, you are a pragmatic, rational, sane human-being.

Read the rest of this entry »


Page 4 of 41234