OSX + Vim + CTags (Exuberant) for fast context switching in large projects

Installing CTags (Exuberant)

Lets first install ctags-exuberant using Homebrew

brew install ctags-exuberant

Remember the path that ctags got installed to, with version 5.8 on my machine it was in:

/usr/local/Cellar/ctags/5.8/bin/ctags

Setting up Vim/MacVim

Download the TagList plugin from VimOnline.

In your .vimrc file add the following:

let Tlist_Ctags_Cmd='/usr/local/Cellar/ctags/5.8/bin/ctags' 

let g:Tlist_Ctags_Cmd='/usr/local/Cellar/ctags/5.8/bin/ctags' 

fu! CTagGen()
    :execute "!" . g:Tlist_Ctags_Cmd .  " -R ."
endfunction

nmap <silent> :ctg :call CTagGen() 

Open up vim/MacVim, and type

:ctg 

You can then go to a controller for example:

Type in:

:Tlist

And the follow should appear.

ctag list

Lets say I've got my cursor on StoryType and I want to go to the model, I can just hit Ctrl+] to get there. You can now do this for any method (helpers, methods, anything thats in your ctags file!).

Cucumber and Mysql2::Error Lock when deleting records

I was writing some cucumber features for reru_scrum when I ran into an issue with destroying user records and Mysql2 throwing a Lock error.

The full error:

Mysql2::Error: Lock wait timeout exceeded; try restarting transaction: UPDATE `users` SET `last_sign_in_at` = '2011-11-22 00:06:32', `current_sign_in_at` = '2011-11-22 00:11:28', `sign_in_count` = 3, `updated_at` = '2011-11-22 00:11:28' WHERE `users`.`id` = 1

A simple solution is to use the database_cleaner gem.

Inside your features/support/env.rb file:

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'
  DatabaseCleaner.strategy = :truncation
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

A good idea is to create the before and after hooks to use the DatabaseCleaner.start and DatabaseCleaner.clean methods.

Inside features/support/hooks.rb:

Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end

You should then be able to run your features and have your database cleaned between steps.

Rails: bundler installing into a directory

If you run into an issue with bundler always installing into a directory then you may have accidentily run:

bundle install foobar

and now its installing into foobar.

You can run:

bundle install --system

To go back to installing gems to the system/RVM path.

Coffeescript onclick referencing this not working

When defining a event listener for objects in Coffeescript you nede to make sure you use a -> -- using a => will result in any references to attr() be "undefined"

Here is an example of some correct on click bindings that use the attr() methods

  jQuery ($) ->
  $('[id^=story_]').click ->
    $( "#" +  $(this).attr("id") + "_loader")
      .load('/projects/' + $(this).attr('project_id') +
      '/story_types/' + $(this).attr('story_type_id') +
      '/stories/'+ $(this).attr('story_id') + '/tasks/new')

Rails: Rendering a view in a model to generate a pdf

class RenderHelper
  class << self
    def render(assigns, options, request = {})
      request = { 
        "SERVER_PROTOCOL" => "http", 
        "REQUEST_URI" => "/",
        "SERVER_NAME" => "localhost", 
        "SERVER_PORT" => 80
      }.merge(request)

      av = ActionView::Base.new(ActionController::Base.view_paths, assigns)

      av.config = Rails.application.config.action_controller
      av.extend ApplicationController._helpers
      av.controller = ActionController::Base.new
      av.controller.request = ActionController::Request.new(request)
      av.controller.response = ActionController::Response.new
      av.controller.headers = Rack::Utils::HeaderHash.new

      av.class_eval do
        include Rails.application.routes.url_helpers
      end

      av.render options 
    end
  end
end

Usage

 html_output = RenderHelper.render({:instance_variable1 => "foo", 
                                                :instance_variable2 => "bar"}, 
                                                :template => 'view_to/render')

You can then use you're favorite PDF generator (I use PDFKit) to take the html output and parse it to a PDF.

Read More in the Archive