A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails and iOS developer.

Navigation

A Fresh Cup

Notes on Rails and other development

Friday
Nov072008

Double Shot #328

Well, I still have hours to sell, but a couple of little projects appear to be coming together, so hopefully I won't be on the bench for too long.

  • Scaling Ruby - The latest EnvyCast. I heard some good things about this material being presented at RubyConf.

  • Rubular - Ruby regular expression editor online.

  • Bort Update to Rails 2.2 - This prepackaged Rails solution, including various bells and whistles, is in active development.

  • A few reasons braid is better than 40 lines of Rake. - After wrestling with a rake-based git subtrees solution for a while, I'm getting inclined to agree that there must be a better way.
  • Wednesday
    Nov052008

    Customizing to_xml and to_json in Rails

    You probably already know (well, if you're a Rails developer) that you can use to_xml or to_json to quickly get XML or JSON representations of Active Record model instances. But did you know that these methods are configurable? By default they simply dump all of the attributes of the model along with their values, but if you want to do something different, you can - and usually without overriding the base methods.

    To start, you can specify exactly which attributes to export with the :only or :except options:

    [sourcecode language='ruby']
    @user.to_xml :only => [ :name, :phone ]
    @user.to_xml :except => :password
    @user.to_json :only => [ :name, :phone ]
    @user.to_json :except => :password
    [/sourcecode]

    You can include associated records, nesting as needed, with the :include option:

    [sourcecode language='ruby']
    @user.to_xml :include => {:orders =>
    { :include => [:shipments, :backorders] }}
    @user.to_json {:orders =>
    { :include => [:shipments, :backorders] }}
    [/sourcecode]

    :only and :except also work on includes:

    [sourcecode language='ruby']
    @user.to_xml :include => {:orders =>
    { :include => [:shipments, :backorders] },
    :only => :order_date }
    @user.to_json {:orders =>
    { :include => [:shipments, :backorders] },
    :only => :order_date }
    [/sourcecode]

    You can create XML or JSON attributes from model methods by using the :methods option:

    [sourcecode language='ruby']
    @user.to_xml :methods => :permalink
    @user.to_json :methods => :permalink
    [/sourcecode]

    Additionally, there are some options that apply only to to_xml. :skip_instruct suppresses the XML processing instruction. :skip_types suppresses the output of types to the XML. :dasherize => false turns off dasherization of column names.
    Tuesday
    Nov042008

    Double Shot #327

    I've shut down and reformatted my Windows desktop for good (I hadn't turned it on for six months or so, it just took me this long to get around to reformatting the drives). If anyone wants a deal on a Dell PowerEdge 1800 server before I EBay it, holler.

  • We've stopped using rSpec ... - And ignited a bunch of debate in comments. Caboose has bailed out; the comment chain is interesting for a glimpse of what testing solutions the community is using.

  • Google Analytics plugin - Finally got around to making some doc and feature improvements to my fork of this project.

  • giternal - An alternative for managing git externals in your Rails project. Or any other project, for that matter.

  • Tuesday
    Nov042008

    Generating Proper rdoc for cattr_accessor Declarations

    If you take a look at the Rails source code, you'll find numerous useful comments like this one from ActionController::Base:

    [sourcecode language='ruby']
    # All requests are considered local by default, so everyone
    # will be exposed to detailed debugging screens on errors.
    # When the application is ready to go public, this should be set to
    # false, and the protected method local_request?
    # should instead be implemented in the controller to determine
    # when debugging screens should be shown.
    @@consider_all_requests_local = true
    cattr_accessor :consider_all_requests_local
    [/sourcecode]

    But if you check your favorite Rails API site for documentation of consider_all_requests_local, you'll come up blank. What's up?

    I spent some time chasing this, and it turns out to be a conflict between the way that Rails is commented and the way that rdoc thinks things should be done. There's actually a ticket in the old Rails Trac with a proposed resolution for this. As it happens, that ticket isn't quite right, but it provoked rdoc into changing things. The secret lies in the rdoc 2.x support for documenting metaprogrammed methods.

    To properly document cattr_accessor (and similar) declarations in your own Rails code, you need to do two things. First, upgrade from your musty old rdoc 1.0.1 to a more recent version - 2.2.1 is current. If you look at their downloads you will find there is a gem version, but just installing this may not be enough: putting the gem on my system gave me rdoc 2 from the command line but rdoc 1 from rake tasks. That's because (at least on my Mac), rdoc is also out there in the ruby/1.8 standard tree, and so I had to replace the 1.0.1 there with the new bits.

    Second, you need to change your markup comments to tell rdoc that this is a metaprogrammed method. Here's the revision for that method from ActiveController::Base:

    [sourcecode language='ruby']
    @@consider_all_requests_local = true
    ##
    # :singleton-method:
    # All requests are considered local by default, so everyone
    # will be exposed to detailed debugging screens on errors.
    # When the application is ready to go public, this should be set to
    # false, and the protected method local_request?
    # should instead be implemented in the controller to determine
    # when debugging screens should be shown.
    cattr_accessor :consider_all_requests_local
    [/sourcecode]

    The ## indicator tells rdoc that this is a metaprogrammed method, which means it will ignore the first token on the declaration and pick up the second one as the method name. The # :singleton-method# indicator tells rdoc to document this as a class method rather than as an instance method.

    The Rails Documentation team is exploring how we'll fix up the core Rails source to use the new markers. Meanwhile, you should start using this anywhere that you have the cattr methods in your own code or plugins - and upgrade your rdoc bits in anticipation.
    Tuesday
    Nov042008

    Double Shot #326

    Yesterday saw my first posting to the official Rails weblog. A nice step on the way to world domination, I guess.

  • Gerrit and Repo, the Android Source Management Tools - Google has built some tools to make git work better for large-scale projects, including workflow and code-review bits.

  • RubyMine Public Preview - JetBrains is getting into the Rails IDE business. I may take a look, though honestly, two years after closing the IDE I don't miss it.

  • GitHub Code Search - A bit of poking around here reveals that Ruby coders pretty much have a lock on the chunky bacon market.

  • Life on the Edge with Merb, DataMapper, and RSpec - Work-in-progress aimed for folks who might be thinking of switching from Rails.

  • ActiveSupport::Rescuable - Pratik Naik shows how to mix this into your own code with Rails 2.2.

  • 40 Beautiful Free Icon Sets - Some nice stuff out there; be sure to check the fine print before using.