The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Hirb Tips For Rails

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Gabriel Horner

Posts: 62
Nickname: cldwaker
Registered: Feb, 2009

Gabriel Horner is an independent consultant who can't get enough of Ruby
Hirb Tips For Rails Posted: Sep 7, 2009 10:52 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Gabriel Horner.
Original Post: Hirb Tips For Rails
Feed Title: Tagaholic
Feed URL: http://feeds2.feedburner.com/tagaholic
Feed Description: My ruby/rails/knowledge management thoughts
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Gabriel Horner
Latest Posts From Tagaholic

Advertisement

Hirb has been getting the attention of Rails users with Ryan Bates’ screencasts. To address these newcomers I’ll share some basics on setting up, customizing and configuring hirb in the context of Rails’ script/console. Non-Rails hirb users may find some useful tips here as well.

Setting Up script/console

So you’re digging hirb and you want to have it autoload in script/console…

To do this for a local Rails app, append this to config/environment.rb:

  if $0 == 'irb'
    require 'hirb'
    Hirb.enable
  end

And if you just want it in all your Rails’ script/console sessions, drop this in your ~/.irbrc:

  if ENV['RAILS_ENV']
    require 'rubygems'
    require 'hirb'
    Hirb.enable
  end

Of course, these script/console setups work for any other gems you want to autoload. You may also want to try using/adding these gems into your script/console setup: console_update and alias.

Configuring Hirb

Part of what makes hirb nice is that it just automagically works with Hirb.enable. But what happens when your columns are smushed and irrelevant columns are displayed? Just configure hirb. Hirb can be configured via config/hirb.yml or by passing a config directly to Hirb.enable.

For example, if you have a model Url and you only want the columns id, name, description and tag_list to show up, you could make this config file:

  :output:
    Url:
      :options:
        :fields:
          - id
          - name
          - description
          - tag_list

Or you could pass this same config without a config file:

  Hirb.enable :output => {
    "Url"=>{
      :options=>{
        :fields=>%w{id name description tag_list}
      }
    }
  }

Some additional points:

  • There are a slew of other options you can pass to :options. For example if you want to have vertical tables, try the :vertical option. If you want to format certain column strings after they’ve come from Rails, use the :filters option.
  • To pass additional models in the above config, you would put them under :output.
  • In the above config example, tag_list is actually not a column. Rather it’s a method that returns associated tag objects that have been stringified. So yes, a table’s fields can be any object method. If a method doesn’t return a string, customize its stringification with the :filters option.

Customizing Hirb

The other day someone private messaged me about wanting to see the model name with each table’s description. Here’s what I came up with:

Basically I subclassed hirb’s ActiveRecord helper class and overrode the necessary methods. To use this custom helper class you would need to place it before Hirb.enable and then configure hirb:

  Hirb.enable :output=>{
    "ActiveRecord::Base"=>{:class=>:my_active_record_table, :ancestor=>true} 
  }
  # :class could also have been "Hirb::Helpers::MyActiveRecordTable"

Seeing how simple it is to create and use your own custom hirb helpers, it should be fairly easy to use hirb with other non-Rails ORMs i.e. DataMapper or Sequel. Simply subclass Hirb::Helpers::ObjectTable and make some reasonable defaults as Hirb::Helpers::ActiveRecordTable does.

If you want tweak Hirb more for your script/console experience, don’t forget to read the docs!
Stay tuned for more script/console goodness

Read: Hirb Tips For Rails

Topic: Using p4merge with Git Previous Topic   Next Topic Topic: Planting the seeds

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use