Ruby Code Duplication Detection with Flay

Flay is terribly useful, but has terribly usability …

If the repo was not such a mess I’d make PRs to fix it, but tests are not even runnable and PRs to make the Readme readabe got rejected … so I’m not going to bother … a small excerpt from Samson
More config options can be found on it’s homepage
Just wanted to share this useful rake task which found a bunch of duplication and already prevented me from adding new duplication twice 🙂

desc "Analyze for code duplication (large, identical syntax trees) with fuzzy matching."
task :flay do
  require 'flay' # do not require in production
  files = Dir["{config,lib,app/**/*.{rb,erb}"]
  files -= [
    # Things you want to ignore
  flay =[*files, '--mass', '25']) # mass threshold is shown mass / occurrences
  abort "Code duplication found" if

Omniauth / OAuth integration test with webmock

Took me a while to figure out all the stubs … maybe this is useful later 🙂
I’m using this to test our facebook / twitter signup flows.

def external_redirect(url)
rescue ActionController::RoutingError # goes to
  current_url.must_equal url
  raise "Missing external redirect"

it "signs up" do
  twitter_id = 12345
  visit "/"
  stub_request(:post, "").
    to_return(body: "oauth_token=TOKEN&oauth_token_secret=SECRET&oauth_callback_confirmed=true")

  external_redirect "" do
    click_link "Continue with Twitter"

  stub_request(:post, "").
    to_return(body: "oauth_token=TOKEN&oauth_token_secret=SECRET&user_id=#{twitter_id}&screen_name=twitterapi")

  stub_request(:get, "").
    to_return(body: %{{"profile_image_url_https": "image_normal.png"}})

  visit "/auth/twitter/callback"

How Stop Autotest From Running After Each Failed Test Was Fixed

Autotest until recently only had one flaw: it could not be used for large test suites, since after each red-green cycle I had to wait x minutes for all tests to pass, which made autotest really frustrating.

So grep autotest (the ‘without ZenTest version’) from github and enjoy “autotest -c” (also works with auospec).

And remember kids: always run autospec with script/spec_server and its twice the fun 😉

Cucumber vs ActiveRecord 2.3 — Attempt to call private method (NoMethodError)

All of the sudden some columns began to misbehave in cucumber tests, claiming that “Attempt to call private method (NoMethodError)” on normal column attributes!

A simple fix for this, essentially saying that a method is not private when its a column:

#features/support/ar_private_fix.rb required from env.rb
unless ActiveRecord::Base.methods.include?('private_method_defined_with_fix?')
  class ActiveRecord::Base
    class << self
      def private_method_defined_with_fix?(method)
        return false if columns_hash.keys.include?(method.to_s)
      alias_method_chain :private_method_defined?, :fix