Install a local Gemfile on a remote server

Call with the server's hostname (and user if you have no SSH agent), e.g.

install-gems-remotely my.server.com
# or without agent:
install-gems-remotely me@my.server.com

When you call it from a rails directory, it uploads your Gemfile, Gemfile.lock as well as the gemspecs of all vendored gems in to a temporary folder on the server and does a bundle install there.

If you need to install gems from anothere Gemfile, just do it like this:
BUNDLE_GEMFILE=Gemfile.something; install-gems-remotely my.server.com


This scri...

Debug Ruby code

This is an awesome gadget in your toolbox, even if your test coverage is great.

  • gem install ruby-debug (Ruby 1.8) or gem install debugger (Ruby 1.9)
  • Start your server with script/server --debugger
  • Set a breakpoint by invoking debugger anywhere in your code
  • Open your application in the browser and run the code path that crosses the breakpoint
  • Once you reach the breakpoint, the page loading will seem to "hang".
  • Switch to the shell you started the server with. That shell will be running an irb session where you can step thr...

Override e-mail recipients in ActionMailer

Our gem Mail Magnet allows you to override e-mail recipients in ActionMailer so all mails go to a given address.

This is useful for staging environments where you want to test production-like mail delivery without sending e-mails to real users.

Automatically run bundle exec if required

There will probably be better solutions as we become more experienced with using Bundler, and more command line tools become Bundler-aware.

b will use bundle exec if there is a Gemfile in the working directory, and run the call without Bundler otherwise.

b spec spec

This script is part of our geordi gem on github.

Load order of the environment

