beforeRunningCommand nop bundleUUID 5C3E29B9-B473-457E-A850-9499F1FD783C command #!/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 tbl_name = File.basename(ENV['TM_FILEPATH'], '.yml') end rails_dir = $` class MysqlRetriever def initialize(tbl_name, conf) require 'mysql' @tbl_name = tbl_name @db = Mysql.connect *conf.values_at(*%w[host username password database]) end def table_exists? r = @db.query "show tables like '#@tbl_name'" r.fetch_row end def columns res = @db.query "show fields from #@tbl_name" res.extend Enumerable res.map{|r| r[0]} end end class ActiveRecordRetriever def initialize(tbl_name, conf) require 'rubygems' require 'active_record' eval "class #{tbl_name.classify} < ActiveRecord::Base; end", TOPLEVEL_BINDING @model = tbl_name.classify.constantize ActiveRecord::Base.establish_connection conf end def table_exists? @model.table_exists? end def columns @model.columns.map{|clm| clm.name} end end require 'yaml' conf = YAML.load(File.read(rails_dir + "/config/database.yml")) db_conf = conf['development'] retriever = db_conf['adapter'] == 'mysql' ? MysqlRetriever.new(tbl_name, db_conf) : ActiveRecordRetriever.new(tbl_name, db_conf) unless retriever.table_exists? TextMate.exit_show_tool_tip "there is no table called #{tbl_name}" exit 1 end columns = retriever.columns puts '${1:fixture_name}:' idx = 2 columns.each do |c| puts " #{c}: ${#{idx}}" idx += 1 end puts '$0' fallbackInput none input none name rails: new fixture output insertAsSnippet scope source.yaml tabTrigger fix uuid ED98F583-776D-4A72-A3E3-0C79E8F30F90