Submit a form with Prototype

For example, to send a form and populate a preview div with the response.

$('content_form').request({
  parameters: { 'preview': "1" }, // overrides parameters
  onComplete: function(transport){ 
    $('previewContent').update(transport.responseText); 
  }
});

Autofocus a form field with HTML5 or jQuery

Automatically placing the cursor in a form field improves first interaction and removes an extra click. HTML5 autofocus works in modern browsers; jQuery can provide a fallback.

Fixing Webrat after following an external link

When a Cucumber feature leaves your page through an external Link, Webrat has problems like "Could not find field: "E-mail" (Webrat::NotFoundError)" using your page afterwards. It will also have trouble following redirects.

Fix it with this step:

Given /^I am back on my page$/ do
  webrat_session.header("Host", "www.example.com")
end

MySQL replication how-to

Setting up MySQL master-slave replication is error-prone, especially with initial data dumps, binary log positions, and SSH tunnels for remote connectivity.

MySQL Server and UTF-8 Defaults

MySQL defaults can corrupt UTF-8 exports and force manual charset settings for each new database. Setting server and client defaults to utf8mb4 prevents incorrect mysqldump output.

Convert a Subversion repository to Git

Migrating a Subversion repository to Git can preserve full history and branches, but tool quirks may affect the result. A trunk-only import is possible with git svn clone.

Branching and merging in Subversion

Subversion branch workflows avoid commit and merge conflicts when isolating long-running work and later bringing changes back to trunk.

Common Screen Resolutions

Target layouts for common notebook, desktop, netbook and iPad screen sizes to reduce clipping and horizontal scrolling.

Load all models into an Array

Dir.glob(File.join RAILS_ROOT, 'app', 'models', '*.rb').collect{ |path| path[/.+\/(.+).rb/,1] }.collect(&:camelize).collect(&:constantize)

Cucumber Webrat steps

Custom Cucumber step definitions for checking fields, HTML content, regex matches, and table data in Webrat-based tests.

Reprocess only missing images with Paperclip

Adding a new image variant to many Paperclip attachments can trigger costly full reprocessing; patching Paperclip::Attachment skips styles that already exist.

Convert colorspace of images attached with Paperclip

has_attached_file(
  :avatar,
  :styles => { :large => "300x300", :small => "100x100" },
  :convert_options => { all => "-colorspace RGB" }
)

Reprocess Paperclip attachments in one line

script/runner -e development 'Article.all.each { |a| a.image.reprocess! if a.image.exists? }; "done"'

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

Publishing an older Jeweler-based gem can require version bumps, gemspec regeneration, tagging, and sometimes a manual gem push to RubyGems.org.

Aggregated RSpec/Cucumber test coverage with RCov

RCov defaults fail to aggregate RSpec and Cucumber coverage cleanly; a custom rake rcov:all task produces a Rails report for app/**/*.rb only.

Better output for Cucumber

cucumber_spinner adds a progress bar for Cucumber features and shows failing scenarios immediately, with optional browser opening for failed pages.

Test concurrent Ruby code

Concurrent Ruby code with blocking system calls can fail under Thread-based tests; using forked processes, pipes, and separate ActiveRecord connections keeps synchronization tests reliable.

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.

Array.uniq does not work in Prototype

For arrays of objects, uniq does not work as expected, since it uses strict equality. So

[[1], [1]].uniq() == [[1], [1]]

In some cases, this might be a workaround:

[[1], [1]].invoke("toJSON").uniq().invoke("evalJSON")   
// == [[1]]

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

Parsing XML and HTML with Nokogiri needs the right selector syntax; namespaces can make XPath queries miss nodes unless prefixes are used.

Rails - Multi Language with Fast_Gettext

Rails localization with fast_gettext enables multi-language apps using .po files, _() and n_() for translated text and plural forms.

Automatically build sprites with Lemonade

How it works

See the lemonade descriptions.

Unfortunately, the gem has a few problems:

  • it does not work with Sass2
  • it always generates all sprites when the sass file changes, which is too slow for big projects
  • it expects a folder structure quite different to our usual

All these problems are solved for us, in our own lemonade fork. This fork has since been merged to the original gem, maybe we can use t...