Entering fixtures with TextMate

Posted on March 15, 2007

Probably the most boring task when writing Rails test cases is entering new fixtures. With a little help from TextMate this process can be made more pleasant.

  • Open Bundle Editor and create a new command.
  • Name it, for example, 'rails: new fixture'
  • Use this script as the command body:

    #!/usr/local/bin/ruby
    require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes"
    unless ENV['TM_FILEPATH'] =~ /\/test/
      TextMate.exit_show_tool_tip  "you're not in the context of a rails project"
      exit 1
    else
      rails_dir = $`
      tbl_name = File.basename(ENV['TM_FILEPATH'], '.yml')
    end
    
    
    require 'rubygems'
    require 'active_record'
    eval "class #{tbl_name.classify} < ActiveRecord::Base; end", TOPLEVEL_BINDING
    model = tbl_name.classify.constantize
    
    
    conf = YAML.load(File.read(rails_dir + "/config/database.yml"))
    db_conf = conf['development']
    ActiveRecord::Base.establish_connection db_conf
    
    
    unless model.table_exists?
      TextMate.exit_show_tool_tip  "there is no table called #{tbl_name}"
      exit 1
    end
    
    
    columns = model.columns.map{|clm| clm.name}
    puts '${1:fixture_name}:'
    idx = 2
    columns.each do |c|
      puts "  #{c}: ${#{idx}}"
      idx += 1
    end
    puts '$0'
    
  • Use None for the Command Input

  • Use Insert as Snippet for the Command Output
  • Activation should be set to Tab Trigger with the fix as the activation sequence.
  • Source Selector must be set to source.yaml

Now, when the command is created, open your fixture file and enter fix followed by Tab key. TextMate will insert the template your new fixture with all column names specified. You can enter values for the first column and move to the next one by pressing Tab key.

New Fixture

This is not really a 'big deal' command, but it surely helps when entering a lot of fixtures.

Also, there is a simpler way of installing this command. Download this command file to your desktop and double-click it to install. The script in this file contains the implementation that is better optimized for Mysql database by avoiding using ActiveRecord library.

Enjoy.


New Fixture
From DB

UPDATE: Another useful command is to create a new fixture from a database record. When using this command it'll prompt you for the record id and then fill all columns with data from the database.

Comments
  1. YoNoSoyTuMarch 19, 2007 @ 04:04 PM

    A small fix for those using SQLite, after the line "db_conf = conf['development']" add this:

    if %w(sqlite sqlite3).include? db_conf['adapter'] dbfile = dbconf.delete('dbfile') || dbconf.delete('database') dbfile = rails_dir + '/' + dbfile if dbfile[0] != ?/ db_conf['database'] = dbfile end

    Nice idea and nice bundle, btw.

  2. Chuck VoseMay 17, 2007 @ 12:11 PM

    The newFixture.tmCommand file doesn't quite match the source of the post. Seems like they both work but thought I would mention it.

  3. JoeMay 19, 2007 @ 08:19 AM

    Nice idea, seeing some issues. My ruby is pointing to the correct area, and I added TM_RUBY env variable as some other sites have suggested.

    /tmp/temp_textmate.itd7UP:13:in /bin/bash: -c:  line 1: unexpected EOF while looking for matching `''
    

    /bin/bash: -c: line 3: syntax error: unexpected end of fileinitialize' from /tmp/temp_textmate.itd7UP:52

    Not quite sure how to debug this.