Rails 3, 4, 5, 6

  1. config/application.rb
  2. config/environment.rb before the initialize! call (we don't usually edit this file)
  3. The current environment, e.g. environments/production.rb
  4. Gems
  5. Vendored plugins
  6. All initializers in config/initializers/*.rb
  7. config/environment.rb after the initialize! call (we don't usually edit this file)
  8. Your own code from app

Rails 2

  1. Code in config/preinitializer.rb (if it exists)
  2. environment.rb, code above the Rails::Initializer.run blo...

Scope to records with a given state in state_machine

The state_machine gem ships with a scope with_state. This scope has some problems in complex queries or scope chains.

Use this instead:

named_scope :having_state, lambda { |*state_or_states|
  state_or_states = Array.wrap(state_or_states).map(&:to_s)
  { :conditions => [ 'articles.state IN (?)', state_or_states ] }
}

If you want a scope with hash options (with the side effects you should know about):

named_scope :having_st...

Bundler for Rails 2.3.x

Update RubyGems and Passenger

Bundler requires Rubygems >= 1.3.6. Run gem update --system if you have an older version.
It also is not compatible with older versions of passenger, so bring that up to date as well (2.2.15 works).

If you installed RubyGems through apt (which you should never do!), you may see a message giving you a hint to use apt to update.
Some people advise to install the 'rubygems-update-1.3.7' gem on Ubuntu systems if you used apt to install RubyGems.
I did that - and lost all...

Recursively remove unnecessary executable-flags

Sometimes files attain executable-flags that they do not need, e.g. when your Windows VM copies them over a Samba share onto your machine.

From inside your Rails project directory call regularly:

geordi remove-executable-flags

Runs chmod -x on Ruby, HTML, CSS, image, Rake and similar files.


This script is part of our geordi gem on github.

Boolean attributes and pretty enumerations in Cucumber Factory 1.7

Boolean attributes can now be set by appending "which", "that" or "who" at the end:

Given there is a movie which is awesome
And there is a movie with the name "Sunshine" that is not a comedy
And there is a director who is popular

Instead of "and" you can now also use "but" and commas to join sentences:

Given there is a movie which is awesome, popular and successful but not science fiction
And there is a director with the income "500000" but with the account balance "-30000"

Update with `sudo gem install cucumber_facto...

How Rails and MySQL are handling time zones

When working with times and dates in Rails applications, you need to deal with the following problem:

  • In Rails, Time objects have a time zone. You can get the zone name by doing time_object.zone.
  • This zone is considered when doing time calculations, e.g. 10 AM CEST minus 8 AM UTC is zero.
  • A datetime in MySQL does not have a zone. It just stores the literal string "2010-05-01 12:00:00".
  • That means that Rails must make assumptions about timestamps loaded from and written to MySQL.

Rails has two completely different modes ...

Run a POP3 server on a directory of mail files with popthis

popthis is awesome when used with inaction_mailer.

Setup inaction_mailer

Install the gem:
sudo gem install popthis

Start the server:
popthis tmp/sent_mails/ # e.g. the folder containing the .txt-mails generated by inaction_mailer

Now, configure your mail client as follows:
Server: localhost
Protocol: POP3
Port: 2220
Username: anything
Password: anything

Marry Capybara with SSL-enabled applications

Capybara does not play nice with sites that have some actions protected by SSL, some not. A popular way to implement this in Rails is using the ssl_requirement plugin by DHH, which redirects a requests from HTTP to HTTPS if the requested action requires SSL and vice versa.

Capybara follows the redirect, but seems to forget the changed protocol for the next request. The only hack-free workaround right now is to use URLs in lieu of paths everywhere (links, form actions).

For a hackful fi...

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

Announcing YARD 0.6.0 (gnuu.org)

YARD 0.6 adds the ability to serve documentation for gems as well as the current project with yard server. Just like gem server in RubyGems, you can serve gem docs. The advantage to YARD’s server is that you don’t need to pre-generate the static docs (with a gem install) before running the server. If you installed your gem with --no-rdoc, YARD will just generate it on the fly!

Build a JSON API for a Rails application

Try our Apify gem which solves many problems you will be having.

Error installing the raspell gem

When you get this while installing the raspell gem:

ERROR:  Error installing raspell:  
ERROR: Failed to build gem native extension.

You need some libraries:

sudo apt-get install libaspell-dev

Aspell Error - No word lists can be found for the language XY

When you get this error:

No word lists can be found for the language "de".

An aspell dictionary is missing. Install it with

sudo apt-get install aspell-de

stefankroes's ancestry at master - GitHub

Ancestry is a gem/plugin that allows the records of a Ruby on Rails ActiveRecord model to be organised as a tree structure (or hierarchy). It uses a single, intuitively formatted database column, using a variation on the materialised path pattern. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single sql query. Additional features are STI support, named_scopes, depth caching, depth constraints, easy migration from older plugins/gems, integrit...

Release gem; Deploy gem; Update a gem created with Jeweler

Until May 2011 our gems have been created with Jeweler, which is a helper library to package code into a gem. You know a gem was cut with Jeweler if you see the word jeweler in a gem project's Rakefile.

This note describes how to update a gem that was cut using Jeweler. Note that this can be traumatic the first time. It would be great to have an easier workflow for this. Jeweler is deprecated these days because you can

**now [cut gems more easily using Bundler](https://makandracards.com/makandra/1229-updat...

Better output for Cucumber

We built cucumber_spinner to have a progress bar for Cucumber features, which also outputs failing scenarios as soon as they fail.

Installation

gem install cucumber_spinner

Usage

cucumber --format CucumberSpinner::ProgressBarFormatter

If you use CucumberSpinner::CuriousProgressBarFormatter and a feature fails, the according page will show up in your browser.


Note that if you run your Cucumber tests using the [cuc](https://makandracards.com/makandra/1277-a-nicer-way-to-...

Better Output for RSpec

rspec_spinner is a progress bar for RSpec which outputs failing examples as they happen (instead of all at the end).

Installation

gem install rspec_spinner

Usage

script/spec -r rspec_spinner -f RspecSpinner::Bar -c

To make a shortcut in your .bashrc

alias ss='script/spec -r rspec_spinner -f RspecSpinner::Bar -c'

There's also an alternate runner RSpecSpinner::Spinner which shows a spinner and the name of the current spec instead of a progress bar.

Concurrent Tests

Install gem and plugin

sudo gem install parallel
script/plugin install git://github.com/grosser/parallel_tests.git

Adapt config/database.yml

test:
  database: xxx_test<%= ENV['TEST_ENV_NUMBER'] %>

Create test databases

script/dbconsole -p
CREATE DATABASE `xxx_test2`;
...

Generate RSpec files

script/generate rspec

(you'll probably only let it overwrite files in script/)

Prepare test databases...

Parse XML or HTML with Nokogiri

To parse XML-documents, I recommend the gem nokogiri.

A few hints:

  • xml = Nokogiri::XML("<list><item>foo</item><item>bar</item></list>") parses an xml string. You can also call Nokogiri::HTML to be more liberal about accepting invalid XML.
  • xml / 'list item' returns all matching nodes; list item is used like a CSS selector
  • xml / './/list/item' also returns all matching nodes, but .//list/item is now an XPath selector
    • XPath seems to be triggered by a leading ....