• DRYing up link_to_remote for degradable URLs

    I like to keep my links in my Rails applications that make AJAX requests degradable in non-JavaScript browsers so I often find myself doing this:

    
    link_to_remote 'Show details',
      { :url => { :action => 'show', :id => post.id } },
      { :href => url_for({ :action => 'show', :id => post.id }) }

    This allows me to get HTML links like so:

    
    <a href="http://www.example.com/posts/show/123" onclick="...">Show details</a>
    

    instead of the usual (if the :href parameter is not specified):

    
    <a href="#" onclick="...">Show details</a>
    

    You've probably already noticed the duplication right there in the :url key of the 2nd parameter (options) and the :href key of the 3rd parameter (HTML options) that litters my Erb templates where link_to_remote calls are prevalent. I got tired of editing and copying and pasting the url_for parameters so I monkey patched Rails' PrototypeHelper class in ActionPack (actionpack/lib/action_view/helpers/prototype_helper.rb).

    To do this, I had to replace Rails' link_to_remote method definition. Thanks to living and breathing code examples in Rails that do this, I was able to promptly come up with this module that I placed into the lib/ directory of my Rails application and require it in my environment.rb file.

    
    module Bezurk #:nodoc:
      module ActionView #:nodoc:
        module PrototypeHelperExtensions
          def self.included(base)
            base.alias_method_chain :link_to_remote, :degradation
          end
    
          def link_to_remote_with_degradation(name, options = {}, html_options = {})
            html_options[:href] = url_for(options[:url]) if !html_options.has_key?(:href)
            link_to_remote_without_degradation(name, options, html_options)
          end
        end
      end
    end
    
    ActionView::Helpers::PrototypeHelper.send(:include,
      Bezurk::ActionView::PrototypeHelperExtensions)

    The modification to link_to_remote itself is dead simple: it checks for the non-existence of a :href key in the html_options parameter and sets the :href option in the html_options parameter hash to the url_for the :url value in the options parameter hash. It then calls the original link_to_remote to do what it does best.

    I've submitted a (trivially simple) patch to Rails in my opened ticket #7904 - DRY up link_to_remote for degradable URLs. Let's hope this makes it in so I can kill my monkey patch!

  • A Singapore MRT WTF

    The Singapore MRT has to be one of the best public rail transport systems in the world (I fancy the Tokyo train/subway system as well - the fare adjustment machine is one of those "since sliced bread" ideas), but they still have room to screw up in a Daily WTF-worthy way.

    This sign at Raffles Place MRT used to show train arrival times:

    MRT signboard rebooting


    It's been there for about 3 days, was still there when I checked this morning.

  • Validate that favicon

    The Favicon validator ensures that you have a correctly created favicon. If you're wondering why your favicon works in some browsers but not in others (like IE), it probably has an incorrect format.

    Favicon validator results


    I also learnt from validating my favicons that you can actually place more than 1 version of the favicon image into the .ico file (it seems to be a container for these images rather than being an image itself).

    Oh, and the same site also has a very handy Favicon generator - I used this to fix some of my favicons, which used to be GIFs renamed with the .ico file extension. Very handy.

  • Firefox tips sharing session at WebSG meetup

    Kevin has uploaded a video of my session on Google Video - it's kinda grainy and really soft.

    I was at the 2nd WebSG (Web Standards Group Singapore) meetup (read about the recap of the meetup) on Wednesday (28 Feb 2007) where I hosted a short session on some Firefox tips. It's all thanks the urgings and encouragement of Lucian (the organizer) that I finally got down to publicly sharing my Firefox knowledge ever since I wrote Firefox Secrets.

    Me at WebSG meetup 2


    I was asked to blog about this (since I didn't have any presentation slides), so here it is, for those of you who missed anything in my mumbling or frantic keystrokes.

    Keyboard shortcuts you should know

    Note: Mac users should replace Ctrl with the Cmd key for any of the keystrokes listed below.

    • Ctrl-L puts your cursor in the address bar, Ctrl-K puts your cursor in the search bar.
    • Ctrl-T opens a new tab and puts your cursor in the address bar, but
    • I forgot to mention this during the session, but I personally prefer using Ctrl-L or Ctrl-K, typing in the new URL or search query, and hitting Alt-Enter open the new page or search result in a new tab - it just feels more accessible than Ctrl-T. Most people I observe still use Ctrl-T though.
    • Ctrl-Shift-T re-opens closed tabs. Keep hitting it to reopen more closed tabs in your history. This is a big time saver. Newly built into Firefox 2.0.
    • Ctrl-Tab and Ctrl-Shift-Tab brings you to the next and previous tab. If you're having trouble remembering both, just remember that the usual convention is a Shift does the reverse. So all you need is to remember Ctrl-Tab, and adding a Shift modifier does the reverse (i.e. goes to the previous tab instead of the next).
    • Ctrl-F brings up the Find bar, and you can start typing in whatever you want to find on the page and in any input fields. Use Ctrl-G and Ctrl-Shift-G to go to the next and previous occurences respectively. The Find bar is good, but...
    • I forgot to mention (as Zul mentioned to me after) during the session that Quick Find is much better. Just hit / and start typing as you would for the normal Find bar, and the Quick Find bar will go away after several seconds. Much better in most cases than the Find bar which likes to stick around until you close it. And why is this important? Because the Find bar takes up screen real estate, especially for those of us using wide screens.

    Getting rid of the close button on all tabs

    I've been told how annoying Firefox 2.0 is when it insists on putting a close button on each tab.

    Firefox 2.0 - close buttons on each tab


    Accidental closing of tabs when you try to select them is one big complaint. Well, you can turn it off and go back to the good olde Firefox 1.5 days of a single close button on the extreme right of the tab bar. Enter about:config in your address bar, filter for browser.tabs.closeButtons, double click on the preference entry that shows up, and change the value from the default of 1, to 3. If you don't want the close button on the right of the tab bar, enter a value of 2 (this assumes you're going to close tabs with either Ctrl-W or middle-clicking on the tab - this is my prefered setup).

    Backing up your profile

    MozBackup is all you ever need if you use Windows exclusively. This neat little (Windows-only) application backs up your profile into a single file - all you need is to use a very simple wizard. And of course, you can restore your profile just as easily. I loved this when I was still using Windows.

    Like I said, people using other operating systems can easily DIY - just read Mozilla's official documentation on Firefox profiles. You're probably using either Mac OS X or some Linux distro - you can do this without any hand-holding ;)

    What about backing up extensions?

    Some people asked me during and after my session about backing up extensions. FEBE is one extension you can look at unless you use a Mac (I tried the beta version that supports Mac OS X, but gave up when I couldn't get it to use the correct binaries for zipping and unzipping.)

    My personal take on backing up extensions: don't back them up. I prefer to dump a list of extensions (I use the Extension List Dumper extension) that I have installed and just re-install them again. This is no big chore for me now since I have significantly fewer extensions installed than back in the day. I just like starting clean, after having had bad experiences with restoring extensions from backup before.

    Still, if you really want to back up your extensions, I say just go ahead and see if it works (MozBackup and FEBE should work).

    Profiles and "why is my Firefox so damn slow?"

    This is one thing I had planned to mention but I got kinda threw off kilter when the slowness of the wireless internet connection made me worried about quitting Firefox (didn't want to quit Firefox, load up another profile, only to find that it'd take another 5 mins to get my demo tabs loaded again).

    Anyway, some questions I often get is, "why is my Firefox so slow?" and "why does Firefox use up so much RAM/CPU?" The first thing I suspect is a busted profile. Whenever I experience intolerable slowness in my Firefox setup, I dump and restore my profile into a newly created profile, minus all the tab history information (and of course, never restoring backed up extensions). Sometimes I even start clean new profiles, importing only bookmarks. You'll very likely notice immediately that Firefox is back to its old speediness. And if anything happens, you still have your old profile with you.

    I'm not that certain of what exactly causes Firefox to get too slow over time, but my suspicions lie with:

    1. Firefox saving browsing history in all the bazillion tabs you have open - that's a lot of data to be carrying around
    2. broken/bad extensions
    3. a busted profile

    Profile management for productivity

    Firefox hides its Profile Manager from you. You can access it by passing the -profilemanager switch to your Firefox executable. For Windows users, it's actually --profilemanager.

    Firefox profile manager


    The Profile Manager is really handy for people who need to remove distractions. For me, I setup different profiles for work and for play so I don't become distracted the "play" tabs I have open while I'm working (I'm beginning to like the Tab Groups extension for segregating tabs more though). Also really handy for setting up clean profiles for running user demos (I setup a WebSG profile for my presentation and installed specific extensions I wanted to talk about) - don't really want to have your naughty browser history showing up in some autocomplete heh.

    Some extensions I dig

    I also demoed some extensions that I find essential.

    • Download Statusbar for unobtrusive downloads.
    • All-in-One Gestures for easily accessible Back and Forward gestures when you happen to have your hand on the mouse. Like I said, I don't use any of the other gestures because they all require memorization (I use too much of my memory storing keyboard shortcuts).
    • Super DragAndGo for drag and drop searching. Great time saver when you are doing online research.
    • Web Developer toolbar for its convenient Disable Cache, browser resizing, and HTML validation shortcut (Ctrl-Shift-A).
    • UrlParams for easy viewing and editing of GET or POST parameters.
    • And of course, Firebug - godsend for web developers, web designers, and especially AJAX developers like us.

    Oh, and Mousepose was the application I used to get my keystrokes to show up on the screen. It's simple yet extremely helpful for presentations, though I probably should have used the mouse spotlight feature more. Oh well, I did just buy the license for it a few hours before the meetup - goes to show how unprepared I was heh.

    It's late and I'll blog more on the event and the people I met another day.

  • Tab Groups for Firefox: best extension since sliced bread... er, tabbed browsing

    I saw a Lifehacker blog post today in my RSS reader on this Firefox extension, took one look at the screenshot, and went "Woooooow, give me some of that". Aforementioned Firefox extension: Tab Groups extension for Firefox.

    As its name suggests, it allows you to group your tabs in, er, groups. Forget the verbiage, just look at the screenshot:

    Tab Groups extension in action


    This extension is a godsend for those of us who usually have a bazillion tabs open in Firefox (personally I keep it under a bazillion tabs so all those tabs don't slow Firefox down - I usually start killing tabs once I hit 30). By allowing you to group your tabs into logical groups, such as one group for work, one for play, and one for your many Gmail accounts, it brings some order to the "tab hell", which usually happens when you have to scroll or Ctrl/Cmd-Tab through more than 20 tabs to find the tab you want. Some people may use an Expose-style extension to show thumbnails of all their tabs at once, Showcase is one example - those are quite nifty as well though I've always found them to be unmanageably slow when there are too many tabs, which kinda defeats the purpose.

    Anyway, coming back to Tab Groups extension... This is an extension that's going to be absolutely necessary for those of us who like having many tabs open at once. For those of you who are less tab-crazy, or even, god forbid, surf in single tab mode, I'd expect you'd be much less excited about Tab Groups.

    Tab Groups is pretty much early release software (the author considers it pre-alpha), but it's very much already usable at this stage. You can create groups, rename them, and start dragging tabs into whichever tab group they belong. There are several features that are missing but almost definitely planned for future releases. For one thing, you can't move tab groups around, but I'm sure that'll be baked in soon enough. Also, the tab selector (the dropdown list of tabs thingie on the extreme right of your tab bar) stops working if tab is in another group. Another thing is the Ctrl/Cmd-Shift-T to reopen closed tabs currently reopens tabs in a new tab group. All small inconveniences (that would be soon be removed once Tab Group history gets baked in) for a very nice extension.

    Via Lifehacker via CyberNet News.

subscribe via RSS