China, Day 3 – Guilin

Posted in General | Tagged , | Leave a comment

China, Day 2 – The Forbidden City

Posted in General | Tagged , | Leave a comment

China, Day 1 – Beijing Temples, Drum and Bell Towers, & Olympic Complex

Waiting for text from Bill

Posted in General | Tagged , | Leave a comment

The Dune Series

I’ve been on a sci-fi kick recently (the last year or so).

Last year I started with A Stranger in a Strange Land (Complete Version) by Robert A. Heinlein. It was really out there, but had a definite profound impact on me. Parts were uncomfortably preachy and many of the topics preached on were dated, but some were still fairly liberated by current standards.

Then I read A Wrinkle in Time and A Wind in the Door by Madeleine L’Engle. I liked the stories, but wanted a bit more detail. I understand that they were written for children and that aspect was fun, but I still wanted a bit more.

Then on to Ender’s Game by Orson Scott Card. A very quick read. Fun and able to keep my interest. I wanted to read the next two books in the series, but figured I could get back to them.

Then back to another Heinlein with Starship Troopers. Again, fairly preachy. I wouldn’t say I got a direct pro war sentiment from it, but it probably makes a great tool for the Marines whom it appears to be modeled after. Difficult to put words on right now.

Then I read Dune. I instantly got sucked in and jumped to Dune Messiah and then to The Children of Dune. Now I’m 10 pages from the end of God Emperor of Dune, and depending on how these 10 pages go, I may or may not go straight on the the final two books in the Dune series. Probably will finish the series though. Part of me thinks that if I don’t finish the series now, I may not get back to it, and even though I’ve had an extremely large dose of Melange so far, it’s awesome enough for me to finish. So far, the 4th book seems to be the most different and potentially the most long winded. At this point, all of the concepts created and touched in the first three books feels completely evolved. It makes me wonder how many of these ideas were already in Frank Herbert’s mind when he wrote the first book. It all seems to tie together so well, that it feels like most of it was there by at least the second book. Anyway, good stuff. I’ll let you know what I think of the last ten pages.

Posted in General | Tagged , , | Leave a comment

CA DMV

I’ve been living in my current appartment for about three years. When I moved here, I called the DMV regarding updating the address on my license. The first person I got on the phone told me that they don’t send out new licenses for just address changes anymore. Instead I need to write down my new address on a piece of paper and keep it in my wallet next to my license. Really…? I called back to talk to someone else. This couldn’t be for real. The next person confirmed that this was the policy, and that if I was worried, she could mail me an “official” piece of paper to write my new address on. I went for it.

A few days later, I got an envelope with three small brown pieces of paper with a place for me to write my name, driver’s license ID number, and address on it. It eventually wore out and the other ones got lost. Whatever.

Fast forward to now. My license is about to expire. I need to renew it. I check out the DMV website. You can renew online. Nice. Also you can change your address online. Nice. You can’t change your address online and renew at the same time. Not Nice.

It turns out they have an online scheduling system for appointments. I make one. It’s for Tuesday at 9:05 AM. I arrive and see that it’s already pretty busy. There are lines with 10 or 15 people in them all over the place. I look to the ceiling for signs to determine where to go. I see a person sitting behind a half height cube wall with no line at all. The sign above reads “Appointments”.

Long story short. I was in and out in under 8 minutes. At no point did I wait in any line.

There still seems to be a massive amount of inefficiency, in that their process required me to go to four different people at different desks at various locations, one of whom incorrectly checked the box (marked admin use only) for ID card instead of driver’s license, another who later incorrectly verified that information. It wasn’t until I had to pay that the woman was very upset that ID card had been checked because an ID card costs $4 less that a drivers license.

Posted in General | Tagged , , | 2 Comments

Ruby sort by descending domain

Here’s a fun and simple way to sort an ActiveRecord of domain names by subdomain, then domain.

