Handling Ajax Redirects in Rails

Apparently there is no clean way to do ajax redirects in Rails. The recommended way to do that is to send a regular 200 OK status code along with some special parameter signaling the javascript side that it is a REDIRECT.

Now, what follows is a relatively cleaner way to handle redirection in ajax calls.

On the rails side, we create a helper that returns a response that has no content but just one header x_ajax_redirect_url and status code 302.

application_helper.rb
1
2
3
def ajax_redirect_to(url)
    head 302, x_ajax_redirect_url: url
end

A global ajax handler on the javascript side will intercept all ajax calls across the application and redirect on seeing the appropriate header construct.

application.js
1
2
3
4
5
6
7
8
9
10
$.ajaxSetup({
    statusCode: {
        302: function (response) {
            var redirect_url = response.getResponseHeader('X-Ajax-Redirect-Url');
            if (redirect_url != undefined) {
                window.location.pathname = redirect_url;
            }
        }
    }
});
Read on →

Adding custom ringtones to iPhone via iTunes

I recently tried adding custom ringtones to my iPhone via iTunes and what a pain it was! I had to browse through multiple apple support pages and blogs to finally figure out how to accomplish this trivial task. God I love and miss those Nexus S days…

This post is to share the steps involved so that it saves the frustration for others.

So my gig:

  • iTunes 11.1.5 running on Mavericks
  • iPhone 5
Read on →

Running cucumber features in different locale

We use Selenium + Capybara + Firefox to run all our cucumber features. Recently, we decided to extend the tests to run in different locales and this post is a summary of the problems and the plausible solutions.

We had the following objectives in mind:

  • Since, we run the tests in firefox, the test process should be capable of creating an appropriate profile with required locale settings
  • The tests should be generic enough so that they don’t have to be written and maintained separately for every locale

Setup

The first step involves creating a new browser profile and switching the locale (italian in this case) and passing it on to the Capybara driver.

env.rb
1
2
3
4
5
6
7
8
9
10
11
Capybara.default_driver = :selenium

it_profile = Selenium::WebDriver::Firefox::Profile.new
it_profile['intl.accept_languages'] = 'it' # Italian Locale

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :firefox,
                                      :profile => it_profile)
end

Capybara.current_session.driver.browser.capabilities.firefox_profile = it_profile

The advantage of creating a profile on-the-fly is that the tests do not require every machine to have a pre-configured firefox profile that has the necessary settings.

Read on →

What’s the deal with Ruby GC and Copy-on-write

This post aims at answering the following questions:

  • What is Unix Copy-on-write (COW)
  • Why is current version of ruby (1.x.x) not COW friendly
  • How does the GC packaged with Ruby 2.0 fix that

Holy COW

The fork functionality in Unix systems uses an optimization strategy where memory is shared between the parent and the child processes. The shared memory is maintained till either the parent or one of the children modify their copy of the resource. At that point, a true private copy is created to prevent the changes from being visible to other processes. The primary advantage is that if any of the processes do not make any modifications, no private copy needs to be ever created. This is called Copy-on-write (COW) technique.

1
2
3
4
5
6
7
8
9
shared_array = [1,2,3,4,5]

if fork
  #will be executed by parent process
  parent_array = [6,7,8]
else
  #will be executed by child process
  child_array = [9,10,11]
end
Read on →

3 ways to keep a Linux process alive

Assume that you have a ruby script data_cruncher.rb that will run for a long time. You would like to log into a VM, run the script and keep it running in the background when you log out. Now, I end up writing long running scripts like these all the time and I found the following ways to make them run uninterrupted:

1. Job Control

Job control allows you to interact with background jobs, suspend foreground jobs and manage multiple jobs in a single session. Also, when you later realize that the currently running command must be pushed to the background, job control makes it easy to do that.

  • Start running the process using the command ruby data_cruncher.rb
  • Press ctrl+z to suspend the process
  • type bg to resume it in the background

That’s it. The job will run in the background. Note that using bg command later is the same as running the parent process with & at the end like this: ruby data_cruncher.rb &.

Read on →