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

Override Rails default rake task


desc "this will be now a default task"
task info: :environment do
    puts 'Run rake test to test'
end

task(:default).clear.enhance(['info'])
source

Tuesday, March 3, 2015

Carrierwave uploader not triggering proces in RSpec

processing is turn off for sake of test speed
# spec/my_test.spec
before do
  DocumentUploader.enable_processing = true
end
It's probably because you have something like
# config/initializers/carrierwave.rb
if Rails.env.test?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end
else
  CarrierWave.configure do |config|
    config.storage = :fog
  end
end
I recommend to keep the enable_processing = false and just overide it when needed