Wednesday, March 2, 2016

Removing old release branches

After some time release branches piles up and we may want to clean up our Github from old live-* branches

Given we name our release branches live-20150821 (live-yearmmdd) here is an example how to remove all live branches from previous year (Given it's 2016)
  • cd to the repo of your project you want to cleanup
  • create the cleanup.rb with content bellow
  • lunch ruby cleanup.rb
# cleanup.rb
old_live_branches = `git fetch origin && git branch -r | grep live-2015`  # all branches `live-2015*`
old_live_branches
  .split("\n")
  .map(&:strip)
  .map { |i| i.gsub("/", ' :') }
  .each do |destroy|
    # e.g.: git push origin :live-20151129
    puts `git push #{destroy}`
  end



Thursday, October 15, 2015

solving Ubuntu 14.04 Ruby C dependancy

Given you are running Ubuntu 14.04 and you are installing/upgrading some Ruby gems (let sey Rails 4.2.4 gem, or RSpec, ...) and you get this error:
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/tomi/.rvm/rubies/ruby-2.2.3/bin/ruby -r ./siteconf20151015-21730-1qh2hm1.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling generator.c
linking shared-object json/ext/generator.so
/usr/bin/ld: cannot find -lgmp
collect2: error: ld returned 1 exit status
make: *** [generator.so] Error 1

make failed, exit code 2

Gem files will remain installed in /home/tomi/.rvm/gems/ruby-2.2.3@maze_magic/gems/json-1.8.3 for inspection.
Results logged to /home/tomi/.rvm/gems/ruby-2.2.3@maze_magic/extensions/x86_64-linux/2.2.0/json-1.8.3/gem_make.out
An error occurred while installing json (1.8.3), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.3'` succeeds before bundling.
run:
sudo apt-get install libgmp-dev
or
sudo apt-get install libgmp3-dev
should solve the problem.

Monday, August 24, 2015

Rails log - display partials log only

...or: show me only the log of  what is being rendered.
Let say your Rails log is doing to much and you just want to see which partials or layouts are beeing rendered.
cd ~/my-rails-application
tail -f log/development.log                     # entire log as it is

tail -f log/development.log | grep -e 'Render'  # just what is rendered
                                                # partials / layouts
This can be anything you want
tail -f log/development.log | grep -e 'anything'

mirror:
  • https://github.com/equivalent/scrapbook2/blob/master/archive/mini-blogs/2015-08-24-rails-log-only-partials.md

Wednesday, July 22, 2015

how to include Capybara RSpec matchers in RSpec

let say you want to use have_content and have_selector in non rails-helper spec
# Gemfile
gem 'capybara'
gem 'rspec'
# spec/spec_helper.rb
#...
require 'capybara/rspec'
#...
require 'spec_helper'

RSpec.describe MyPresenter do
  include Capybara::RSpecMatchers

  it do 
    expect("<b>abc</b>").to have_selector("b")
  end
end
raw: https://github.com/equivalent/scrapbook2/blob/master/archive/mini-blogs/2015-07-22-capybara-matchers-in-rspec.md

Wednesday, June 17, 2015

Rails mysql gem (>= 2.9.0) throwing errorchecking for mysql_query() in -lmysqlclient... no

My settup:
  • Ubuntu 14.04
  • Rails 4.2.x
  • Ruby 2.2.0

Error:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/tomi/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20150617-4359-1xygr4t.rb extconf.rb 
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/tomi/.rvm/rubies/ruby-2.2.0/bin/$(RUBY_BASE_NAME)
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /home/tomi/.rvm/gems/ruby-2.2.0@rails-framework-4.2/gems/mysql-2.9.1 for inspection.
Results logged to /home/tomi/.rvm/gems/ruby-2.2.0@rails-framework-4.2/extensions/x86_64-linux/2.2.0/mysql-2.9.1/gem_make.out

solution:

sudo apt-get install  libmysqlclient-dev
if not try
 sudo apt-get install mysql-server

Thursday, April 23, 2015

Use Rails (ActiveSupport) delegation class in plain ruby

# Gemfile
source "https://rubygems.org"
gem 'active_support'
require 'active_support/core_ext/module/delegation'

class Foo
  delegate :call, to: :other

  def other
    ->(){ 'foo' }
  end
end

Foo.new.call
# => 'foo'

Tuesday, March 31, 2015

Run multiple instances of RSpec on same machine

Sime times you are stuck with realy badly written Rails project (or other application which comunicates with Database) which entire test suite takes ages to run. You want to run all the tests but you while they running you want to continue your TDD work.
If you run RSpec multiple times (multiple instances at a same time) you may break one of test suits because you will write data to same database both RSpec instances are comunicating with.
One way to handle this is if you run rspecs on different databases
# config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  reconnect: false
  pool: 5
  username: root
  password: my_development_password
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test<%= ENV['TEST_ENV_NUMBER'] %>
 RAILS_ENV=test TEST_ENV_NUMBER=2 rake db:create
 RAILS_ENV=test TEST_ENV_NUMBER=2 rake db:migrate

 TEST_ENV_NUMBER=2 rspec spec/
 If you are looking for solution how to run same test suite on multiple databases check https://github.com/grosser/parallel_tests

https://github.com/equivalent/scrapbook2/blob/master/archive/mini-blogs/2015-03-30-run-multiple-times-rspec-on-same-machine.md