Jon Arrien

Tag: Ruby (page 1 of 2)

Pow: rake not found

Captura de pantalla 2014-05-26 a la(s) 08.29.13

If you use pow as your development server, may be you’ve encountered an issue with Rake. You’ve run “bundle install”, but pow still is not able to find the gem.

If this is your case, just run this command from your projects root path to load you environment in .powenv:
[cc lang=”bash”]
rvm env > .powenv

New in RSpec 3

  • Removed support for Ruby 1.8.6 & 1.9.1
  • Improved Ruby 2.0 support
  • New rspec-support gem
  • Ability to disable all of the monkey patching mode with one option
  • :each and :all have now aliases => :example and :context
  • describe, shared_examples_for and shared_context methods under RSpec module.
  • xdescribe,  xcontext and xit for examples
  • fdescribe, fcontext and fit to set :focus => true
  • Changes in pending, introduced skip
  • Added is_expected
  • Groups can be ordered individually
  • Improvements in rspec —init
  • New —dry-run CLI option
  • Completely new formatter API
  • Removed expect_with  :stdlib. Instead explicit :text_unit and :minitest.
  • Added define_derived_metadata to tag all specs in a group
  • Ability to chain multiple matchers togethers using and or or (Aliases for & and | )
  • match matcher can be used for data structures
  • New all matcher
  • New be_between matcher
  • Boolean matchers renamed to be_truthy and be_falsey
  • match_array renamed to contain_exactly
  • Improved integration with minitest
  • Removed mock and stub aliases of double
  • File-type inference disabled by default




Rails 4 & Etag

When a request is processed in Rails 4, it creates a MD5 hash of the entire page and include the digest in the headers.
[cc lang=”ruby”]
header[‘Etag’] = Digest::MD5.hexdigest(body)

In following requests, it will compare generated Etag with previously generated ones, and if a matching is found, it will respond with 304 HTTP response(Not modified). The page will be retrieved from browser’s cache history and will avoid sending the full response again to client side.

We can use the fresh_when method and pass an array of items to create more complex cachings. For example, based on current user and his age.
[cc lang=”ruby”]
class ProfilesController < ApplicationController def show @profile = Profile.find(params[:id]) fresh_when([@profile,, current_user.age]) end end [/cc] But sometimes, we want to reuse part of this array and don’t have to repeat it every time. In these cases, we can extract some tags and apply to everything. [cc lang="ruby"] class ProfilesController < ApplicationController etag { } etag { current_user.age } def show @profile = Profile.find(params[:id]) fresh_when([@profile]) end end [/cc]


Add GeoIP gem to your Gemfile and run bundle install:
[cc lang=”ruby”]
gem ‘geoip’
Download GeoIP.dat.gz from and unzip it in “db” folder. Initialize geoip with downloaded file path and get location-based on request IP:
[cc lang=”ruby”]
@geoip ||=“#{RAILS_ROOT}/db/GeoIP.dat”)
remote_ip = request.remote_ip
location =[:country_name]


Usually when a developer declares a Class in Ruby, they create a initializer method only for assigning some ivars.
[cc lang=”ruby”]
class Car
attr_reader :model, :color
def initialize(model, color)
@ model, @ color = model, color

My religion forces me to not duplicate code, so I founded a simple solution for monkey patching Class object and add a helper method to abbreviate this casuistry:
[cc lang=”ruby”]
class Car
attr_initializer :model, :color

[cc lang=”ruby”]
class Class
def attr_initializer(*attributes)
attr_reader *attributes
class_eval <<-RUBY def initialize(#{attributes.join(', ')}) #{{ |attribute| "@#{attribute}" }.join(', ')} = #{attributes.join(', ')} end RUBY end end [/cc]

Spring with Cucumber & RSpec

Spring and Guard allows us to see tests running almost immediately after hitting save on a Feature or Spec. Once you’ve setup RSpec and Cucumber, just add spring commands to take its benefits:

[cc lang=”ruby”]
group :development, :test do
# …
gem ‘spring-commands-rspec’
gem ‘spring-commands-cucumber’
# …

Launch binstubs to let commands available for spring:
[cc lang=”bash”]
% bundle install
% spring binstub –all
* bin/rake: spring already present
* bin/rspec: generated with spring
* bin/rails: spring already present

And remember prepending spring before rspec or cucumber command.
[cc lang=”ruby”]
guard :rspec, cmd:”spring rspec” do

Also read this post if you want to integrate with Sublime Text:
Spring + ST3

Is TDD dead?

On Friday, May 9th at 11am eastern (17pm in Spain) will take place an interesting live discussion about TDD.


ActiveAdmin & WillPaginate

To get Will_Paginate and ActiveAdmin to play nice and avoid this error:
[cc lang=”ruby”]
undefined method `per’ for #

In your config/initializers, add this:
[cc lang=”ruby]
# config/initializers/kaminari.rb
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari


Copyright © 2017 Jon Arrien

Theme by Anders NorenUp ↑