Switching to RubyMine from Netbeans on Ubuntu

I recently did the switch and it was fun, and now I finally got everything working, here are the steps I took:

  • Install rubymine and make a shortcut
  • Use Textmate style shortcuts (so I can work with all the evil textmaters out there :> )
  • Correct Meta key behaviour:
    keyboard > layout options
    choose meta is mapped to left win
  • fuzzy autocomplete / Textmate Esc autocomplete is called “cyclic completion”, find it in keymap and replace it with Esc
  • add ctrl+c/v/x/z/… shortcuts so rubymine behaves like all the other apps

Benefits:

  • great fuzzy file search e.g. “ba/us/in” -> app/backend/users/index.erb
  • Fast autocompletion
  • Nice refactoring with preview
  • Warnings / hints on local / global / unused variables and sane spellchecking
  • Tons of options for whitespace/editor layout
  • Integrated git with visible history
  • “click something and got to its definition” works great

Drawbacks

  • Startup takes ~30s for large app (initial indexing took ~ 4min)
  • To many menu entries, but they can be customized out…

Speeding up Slow Spork Startup for Rails 2.3+

If you noticed spork startup getting slower when switching to Rails 2.3, your not alone ๐Ÿ˜‰

  • Views are eager loaded
  • app/ is eager loaded (when config.cache_classes is on)

Without hack: 18s startup
With hack: 2s startup ๐Ÿ˜€

Try it

#spec/spec_helper.rb
require 'spork/app_framework/rails'
Spork::AppFramework::Rails::NinjaPatcher.class_eval do
  # views are preloaded  spork must be restarted for view changes
  def delay_eager_view_loading
    puts "removed because i am too slow..."
  end

  # do not preload application files
  # alternatively urn off config.cache_classes
  def delay_app_preload
    ::Rails::Initializer.send(:define_method, :load_application_classes){}
  end
end


Spork.prefork do
  ...

Stop spec_helper from being loaded multiple times

If you got a big project, chances are spec_helper is required through different ways, File.expand_path / File.join() / … which results in it being loaded several times!

Try it:

#spec_helper.rb
print "YEP!"

rake spec -> “YEP!YEP!YEP!YEP!YEP!YEP!YEP!YEP!……”

So how to prevent it ?

Unify!
Unify it to ‘spec_helper’ for rspec or ‘./spec/spec_helper’ for minitest, which can be copied without modification (like adding ‘../../’) and prevents duplicate spec_helper calls

#unify_spec_helper.rb
files = Dir["spec/**/*_spec.rb"].reject{|file|File.directory?(file)}
files.each do |file|
  lines = File.readlines(file)
  lines = lines.map do |line|
    if line =~ /require.*spec_helper/
      "require 'spec_helper'\n"
    else
      line
    end
  end
  File.open(file,'w'){|f| f.write lines.join('') }
end

Run: ruby unify_spec_helper.rb

ActiveRecord.find_each_with_order = find_each with order…

I just hate things that fail silently, just like User.order_by(:created_at).find_each

Which will simply ignore the order option….

So here comes the alternative…

class ActiveRecord::Base
  # normal find_each does not use given order but uses id asc
  def self.find_each_with_order(options={})
    raise "offset is not yet supported" if options[:offset]

    page = 1
    limit = options[:limit] || 1000

    loop do
      offset = (page-1) * limit
      batch = find(:all, options.merge(:limit => limit, :offset => offset))
      page += 1

      batch.each{|x| yield x }

      break if batch.size < limit
    end
  end
end

(the ๐Ÿ˜ฎ is : and o)