Feature testing mobile variants

For a project, we wanted to write a feature spec for the mobile variant of the site. Instinctively, the first thing I did was google. I found nothing. The next thing I did was think. I came up with this, which worked:

require 'rails_helper'

feature 'Mobile variant' do
  before do
    allow_any_instance_of(ActionDispatch::Request).to receive(:variant).and_return([:mobile])
  end

  scenario 'Look at customer information' do
    # your test here!
  end
end

Happy testing!

Filter Rails SQL log in production

In order to debug a problem, which only occurred in production, we recently wanted to tweak our Rails SQL logs to only show the access to a specific table.

Here’s what we did to accomplish this. We created a file initializers/filter_sql_log.rb with this content:

if Rails.env.production?

  module ActiveRecord
    class LogSubscriber
      alias :old_sql :sql

      def sql(event)
        if event.payload[:sql].include? 'users'
          old_sql(event)
        end
      end
    end
  end

end

This monkey-patches the ActiveRecord::LogSubscriber class and only delegates to the old logging method, if the SQL statement includes the string "users".

By default, SQL logging is deactivated in the Rails production environment. Therefore we needed to change config/environments/production.rb like this:

config.log_level = :debug