Jon Arrien

Tag: Rails

Reserved words in Rails

A simple web app to check reserved words in Rails:

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]

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

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

Spring in Rails 4.1 and ST3

I always found a bit embarrassing to configure Spork gem or even use Zeus server to run my specs and features. But fortunately, a few weeks ago Rails 4.1 was released with Spring application preloader by default. I found really sweet because it is totally transparent for the developer and runs in the background the first time you use it.

I usually let Guard running in iTerm, but sometimes I like to run simple test from the editor and that’s why I use RubyTest plugin in Sublime Text. The cool thing is that you can change the command executed in: Preferences > Package Settings > RubyTest > Settings User and update it to use Spring.

[cc lang=”javascript” tab_size=”2″ lines=”40″]


“run_rspec_command”: “spring rspec {relative_path}”,
“run_single_rspec_command”: “spring rspec {relative_path}:{line_number}”,

“run_cucumber_command”: “spring cucumber {relative_path} –no-color”,
“run_single_cucumber_command”: “spring cucumber {relative_path} –no-color”,


Copyright © 2018 Jon Arrien

Theme by Anders NorenUp ↑