Topics: Gems

Use the "paper_trail" gem to track versions of records

paper_trail is an excellent gem to track record versions and changes.

You almost never want to reimplement something like it yourself. If you need to log some extra information, you can add them on top.

It comes with a really good README file that holds lots of examples. I'll show you only some of its features here:

  • Setting up a model to track changes
    Just add has_paper_trail to it:
    class User < ActiveRecord::Base
      has_paper_trail
    end
    
  • Accessing a previous version
    Saying user.previous_version gi…

Updated: Test a gem in multiple versions of Rails

Updated the card with our current best practice (shared app code and specs via symlinks).

Building Gem 'RedCloth' with Bundler and GCC 4.6

If you cannot install the gem 'RedCloth' via bundle install you might want to try the following.

Specifying extra build options for bundler which will only be applied when building RedCloth:

bundle config build.RedCloth --with-cflags=-w

Install the typhoeus gem with native extensions

If you get this:

Installing typhoeus (0.3.3) with native extensions
/usr/local/lib/site_ruby/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/usr/bin/ruby1.8 extconf.rb 
checking for curl/curl.h in /opt/local/include,/opt/local/include/curl,/usr/include/curl,/usr/include,/usr/include/curl,/usr/local/include/curl... no
need libcurl

You can fix it by installing the libcurl3-dev package:

sudo apt-get install libcurl3-dev

Now you should…

How to create Excel sheets with spreadsheet gem and use number formats for cells like money or date

The following snippet demonstrates how you could create excel files (with spreadsheet gem) and format columns so that they follow a specific number format like currencies or dates do.

require 'rubygems'
require 'spreadsheet'

Spreadsheet.client_encoding = 'UTF-8'

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => 'test'

money_format = Spreadsheet::Format.new :number_format => "0.00 [$€-407]"
date_format = Spreadsheet::Format.new :number_...

Fix error: Missing the mysql2 gem

So you got this error, even though your Gemfile bundles mysql2:

!!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2'

or

Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.)

The reason for this confusing error message is probably that your Gemfile says mysql2, but your database.yml still uses the mysql adapter. Change it to use the mysql2 adapter:

```raw development: adapter: mysql2 database: myproject_developm…

Bundler: Fatal error and 'no such file to load -- net/https'

Today, I ran into trouble on a fairly fresh installed VM, running Ubuntu. I tried to bundle install and got this stacktrace:

```text Fetching gem metadata from https://rubygems.org/.Unfortunately, a fatal error has occurred. Please see the Bundler troubleshooting documentation at http://bit.ly/bundler-issues. Thanks! /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load – net/https (LoadError)

from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `require&#39;
from /usr/li...

has_defaults is now a gem

  • has_defaults is now a gem, no longer a plugin.
  • The plugin version no longer exists. Note that plugins are no longer supported in 3.2.
  • If you are working on an application that has the plugin version of has_defaults there is no advantage to be gained from upgrading the gem. The gem is there for you should you one day upgrade to Rails 3.2+.
  • Please don't use the defaults gem which we original forked away from in 2009. It sets defaults when a field is `bl…

Install the gherkin gem with native extensions

When trying to install the gherkin gem (possibly not a recent version), you might encounter an error saying:

cc1: warnings being treated as errors

If upgrading is not an option (corresponding issue on github), you can install it properly by changing the compiler flags as follows:

gem install gherkin -v '2.2.9' -- --with-cflags=-w

If you cannot install the specific version you intent to, try uninstalling the gem first.

To install the gem with bundler you can to do this:

...

How to install a specific version of RubyGems (and how to downgrade)

Sometimes you want one distinct version of RubyGems to be installed to replicate the same behavior across multiple servers.

Usually would do this to update your RubyGems, but this always takes you to the latest version:

sudo gem update --system

While there are ways around the interwebs that use the rubygems-update package and call its setup.rb, there is an undocumented switch you can use:

sudo gem update --system 1.3.7

This updates to the given version, 1.3.7 in the above case, by walking the rubygems-update package way it…

Gem development: When your specs don't see dependencies from your Gemfile

When you develop a gem and you have a Gemfile in your project directory, you might be surprised that your gem dependencies aren't already required in your specs. Here is some info that should help you out:

  • Bundler actually doesn't automatically require anything. You need to call Bundler.require(:default, :your_custom_group1, ...) for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment.
  • That also means that if you have an embedded Rails app in your spec folder (like [h…

Run specific version of bundler

You want to run a specific, maybe older version of bundler for whatever reason?

$ bundle _1.0.21_ -v
Bundler version 1.0.21

rspec_candy is now a gem

Our awesome collection of rspec helpers (formerly known as "spec_candy.rb") is now available as a gem. It works, it is tested and there will be updates.

Usage

Add rspec_candy to your Gemfile.

Add require 'rspec_candy/helpers' to your spec_helper.rb, after the rspec requires.

List of features

See on GitHub

Bundler 1.1 has been released, is very fast

Bundler 1.1 has been released. With this version you no longer need to wait for this:

Fetching source index…

The new Bundler is smarter and can fetch required metadata in a few seconds.

You can install the new version by saying:

sudo gem install bundler

Look up a gem's version history

Sometimes it might be helpful to have a version history for a gem, e.g. when you want to see if there is a newer Rails 2 version of your currently used gem.

At first you should search your gem at RubyGems. Example: will_paginate version history.

The "Tags" tab at GitHub might be helpful as well.

How to fix: "500 Internal Server Error" after adding Rack::Bug

When Rack::Bug has been added to your project and your Apache2/Passenger only replies with an Error 500 (Internal Server Error) you won't get any love from both application and Apache logs.

You can start a script/server and try connecting there. It should also fail but you will most likely see this error:

Internal Server Error  
undefined method `new' for "Rack::Bug":String

While the following is (for some reason) working on OSX…

config.middleware.use "Rack::Bug", :secret_key => '...'

…you need to do this so it wor…

88 cards