A nicer way to run RSpec and/or Cucumber
geordi, our collection of awesome shell scripts, has been extended by three scripts to help you call RSpec or Cucumber:
cuc
This script runs Cucumber the way you want it:
- Prints some line feeds to easily find your test results when you come back to the console later
- Configures Cucumber to use cucumber_spinner if it is available in your
Gemfile
- Runs Cucumber under
bundle exec
- Uses an old version of Firefox for Selenium (Javascript) features...
Don't forget to add ":dependent => :destroy" for join models
you should always
has_many :join_models, :dependent => :destroy
Dynamically skip Capistrano hooks
When you have a hook in your Capistrano file that dumps your remote database, you might not want it to dump each time you deploy (say, you're experimenting with staging
and don't want ten dumps an hour). How to skip dump creation:
Capistrano 2
In your Capistrano file:
before 'deploy:update_code', 'db:dump' unless fetch(:skip_dump, false)
The second parameter of fetch
sets a default value if skip_dump
is not defined.
In your terminal:
cap staging deploy -S skip_dump=true
The -S
must be a capital letter t...
Mark up text in Balsamiq Mockups
When you edit text in Balsamiq Mockups, you can define inline styles like this:
| Bold | Some *bold* text
|
| Italics | Some _italic_ text
|
| Blue underlined hyperlink | Some [linked] text
|
Note that most Balsamiq widgets display with a bold font by default, so you won't usually see a difference when a piece of text is explicitly marked up as bold.
How to: Classic scrollbars in Ubuntu 11.04
Ubuntu natty introduced new "invisible" scrollbars for GTK programs such as gEdit or Nautilus.
If you do not like them:
sudo apt-get purge liboverlay-scrollbar-*
In order to have a tool recognize the changes you have to quit all its instances. Or just log out and back in.
Files open with an error "the location is not a folder" after installing XFCE
When using gnome-open
(i.e. double-clicking a file, telling to browser to open a download, etc.) on Ubuntu you may get this error:
The location is not a folder.
It's due to the exo-utils
package which does not play nice with gnome-open
. You can fix the problem by uninstalling it:
sudo apt-get remove exo-utils
Note that this may also uninstall XFCE tools like xfce4-terminal
or thunar
(XFCE's directory browser). If you want to keep them, [try anot...
Commonly used regular expressions - Cheatsheet
For Email-Patterns see most recent E-Mail-Pattern.
HOST = /\A[a-z0-9]+[a-z0-9\-\.]*[a-z0-9]+\z/i
BANK_CODE = /\A\d{8}\z/ # Germany
BANK_ACCOUNT_NUMBER = /\A\d{1,10}\z/ # Germany
ZIP_CODE = /\A\d{5}\z/ # Germany
HEX_COLOR = /\A([a-fA-F0-9]{3}){1,2}\z/
IP_ADDRESS = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
We usually put these constants in...
Fixing: no sound on front audio in Ubuntu 11.04
My front audio output would not work even though the front mic input did fine.
The Pulse Audio mixer (= Ubuntu default) does not offer any complex controls to fix the issue. Instead, run gamix
(be prepared for a GUI blast) and switch "Independent HP" to OFF. The label may be different for your audio device.
This enables all available audio outputs.
Printing to PDF files on Ubuntu
This will install a printer called "PDF":
sudo apt-get install cups-pdf
Files will be put into ~/PDF/
without any questions asked.
List directories ordered by size (with human-readable output)
You know there is the du
command to fetch the disk usage of a directory (“.
” in this example). By default, output is sorted by directory name, not size:
du -h --max-depth=1 .
40K ./a
2.3G ./b
3.1M ./c
500M ./d
2.8G .
To see which directories take up the most (or least) space you can order them by size like this (the -h
switch does the magic of understanding humanized size formats):
du -h --max-depth=1 . | sort -h
40K ./a
3.1M ./c
500M ./d
2.3G ./b
2.8G .
Note that you will need to...
How to test resource_controller hooks
When using the resource_controller gem you often hook onto events like this:
update.before do
do_something
end
For testing such things in your controller you should -- as always -- not trigger something that eventually calls the thing you want.\
Instead, in your specs, have resource_controller run those hooks like it does itself. Like that:
describe 'before update' do
...
Check apache config files' syntax
To check your apache2 config files for syntax errors run
sudo apache2ctl configtest
or shorter
sudo apache2ctl -t
Be aware that this does not guarantee a successful Apache startup. It may still fail due to permission errors, etc.
SSL: Build a Certificate signing request (CSR)
In order to request a SSL certificate from any dealer, you usually need a CSR certificate. As both the CSR as well as key are created in this step, make sure you save this certificate on a trusted, secure machine only. Usually this is your production environment.
Run this on the server (not on your machine) as root.\
Replace your-domain.tld with the domain you request the certificate for and YYYY with the current year so you will not have any conflicts when requesting a certificate next year.
openssl req -new -sha256 -out www.your-dom...
Soft-scroll to an anchor with jQuery
This snippet makes links that refer to an anchor (like "<a href="#something">...</a>
") scroll softly to it.\
In this example we only do it for links that also own a data-animate
attribute.
$('a[href^="#"][data-animate]').live('click', function() {
var hash = $(this).attr('href');
var offset = $(hash).offset();
if (offset) {
$('html, body').animate({ scrollTop: offset.top }, 'slow');
location.hash = hash;
return false;
}
});
Note that this could basically work for any element whos...
Semantic markup standard for search engines
If you would like to enrich your website with semantic markup like contact data, places or events you should have a look at schema.org. "Search engines including Bing, Google and Yahoo! rely on this markup to improve the display of search results, making it easier for people to find the right web pages."
The following example from the schema.org documentation shows you how to describe a movie with semantic markup:
<div itemscope itemtype ="http://schema.org/Movie">
<h1 itemp...
Alternative to #to_a and Array(...)
Note: In Rails 3+ you can use Array.wrap
instead of the solution here.
In the past you could use Array(...)
or #to_a
to turn an object into an array, unless it already is an array:
Array(5) # => [5]
Array([5]) # => [5]
Unfortunately this idiom has issues and it also triggers deprecation warnings like this one:
warning: default `to_a' will be obsolete
For an alternative you can copy the attached file to `config/i...
Open a new tab with the current directory on a mac
I found a nice script on crazylittlehacks and modified it slightly. Put the attachment to /usr/local/bin
, chmod +x
and run it like this:
tab
You may also pass a command that will be run in that newly opened tab:
tab ruby script/server
Note: This does not play too nice with Visor, because Visor will always be "window 1" for AppleScript. By modifying the script and replacing in window 1
with `in window...
Matching elements on complex web pages with Webrat
XPath matchers can be combined with CSS-selector matchers. This is really useful if not, for example, the content of an element should be matched but the element itself like in the following example. Here a form is used to display data as default value in its input elements. This can be the case in web applications in which data should be edited easily without additional clicks.
Enable soft tabs in Vim
If you want to enforce soft tabs (spaces instead of tabstops) in Vim put this into your ~/.vimrc
(Linux) or C:\Users\<Username>\_vimrc
(Windows):
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
You can also call those commands in a running Vim instance (using ":set ...
") to switch on soft tabs temporarily.
On a side note: remember the :mkvimrc
command that saves active settings to a vimrc file.
Dump a two-dimensional array to an Excel .xls spreadsheet
Copy the attached Ruby code to config/initializers
, or paste it into your IRB console. You can now dump any two-dimensional array to an Excel .xls
spreadsheet with a single method call:
table = [['user', 'email', 'hours']]
User.all.each do |user|
table << [user.name, user.email, user.hours]
end
table.dump_to_excel('users.xls')
The first row in the array will be dumped as the table head in bold type.
You need the spreadsheet
gem in your Gemfile
.
Note that there is a [limit of 65535 rows](https://makandracard...
Using StaticMatic for static pages
Update: Staticmatic will not be further developed. They suggest to switch to middleman.
If you need to make a static web page and find yourself missing all your Rails comforts, take a look at StaticMatic.
This works like an extremely stripped down version of Rails, giving you
- HAML
- SASS
- helpers
- partials
When done, everything is simply compiled to s...
Find records created within a timespan
Put the attached file into config/initializers/
to be able to say created_within
on any ActiveRecord
or its scope chain. This will return a scope of all records created between two given timestamps.
Use it like that:
Booking.created_within(1.year.ago, Date.yesterday).count
User.active.created_within(1.hour.ago, DateTime.now)
It explicitly looks at the database table name so you are usually fine when joining other tables.
Gem Versioning and Bundler: Doing it Right
When running an executable, ALWAYS use bundle exec. In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle. However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.