Month: November 2009

  • As stories can be told in first person, or third person, in the form of a diary or a tale, as book or comic or movie; I was thinking that blogging could be a literarly style as well.

    I can think of two sub-genres. Historic and fantastic blogging.

    For historic imagine a blog written in the context of -70 (minus 70) years. So that on October 19th 2009 you’d get a post for October 19th 1939. Who would be the blogger? It could be an important person, what would Churchil blog? Or it could be an unnamed person, an anti-nazi frenchman for example. They could also have a Twitter account! It would be an interesting way to learn history.

    The other genre would be total fantasy. A blogger in the future, imagine if for some strange reason, blog posts of a guy surviving the singularity travel back in time? What if blog posts from a galaxy far away? I would certainly follow those blogs! But of course, it’s hard work that requires a very good writer.

    Another thing that could be applied to any fictional blogging is having a network of blogs. Imagine reading the blogs of a frenchman in the resistance, a nazi soldier, a Russian red-army member. All blogging about the same, from different perspective! What about reading the Twitter feeds of Frodo, Sam, Gandalf, Aragorn, Saruman?

    I think it would be very entertaining.

  • I like knowing when something goes wrong with my web apps, so I’m using Super Exception Notifier to get by email a report should any exception be raised in the app. If you go to Super Exception Notifier’s site you’ll see some instructions on how to add it to your project. This is how I do it.

    Add the gem requirement in environment.rb:

    config.gem 'super_exception_notifier', :version => '~> 2.0.0', :lib => 'exception_notifier'
    

    Then be sure to have gemcutter in your gem sources:

    gem sources
    *** CURRENT SOURCES ***
    
    http://gemcutter.org
    http://gems.rubyforge.org/
    http://gems.github.com
    

    If you don’t have it, you can add it this way:

    gem install gemcutter
    gem tumble

    To install the gem, in your Rails project run:

    sudo rake gems:install

    Create a file in config/initializers, I’ve called it exception_notifier.rb and inside I’ve set up the only really needed value for the notifications, the email address:

    # Notification configuration
    ExceptionNotifier.configure_exception_notifier do |config|
      config[:exception_recipients] = %w(pupeno@pupeno.com)
    end
    

    The last task is to make your application controller noisy by adding one line to it (the second one of course):

    class ApplicationController < ActionController::Base
      include ExceptionNotifiable
      #...
    end
    

    You also need to be sure that you have ActionMailer properly configured, otherwise no mail is going to get through.

  • For my Ruby on Rails pet project Is it Science Fiction? I’ve reached that point when I wanted to show tabs. You know, for the menu, on top of the page. I quickly wrote something like:

    <ul class="tabs">
      <li><%= link_to 'Home', root_url %></li>
      <li><%= link_to 'Recommend', new_item_url %></li>
      <li><%= link_to 'Ranking', items_url %></li>
      <% if session[:user_name] -%>
        <li><%= link_to 'Log out', session_url, :method => :delete %></li>
        <li><%= link_to 'My Profile', edit_profile_url %></li>
      <% else %>
        <li><%= link_to 'Log in', new_session_url %></li>
      <% end %>
    </ul>
    

    and styled it like the Listamatic Unraveled CSS tabs. And everything was good, except that the current tab wasn’t highlighted. Highlighting it is a matter of setting the correct class for it.

    For that I created a new version of link_to that adds the “current” class to a link if it points to the current page. That is not very hard actually, but you have to consider that some tabs, like Ranking, are not only highlighted when that page is show, but when any subpage (like a ranked item) is shown as well. After trying many solutions (in an effort to find the simplest one) I’ve settled for link_to2 and the tabs now look like this:

    <ul class="tabs">
      <li><%= link_to2 'Home', root_url %></li>
      <li><%= link_to2 'Recommend', new_item_url %></li>
      <li><%= link_to2 'Ranking', items_url, {:extra_current => {:controller => :items, :action => :show}} %></li>
      <% if session[:user_name] -%>
        <li><%= link_to 'Log out', session_url, :method => :delete %></li>
        <li><%= link_to2 'My Profile', edit_profile_url %></li>
      <% else %>
        <li><%= link_to2 'Log in', new_session_url %></li>
      <% end %>
    </ul>

    Look at the Ranking tab, it has an extra_current that adds other pages to be treated as current. The code to do this is the following (I’ve put it in application_helper.rb):

    module ApplicationHelper
      def link_to2(*args, &block)
        if block_given?
          options      = args.first || {}
          html_options = args.second || {}
        else
          name         = args.first
          options      = args.second || {}
          html_options = args.third || {}
        end
    
        if current_page?(options) or as_array(html_options[:extra_current]).any? {|o| current_page2? o}
          html_options[:class] = add_class(html_options[:class], "current")
        end
    
        html_options.delete(:extra_current)
    
        if block_given?
          link_to(options, html_options, &block)
        else
          link_to(name, options, html_options, &block)
        end
      end
    
      private
    
      def add_class(classes, new_class)
        ((classes or "").split(" ") << new_class).join(" ")
      end
    
      def as_array(o)
        if o == nil
          []
        elsif not o.is_a? Array
          [o]
        else
          o
        end
      end
    
      # current_page? of {:controller => :blah, :action => :bleh} when the routes also require an id raises a route error. current_page2? doesn't.
      def current_page2?(p)
        current_page? p
      rescue
        return false
      end
    end
    
    

    Feel free to pick the code and use it in any way you want. I’m thinking of turning it into a gem, but I need a better name than link_to2, any ideas?

  • Ruby on Rails has a special object called flash which hold its contents for one more request. It’s particularly useful to show messages after a redirect. Since it’s good style to redirect after each succesful form post, that’s where you put the messages such as: “You’ve logged in”, “Thank you for your feedback”, “The book has been added”, etc.

    This flash object looks like a hash table. I normally use two items in there: notice, for good stuff and errors, for bad stuff. I want these messages to be displayed in all pages, so whenever something bad or good happens, I just drop stuff in the notice or error and forget about it. This is trivial to do, I just put this:

    <% if flash[:error] -%>
      <p class='error'><%=h flash[:error] %></p>
    <% end -%>
    <% if flash[:notice] -%>
      <p class='notice'><%=h flash[:notice] %></p>
    <% end -%>

    on the application layout.

    The problem with doing that is that it doesn’t look nice. I expect error messages and success messages to be near the forms or UI elements I’m interacting with. That means that every view or page will put it in a different location. No problem, you just add that same snippet where you want the messages to appear.

    Then there’s a second problem: you get messages twice. If you remove them from the application layout, then you have to remember to put in absolutely every view, even those that you don’t expect to never show an error or notice. I don’t trust myself to always remember to do anything, so what happens is that I can’t just drop something in the flash and expected it to be show, I have to check every time.

    I’m doing this in for a pet projects with less than 10 views, but I think big. I think of the project having 100 views and three coders than don’t know all the implicit rules, like adding the display message snippet to every view they create.

    I’ve came up with this solution. I created a partial view with this content:

    <% if not @messages_rendered -%>
      <% if flash[:error] -%>
        <p class='error'><%=h flash[:error] %></p>
      <% end -%>
      <% if flash[:notice] -%>
        <p class='notice'><%=h flash[:notice] %></p>
      <% end -%>
    <% end -%>
    <% @messages_rendered = true -%>

    That partial view is rendered from the views and also from the application layout, but it displays the messages only once. Thankfully Rails renders the partials inside the views first, so that the messages gets displayed according to the view, and if the view didn’t display them, the application layout will.

  • For showing what music I like, keeping track of what music I listen to, discovering new music and finding people with the same tastes I use last.fm. For doing that but with books I use aNobii. Is there anything like that for movies? If not, there’s a market.
  • Docking stationThe post-PC era is when we stop having PCs because we move to something else. You may think that’s unlikely and unrealistic but look at the evidence. At one time we had desktop computers and laptops started to appear. They were just toys for people with lots of money, then they became the second computer of people that spent a lot of time on the go, today most people own a laptop instead of a desktop computer.

    The exodus from the PC is not going to be that easy, because the mobile devices are more different to a laptop than laptops were to desktop computers. But it’s not only leaving PCs for smart phones, also for netbooks. I believe it’s going to happen. Probably not as extreme as the PC to laptop but it’s going to happen. We’ll be using our phones as our primarly way to access data and communicate. And when we come home we’ll plug it in a dock station -that already exists-, so that it can use our nice big speakers -that also exists- and so that we use an external keyboard -that exists in many cases- and a big external screen -that exists, at least in the netbook market-.

    What doesn’t exist yet, I believe, is external processing. When I’m at a bar I won’t play Halo and I’m OK if switching between applications is slow, but when I come home I want my device to become faster. I have seen absolutely no progress at all in being able to add processing power to a machine, to a portable machine. The closer I’ve seen were docking stations, probably IBM, which added better sound and video cards. Sharing processing power is hard, but I think we need it to go mobile.

    Reviewed by Daniel Magliola. Thank you!