grosser/rspec-instafail

Show failing specs instantly. Show passing spec as green dots as usual.

Configuration:

# spec/spec.opts (.rspec for rspec 2)
--require rspec/instafail
--format RSpec::Instafail

CSS: Emulate linear gradients with inset box shadows

Why is this useful?

  • You can have a background image on the same element, overlaying it with a transparent gradient.
  • Since you are probably using gradients to give an element a glossy or three-dimensional feel, box shadows work much better for this. This is because linear gradients always stretch to the full size of the element (which can grow with user input), while a natural shine or shadow only highlights a fixed size on the top or bottom.
  • Browser support for linear gradients is a mess. I avoid using them. In part...

CSS: Emulate borders with inset box shadows

When is this useful?

  • When both parent and child elements have borders, with this technique you don't get two borders (which looks ugly). This is because child elements are rendered over inset box shadows, not inside inset box shadows.
  • You want more than one border on the same element. You can have as many inset box shadows on the same element as you like, e.g. allowing you to make a picture-frame-like border.

Examples

Remember the attribute list of box-shadow is x-offset, y-offset, blur radius, shadow r...

RSpec: Change the type of a spec regardless of the folder it lives in

In a Rails application, *_spec.rb files get special treatment depending on the file's directory. E.g. when you put a spec in spec/controllers your examples will have some magic context like controller, post or get that appears out of nowhere.

If you want that magic context for a spec in another folder, use the :type option:

describe CakesController, :type => :controller do
  ...
end

mattheworiordan/capybara-screenshot

Takes a screenshot when you call it, or when a test fails.

Literally 101 Ruby tricks

The linked slidedeck holds many tips, of which I list the most interesting to me below

DATA and END

The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

Running the attached example file data.rb prints:

DATA is a File object to everything in the current file after "__END__"

No braces needed for 'special variables'

@instance, @@class, $global = [ 'instance', 'class', 'global' ]
puts "#@instance, #@@class, #$global"

...

How Ruby lets you keep script and data in *one* file

The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

The attached example file data.rb looks like this:

puts DATA.read

__END__
DATA is a File object to everything in the current file after "__END__"

Running it with ruby data.rb prints:

DATA is a File object to everything in the current file after "__END__"

Geordi: Running Selenium tests in a VNC buffer

Geordi now supports our solution for running Selenium tests without having Firefox or Chrome windows popping up all over your workspace.

This will stop Selenium windows from appearing on your desktop, but you can still inspect them when necessary.

Installation

Update geordi with gem install geordi.

Run geordi vnc --setup and follow the instructions.

Usage

geordi cucumber will automatically use VNC. Launchy will still open pages in the usual place.

geordi vnc will allow...

Howto transfer a single mysql table between several deployment stages