ie.

  • server1.billzajac.com
  • server1.blog.billzajac.com
  • server2.blog.billzajac.com
  • server1.williamzajac.com
  • Here’s what I came up with for rails:

    def create_servernameslist
        find_servernames = Server.find(:all).sort_by {|a| a.name.split(/./).reverse.each {}}
        @sorted_servernames = find_servernames.collect {|s| [s.name, s.id]}
        @servernames = {}
        find_servernames.each {|s| @servernames[s.id] = s.name}
    end

    Here’s an even simpler example for just an array in ruby. I’m pasting from irb to show that it works.

    irb(main):001:0> fqdns = ['a.c.com', 'c.com', 'b.c.com', 'b.b.c.com', 'a.b.c.com']
    => ["a.c.com", "c.com", "b.c.com", "b.b.c.com", "a.b.c.com"]
    irb(main):002:0> fqdns.sort_by {|a| a.split(/./).reverse.each {}}
    => ["a.c.com", "c.com", "b.c.com", "b.b.c.com", "a.b.c.com"]
    irb(main):003:0>
    Posted in General | Tagged , , | 1 Comment

    ActiveResource and Basic HTTP Auth

    Rails 2.0 is pushing Basic HTTP Auth. This is a great simple way to add auth to your app. But with ActiveResource 2.0.2, you define your site argument with your username and password in your URI.

    But…
    If the password contains any non-alphanumeric chars such as an @ or a /, this will cause problems with the way URI (uri.rb) wants to split your URI.

    After losing a lot of hair to this problem by trying to url escape the password or the entire url, I looked at the code to find out that you can specify site as either a string such as:

  • http://username:password@site.url/controller
  • or

  • a uri object (in which you define your variables specifically)
  • newuri = URI::HTTP.build(["username:password", "site.url", nil, "/controller", "query", 'fragment'])

    See: http://www.ruby-doc.org/core/classes/URI/HTTP.html

    Posted in General | Tagged , , | Leave a comment

    Making ActiveResource find work with your controller

    By default, ActiveResource find wants to search your controllers with query string params.

    For example:

    Person.find(:all, :params => { :title => "CEO" })

    Would hit the URL:

  • /people?title=CEO

    To make this work, you’ll need to add something like the following to your controller:

    # Use search conditions by search params for column names. 
    # This allows urls like "contacts/list?company_id=5".
      def collection
        conditions = conditions_from_params
        @collection ||= end_of_association_chain.find(:all, :conditions => conditions)
      end
     
    # Builds search conditions by search params for column names.
      def conditions_from_params
        conditions = nil
        params.reject {|key, value| [:controller, :action, :id].include?(key.to_sym)}.each do |key, value|
          next unless model.column_names.include?(key)
          if value.is_a?(Array)
            conditions = merge_conditions(conditions, ["#{model_name.to_s.pluralize}.#{key.to_s} in (?)", value])
          else
            conditions = merge_conditions(conditions, ["#{model_name.to_s.pluralize}.#{key.to_s} = ?", value])
          end
        end
        conditions
      end
     
      def merge_conditions(*conditions)
        c = conditions.find_all {|c| not c.nil? and not c.empty? }
        c.empty? ? nil : c.collect{|c| model.send(:sanitize_sql, c)}.join(' AND ')
      end
  • Posted in General | Tagged , , | 3 Comments

    Using xml with the resource_controller plugin

    If you’re using an ActiveResource client to access your Rails controller which is using the awesome resource_controller plugin RESTfully you’re going to need to enable xml respsonses (which aren’t enabled by default with the plugin).

    class ProductsController < ResourceController::Base
    # Setup XML responses
      index.wants.xml { render :xml => @products }
      show.wants.xml { render :xml => @product }
      new_action.wants.xml { render :xml => @product }
      create.wants.xml { render :xml => @product, :status => :created, :location => @product }
      create.failure.wants.xml { render :xml => @product.errors, :status => :unprocessable_entity }
      destroy.wants.xml { head :ok }
      update.wants.xml { head :ok }
      update.failure.wants.xml { render :xml => @product.errors, :status => :unprocessable_entity }
    # Response doesn't make sense for edit action
    end

    Two things to note.

  • First, I essentially copied this out of the default scaffold generated by rails 2.0.2.
  • Second, note that the action for new is called new_action. James Golick decided to name it that instead of new to avoid trouble with the word new.
  • Posted in General | Tagged , , , , , | 2 Comments

    Rails REST Associations

    Rails 2.0 comes with a lot of REST goodness baked in, but without a little extra work, you don’t get all of the nice URL accessible associations that you want.

    There’s a plugin called resource_controller by James Golick which will ease the setup of the associations in your controllers.

    There are a couple of great screencasts by Akita which show how to manually setup the RESTful associations and how to use the resource_controller plugin.

  • How to setup your associations with resource_controller
  • How to Manually setup your associations
  • Posted in General | Tagged , , , | Leave a comment