AJAXify your Viviti Site

June 10, 2009

I recently noticed an interesting Viviti site that utilizes the built in jQuery and some simple JavaScript to make all the pages load via AJAX, and thought I'd quickly do a write up for anyone else using Viviti who would like to do the same thing.

Since the main content area of Viviti is inside a div#location_0, all you have to do is hook into the primary navigation links and use the jQuery load function to replace the contents of #location_0 on the current page with the contents of #location_0 on the target page. Adding a slideUp/slideDown with some callbacks makes it have a nice transition as well.

The JavaScript to do this is: 

$j('#primary_navigation li a').click(function() { loadPage( $j(this).attr('href')); return false; });

function loadPage( page ) { 
  $j('#location_0').slideUp(300, function() { 
    $j('#location_0').load( page + ' #location_0 > *', function() { $j('#location_0').slideDown(300);}); 
  });
}


You will need to add this in a script tag to your Viviti theme or as an external JavaScript file. YuAo's script is a bit more complicated and adds a loading indicator as well as dealing with url hash stuff so bookmarking and history works as well, you can check it out at http://showcase.imyuao.com.

Memcache Problems with Rails 2.3 and Litespeed or Passenger?

May 14, 2009

This week was a fun week of code spelunking.

Turns out, ActionController::Session::MemCacheStore runs through all the servers and checks to see if they are alive when the session store is first setup from your environment.rb. This causes it to connect to the Memcache server(s) and if you use a web server like Litespeed or Passenger that fork from a parent process, you end up with multiple processes sharing the same socket.

This is bad because then your child processes trip over each other in their eagerness to get data from Memcache. This can result in all kinds of strange behavior from errors to having incorrect data returned. The Passenger documentation does a good job of example of showing an explanation of the problem (and a solution for those of you using Passenger).

The easiest way to solve this is to ensure your parent process disconnects from Memcache before the fork occurs. In Litespeed this means editing RailsRunner.rb - specifically adding a call to reset to RailsRunner.rb, right before the existing call to ActiveRecord::Base.clear_active_connections! (which is there for the same reason). To do this we have to create a Memcache client and tell Rails to use it, otherwise there is no clean way to access the instance it will create.

In environment.rb:

# you might have to require 'memcache' 
SESSION_CACHE = MemCache.new( 'myserver.com:11211', 
  :namespace => 'myapp_session' )
config.action_controller.session_store = :mem_cache_store
config.action_controller.session = { :cache => SESSION_CACHE, 
  :key => '_my_app_session', :expires => 1.hour}


Then in Litespeeds RailsRunner.rb add SESSION_CACHE.reset right after where it does it's ActiveRecord::Base.clear_active_connections!

This should do the trick. Monkey patching the MemCacheStore to reset after doing the alive? check (or not doing the check) should also fix it.

Rails 2.3 count with named scope

April 29, 2009

There is a known bug in Rails 2.3 that is worth being aware of, count no longer works on anything using named_scope. There are some patches and a fix available in the ticket there and it looks like it will be resolved in the next version... but for now, beware!

also, I'm not sure when but somewhere between rails 2.1 and 2.3 they changed the (partialname)_counter back to it's long lost behaviour of the index starting at 0 (it was changed to 1 awhile ago, and is now back to 0). Please, make up your mind!

 

Recently Heard

Nowby Sweatshop UnionPlayed on 2009/07/03 at 12:36AM
Come On (Part III)by Stevie Ray VaughanPlayed on 2009/07/03 at 12:32AM
Said and Doneby Sweatshop UnionPlayed on 2009/07/03 at 12:28AM
Say What!by Stevie Ray VaughanPlayed on 2009/07/03 at 12:23AM
The Conspirator (Main Theme)by The Quantic Soul OrchestraPlayed on 2009/07/03 at 12:19AM