08 May 2014

Bash prompt with git branch

the simple way

Stephen is well aware most of us already know about this (or use oh-my-zsh with already prepared themes). Here is what he puts in his .bash_profile

Show git branch name at you terminal

# get current branch
function git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
    echo "("${ref#refs/heads/}") ";
}
PS1="[\[\033[1;32m\]\w\[\033[0m] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0m\]$ “
01 May 2014

Selective Zeus

targeting thunder

Stephen is very happy with Zeus for speeding up his testing on OpenApply. But he also like to use tags in his tests to selectively launch specs collections rather than the whole thing:

context 'enrollment', for_test: true do
    xxx
  end

and then

zeus rspec —tag=for_test
01 May 2014

Upgrading to Rails4

because it's more than time

iHower wanted to point out 2 talks from rubyconf we attended last week, that report about rails upgrade:

01 May 2014

SSH to socket

open once, use forever

Mose uses a special trick because he’s often ssh’ing all over the place. Add in your .ssh/config

Host *
  ControlMaster auto
  ControlPath /tmp/%r@%h:%p

It will save your first connection to a host as a socket in /tmp and then all subsequent ssh connections to the same host are open instantly because there is no key renegotiation on the way. The side effect is that the child-connections cannot be closed until the first one closes, but I find it convenient because it tells me that I still have a console open on the server that I should close.

01 May 2014

rspec-given

like a cucumber without the cucumber

Ilake wanted to share especially with the rails4 guys that are this time doing it right about testing, that in TPE we use rspec-given all over the place.

It gives a clear view on which part prepares data, what you try to test and what you expect. It can be mixed with traditional rspec syntax (like with before { RecordingMailer.stub(:recording_email) } for stubbing).

01 May 2014

Requirejs-rails

revived

AaronH is pretty happy about the way requirejs is organized on kb and isis, after long efforts.

He forked the requirejs-rails gem to upgrade it, as it was unmaintained, on https://github.com/aar0ntw/requirejs-rails

It mainly enables to integrate requirejs in the rails4 assets pipeline, by pre-compiling files with r.js. If you want to know more about it, feel free to bug him. And, be nice, go star his repo !

01 May 2014

Random User

get some faces

Oliver really like that Photoshop extension.

The http://randomuser.me website generates random user profiles for filling up user data on html mockups, like the lorem ipsum for users.

But there is also that photoshop extension for including random user photo on images http://randomuser.me/extension it’s damn useful and better than default gravatars.

01 May 2014

RabbitMQ Receipts

avoid getting lost in the rabbit hole

AaronK recently faced the need to implement a way so that each message from the message queue tells the sender if the message was delivered. He had very interesting reading about it. He works now on getting the sync status that way:

01 May 2014

Array to Ostruct

monkey patching

Naiting is doing front-end but he’s leveling up in ruby coding, and he found this monkey patching especially useful in the context of testing:

class Array
      def to_os
        self.map{ |x| OpenStruct.new(x) }
      end
    end

Then rather than

@subjects= [
      OpenStruct.new(:type => 'academics', :title => 'Language A', :icon => 'language_a'),
      OpenStruct.new(:type => 'academics', :title => 'Second Language', :icon => 'language_b'),
      OpenStruct.new(:type => 'academics', :title => 'Experimental Sciences', :icon => 'sciences')
    ]

you can

@subjects = [
      {:type => 'academics', :title => 'Language A', :icon => 'language_a')},
      {:type => 'academics', :title => 'Second Language', :icon => 'language_b')},
      {:type => 'academics', :title => 'Experimental Sciences', :icon => 'sciences')},
    ].to_os

or even, have fake data stored in a yaml file

@subjects = YAML.load_file("subjects.yml').to_os
25 April 2014

Database partial cleaner

for faster tests

On Rails 4 side, I would like to share the code which helps to speed up the tests and resolves the test DB initialization issue. Before running a test, there is the test DB setup action, which clears all tables. But if your test modifies only several tables, it is much better to clear and populate only these tables vs all.

There is a gem for that: https://github.com/bmabey/database_cleaner, which we use and the examples below are related to it.

To find the tables which got some data after the previous test, the next code can be used:

strategy = DatabaseCleaner.connections.first.strategy
connection = strategy.connection_class.connection
db_name = connection.current_database
only = connection.execute("select table_name from information_schema.tables where table_rows >= 1 and `TABLE_SCHEMA` = '#{db_name}';").to_a.flatten
strategy.instance_variable_set("@only".to_sym, ['schools'] + only.reject { |x| x.to_s == ActiveRecord::Migrator.schema_migrations_table_name })

Thanks to @only variable, the cleaner will omit the tables which do not have data. Also, we should never drop schema_migrations table, which is reflected in the reject statement.

However, the above code has one problem - the query may lie to us sometimes, because table_rows value may be inconsistent with the actual table content. To be 100% safe, we need to narrow the optimization to the following code (spec/support/database_cleaner.rb):

RSpec.configure do |config|
  config.before :suite do
    DatabaseCleaner.clean_with :truncation
  end

  config.before do
    if example.metadata[:js]
      DatabaseCleaner.strategy = :truncation, { :pre_count => true, :reset_ids => false }
    else
      DatabaseCleaner.strategy = :transaction
    end

    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

We use :truncation for JS tests cleanup, because they may run in parallel, and :transaction is not recommended for the multiple connections case. :pre_count option is used to ensure that we truncate only the tables which have data.

The above gem and this custom configuration improves the tests running time ~2x for us.

Roman