Example task: Multiply the table holidays between several stages.

  1. Open two terminals:

    shell-for stage_1
    shell-for stage_2
    
  2. Get the stage1 and stage2 MySQL credentials:

    cat /opt/www/the_stage.host.tld/current/config/database.yml
    cat config/database.yml # should do it
    
  3. Dump the table to a path reachable by the stage2 user (e.g. home):

    mysqldump -h mysql1 -u stage_1_user -p stage_1_database table_name > ~/table_name_dump.mysql # Select certain records using --where "some_id > 666"
    

    (-...

Asset pipeline may break Javascript for IE (but only on production)

If some of your JavaScripts fail on Internet Explorer, but only in staging or production environments, chances are that JavaScript compression is the culprit.

By default, Rails 3.2 compresses JavaScript with UglifyJS. I have seen a few cases where this actually breaks functioning JavaScript on IE (one example is the CKEditor).

I fixed this by switching to Yahoo's YUI Compressor.

To do this, do the following:

  • replace the uglifier gem with the yui-compressor gem...

Machinist blueprints do not work with a column :display

This didn't work for me. Seems display is already taken in Machinist.

# in spec/support/blueprints.rb
Partner.blueprint do
   company_name
   display { true }
end

exception_notification: Send exception mails in models

You're using exception_notification and want to send exception mails within a model. Here's how.

The ExceptionNotifier class has a method notify_exception for that. Simply pass an exception:

ExceptionNotifier.notify_exception Exception.new("testfoo")

=> #<Mail::Message:77493640, Multipart: false, Headers: <Date: Mon, 24 Sep 2012 13:37:00 +0200>,
<From: foo@example.com>,
<To: ["fail@failtrain.com", "fail@failbus.org"]>,
<Message-ID: <5060543b3759_212311986a0305e...

Migrating to Spreewald

This describes how to migrate an existing cucumber test suite to Spreewald.

  1. Add the gem

  2. Include spreewald into your cucumber environment by putting
    require 'spreewald/web_steps'
    require 'spreewald/email_steps'
    # ...
    or just
    require 'spreewald/all_steps'
    into your support/env.rb.

  3. Look through your step definitions for everything that might be included in Spreewald. Candidates are web_steps, shared_steps, table_steps, `em...

Using before(:context) / before(:all) in RSpec will cause you lots of trouble unless you know what you are doing

TL;DR Avoid before(:context) (formerly before(:all)), use before(:example) (formerly before(:each)) instead.

If you do use before(:context), you need to know what you are doing and take care of any cleanup yourself.

Why?

Understand this:

  • before(:context) is run when the context/describe block begins,
  • before(:context) is run outside of transactions, so data created here will bleed into other specs
  • before(:example) is run before each spec inside it,

Generally, you'll want a clean setup for each s...

Git basics: checkout vs. reset

Today I got a better understanding of how git works, in particular what git checkout and git reset do.

Git basics

  • A commit holds a certain state of a directory and a pointer to its antecedent commit.
  • A commit is identified by a so-called ref looking something like 7153617ff70e716e229a823cdd205ebb13fa314d.
  • HEAD is a pointer that is always pointing at the commit you are currently working on. Usually, it is pointing to a branch which is pointing to that commit.
  • Branches are nothing but pointers to commits. Y...

Create autocompletion dropdown for Cucumber paths in Textmate

Ever wanted autocompletion for paths from paths.rb in Cucumber? This card lets you write your steps like this:

When I go to path *press tab now* # path is replaced with a list of all known Cucumber paths

This is how you do it

(key shortcuts apply for TextMate2)

  1. Open the bundle editor (ctrl + alt +  + B)

  2. Create a new Item ( + N), select "Command"

  3. Paste this:
    ^
    #!/usr/bin/env ruby -wKU
    require File.join(ENV['TM_SUPPORT_PATH'], 'lib', 'ui.rb')

    cucumber_paths = File.join ENV['TM_PROJECT_DIRECTORY'], 'features'...

Imagemagick: Batch resize images

Trick: Do not use convert but mogrify:

mogrify -resize 50% *

This overwrites the original image file.

In contrast, convert writes to a different image file. Here is an example if you need this:

cd /path/to/image/directory
for i in `ls -1 *jpg`; do convert -resize 50% $i "thumb_$i"; done

How to make your application assets cachable in Rails

Note: Modern Rails has two build pipelines, the asset pipeline (or "Sprockets") and Webpacker. The principles below apply for both, but the examples shown are for Sprockets.


Every page in your application uses many assets, such as images, javascripts and stylesheets. Without your intervention, the browser will request these assets again and again on every request. There is no magic in Rails that gives you automatic caching for assets. In fact, if you haven't been paying attention to this, your application is probabl...

Test meta-refresh redirects with Cucumber

The step definition below allows you to write:

Then I should see an HTML redirect to "http://www.makandracards.com" in the page head

Capybara

Then /^I should see an HTML redirect to "([^\"]*)" in the page head$/ do |redirect_url|
  page.should have_xpath("//meta[@http-equiv=\"refresh\" and contains(@content, \"#{redirect_url}\")]")
end

To find meta tags with Capybara, you can also use page.find('meta', visible: false).

Manage ssh keys with Keychain

Keychain helps you to manage ssh and GPG keys in a convenient and secure manner. It acts as a frontend to ssh-agent and ssh add, but allows you to easily have one long running ssh-agent process per system, rather than the norm of one ssh-agent per login session.

This dramatically reduces the number of times you need to enter your passphrase. With keychain, you only need to enter a passphrase once every time your local machine is rebooted. Keychain also makes it easy for remote cron jobs to securely "hook in" to a long running ssh-agent p...

How to fix gsub on SafeBuffer objects

If you have an html_safe string, you won't be able to call gsub with a block and match reference variables like $1. They will be nil inside the block where you define replacements (as you already know).

This issue applies to both Rails 2 (with rails_xss) as well as Rails 3 applications.

Here is a fix to SafeBuffer#gsub. Note that it will only fix the $1 behavior, not give you a safe string in the end (see below).

Example

def test(input)...

Fun with Ruby: Returning in blocks "overwrites" outside return values

In a nutshell: return statements inside blocks cause a method's return value to change. This is by design (and probably not even new to you, see below) -- but can be a problem, for example for the capture method of Rails.


Consider these methods:

def stuff
  puts 'yielding...'
  yield
  puts 'yielded.'
  true
end

We can call our stuff method with a block to yield. It works like t...

How to test print stylesheets with Cucumber and Capybara

A print stylesheet is easy to create. Choose a font suited for paper, hide some elements, done. Unfortunately print stylesheets often break as the application is developed further, because they are quickly forgotten and nobody bothers to check if their change breaks the print stylesheet.

This card describes how to write a simple Cucumber feature that tests some aspects of a print stylesheets. This way, the requirement of having a print stylesheet is manifested in your tests and cannot be inadvertedly removed from the code. Note that you can...

RSpec and Cucumber: Shorthand syntax to run multiple line numbers in the same file

This works in modern RSpecs (RSpec >= 2.x) and Cucumbers:

rspec spec/models/node_spec.rb:294:322
cucumber features/nodes.feature:543:563:579

Also your features should be shorter than that :)