Jon Arrien

Page 3 of 5

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)
[/cc]

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, curernt_user.id, 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 { current_user.id } etag { current_user.age } def show @profile = Profile.find(params[:id]) fresh_when([@profile]) end end [/cc]

GeoIP

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

attr_initializer

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
end
end
[/cc]

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
end
[/cc]

[cc lang=”ruby”]
class Class
def attr_initializer(*attributes)
attr_reader *attributes
class_eval <<-RUBY def initialize(#{attributes.join(', ')}) #{attributes.map{ |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’
# …
end
[/cc]

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
[/cc]

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

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

QTS 4.1 Release Candidate

1381743376

I’ve just downloaded the QTS 4.1 Release Candidate for my QNAP TS-870, and after the installation everything looks more stable than previous beta versions.

I’ve to admit, I already was delighted with this purchase due to I could expand memory and install Virtualization Station. You can create little instances as EC2 or DigitalOcean for development purposes.

QNAP QTS 4.1 RC

iTerm2 & Tmux integration

This is a homebrew formula to compile easily patched tmux for iTerm2:

[cc lang=”ruby”]
require ‘formula’

class TmuxIterm2 < Formula url 'http://iterm2.googlecode.com/files/tmux-for-iTerm2-20120108.tar.gz' md5 'f15d9f567b9b029482bb7b3227ee7ac3' homepage 'http://code.google.com/p/iterm2/downloads/detail?name=tmux-for-iTerm2-20120108.tar.gz&can=2&q=' depends_on 'libevent' def install ENV.append "LDFLAGS", '-lresolv' system "./configure", "--disable-dependency-tracking", "--prefix=#{prefix}", "--sysconfdir=#{etc}" system "make install" # Install bash completion scripts for use with bash-completion (prefix+'etc/bash_completion.d').install "examples/bash_completion_tmux.sh" => ‘tmux’
end

def caveats; <<-EOS.undent Bash completion script was installed to: #{etc}/bash_completion.d/tmux EOS end end [/cc] To install it from URL: [cc lang="bash"] brew install https://gist.githubusercontent.com/jonarrien/f6bf12df323287c91873/raw/tmux_iterm2.rb [/cc]

Is TDD dead?

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

istddDeadGooglePlusBanner

ActiveAdmin & WillPaginate

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

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

Alcatraz.io

Alcatraz is an open-source package manager for Xcode. It lets you discover and install plugins, templates and color schemes without the need for manually cloning or copying files. It installs itself as a part of Xcode and it feels like home.

[cc lang=”bash]
curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/install.sh | sh
[/cc]

How to install NodeJS on CentOS

First, install development tools on CentOS:
[cc lang=”bash”]
yum -y groupinstall “Development Tools”
[/cc]

Go to sources folder, the usual place to hold on sources.
[cc lang=”bash”]
cd /usr/src
[/cc]

Now, we pick the latest compressed source archive from Node.js website at Node Downloads.
[cc lang=”bash”]
wget http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz
tar zxf node-v0.10.28.tar.gz
cd node-v0.10.28
./configure
make
make install
[/cc]

« Older posts Newer posts »

Copyright © 2017 Jon Arrien

Theme by Anders NorenUp ↑