A Lazy Javascript, a Ruby Load and a Sponge plugin to a bar

March 21, 2008

Lazy Load Content

Sometimes a portion of your page takes a really long time to generate and render. Instead of allowing the user to spin his thumbs, you can display the bulk of content to user and send off a separate ajax request to gather the “slow” data. Lazy Load Content makes late loading a portion of your page through ajax as easy as setting up a remote_function call. With options to render fragment cached if they exist, and perform the late load if not, your app will fly like the sloth eating eagle.


ruby script/plugin install  http://arperftoolkit.rubyforge.org/svn/trunk/lazy_load_content/

Copy the file javascripts/lazyLoadContent.js into your public/javascripts folder and include it with prototype.js in your views.

The Basics:

In your rhtml or haml file:

<h1> I love giraffes.. basically </h1>
<% lazy_load(:remote => {:update => 'lazyLoadContent', :url => {:action => 'lazy_load_action'}})do -%>
  <div id = "lazyLoadContent"> We wait while the giraffes are smelling themselves. </div>
<% end -%>

In your controller define the specified function

def lazy_load_action
  render :text => 'The giraffes are ready!'

When the page loads, the loading text is displayed: We wait while the giraffes are smelling themselves.
Then after the ajax call we see: The giraffes are ready!

…….. and more………. Read the rest of this entry »

Rails is spewing sql in my soup (better sql logging for optimizing)

February 12, 2008

Im all pissy today because Rails is spewing all these sql statements out and I have no idea where they are coming from so I can add my little optimizations or what have you. As if I wasn’t already crying in my soup.

log_sql_caller(:include => 'SELECT count(*) from giraffes', :log_level => :error) { ... }

So I wrote a little plugin to print the callstack in the log so I don’t have to read eleventy billion lines of code! Or enable some debugger of pain.

ruby script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/log_sql_caller/

Que bueno! Now our soup is thick and moist like goat stew. Yum.

Delegate to the Giraffe

February 12, 2008

I have gotten very tired of writing the same code over and over. And apparently I was so tired I forgot to publish this last year.
def foo_bar
my_member ? my_member.foo_bar : nil

The rails delegate method allows you to delegate a method to another class. Neat! There are two tweaks that I made to suite my salty tastes.

1. Nil freaks out. Sure you can write something like
:to => (my_member or return nil) as pointed out on the rails ticket. But that is sort of groddy. boo!

2. I want to rename the destination method sometimes. Avoid collisions, make new friends. Win-Win.

Here is delegate_x. Delegate extra! You can list out the delegated methods or, to rename them, put them in a hash of destination_method_name => target_method_name

class WebTwoPointOMGCorp
attr_accessor :giraffe

delegate_x :hovercraft, :to => :giraffe
delegate_x :to => :giraffe, :giraffe_spots => :num_spots

The first is equivalent to:
def hover_craft
giraffe ? giraffe.hover_craft : nil

and the second

def giraffe_spots
giraffe.num_spots if giraffe

With ActiveRecord :has_one and :belongs_to associations, check out Stefan Kaes’s piggyback plugin to piggy back attributes (define methods) to queried records.

Sweet. A little (terribly formatted) code below. Jam it in the lib or something. Make it a plugin. Just remember, the hovercraft owned by the giraffe is really the hovercraft owned by WebTwoPointOMGCorp.

(Actually I just wrote this whole post to show Joel’s awesome new spongecell giraffe that breathes fire! We love giraffes and so should you.)

Read the rest of this entry »

The complete nginx solution to sending flowers and files with rails

November 13, 2007

Rails send_file is effed. It may send you flowers and whisper sweet nothingsblythedoll into your ear, but believe me, it will steal your memory, your lover and your freetime. If you are using apache or lighttpd, all the cool kids are using the x_send_file plugin. Nginx fans, check out my monkey patch so you too can send files, flowers and love without crashing mongrels with nginx’s equivalent X-Accel-Redirect header.

1. Install the x_send_file plugin.

ruby script/plugin install http://john.guen.in/svn/plugins/x_send_file

2. Copy monkey patch to

Replace vendor/plugins/x_send_file/lib/x_send_file/controller.rb with http://www.acidlunchbox.com/blythe/rails/x_send_file/controller.rb

3. Configure things


remember to also add to server section if sending over ssl

location /files {

root /data/spongecell;




#send files through nginx


:header => ‘X-Accel-Redirect’,

:root => ‘/data/spongecell’,

:file_paths => ‘/data/spongecell/files’


In this example, x_send_file will create the following response header

>> x_send_file ‘/data/spongecell/files/snow/iheartbigdumps.jpg’

>> response.headers['X-Accel-Redirect']

=> ‘/files/snow/iheartbigdumps.jpg’


Other resources:

Nginx X-Accel-Redirect

Nginx X-Accel-Redirect with rails and php examples




Read the rest of this entry »

Up all nite with the sponge

November 15, 2006

Every developer have been up all night doing something completely ridiculous: forgetting a semi-colon and screwing your JS compression, writing hot new code for your acts_as_idiot functionality that unbeknownst to you the RoR team has already implemented and included in the latest version. Once in the C++ days I even forgot a bracket and to deallocate memory. Regardless, there are so many tricks I wish I knew before I was up all night with the sponge.

Let’s take our kimonos off and run around naked. I will show you mine if you show me yours.Kimono
Read the rest of this entry »


Get every new post delivered to your Inbox.