Technical
How To Set Focus On Popup Window Using Monkey Patching

My Love for ruby and Rails increases every minute I’m working with it.

I had a situation where there were many records on the same screen and there was a link on a screen that would have to be open with popup. Not to mention that its super easy using regular link_to method, but it became an issue when client found that if you opened a popup before and click on the same link, but the popup is minimized you’d get a feeling that nothing happened.
Basically, for that i had to set focus() for the popup name that user needs to open. Going through the rails source I found action popup_javascript_function(line 369) which sets all the options but I did not find the way how to set the focus for the window that is opening without changing the method. Using flexibility of Ruby and Rails it became super simple to change behavior of the method.

I added the following to my app/controllers/application.rb


module ActionView
  module Helpers
    module UrlHelper
      private
      def popup_javascript_function(popup)
        popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}').focus();" : "window.open(this.href);" 
      end
    end
  end
end

This basically overrides just a single method of the ActionView module preserving original behavior and makes it a very clean way for the popup.

NOTE: at the moment of writing this article Rails were at the 1.2.3 version.