View
External content

International Address Fields in Web Forms :: UXmatters

Advice for address forms that work with address structures from multiple countries.

Spreewald 0.6.7 "follow link in email"-step won't match backslashes any longer

When you build a link for an email body like this body = "Please click the following link" body << "http://posts/130\n\n" body << "Members:" ...
then updating Spreewald to >=0.6.7 will be able to parse it correctly.

How to not repeat yourself in Cucumber scenarios

It is good programming practice to Don't Repeat Yourself (or DRY). In Ruby on Rails we keep our code DRY by sharing behavior by using inheritance, modules, traits or partials.

When you reuse behavior you want to reuse tests as well. You are probably already reusing examples in unit tests. Unfortunately it is much harder to reuse code when writing integration tests with Cucumber, where you need t…

Implementing social media "like" buttons: Everything you never wanted to know

So you client has asked you to implement a row of buttons to like the URL on Facebook, Twitter and Google+. Here are some things you should know about this.

0. Security considerations

Each "like" button is implemented by including a Javascript on your site. This means you are running fucking remote code on your page. You are giving Facebook, Twitter and Google+ full permission to e. g. copy user cookies. Check with your client if she is cool with that. Also note that if you're site is suggesting security by operating under HTTPS …

RailsPanel chrome extension

Chrome extension that shows all info from your rails log (like parameters, response times, view rendering times, DB requests) inside a chrome panel.

PostgreSQL vs. Rails migration: How to change columns from string to integer

When writing Rails migrations to convert a string column to an integer you'd usually say:

change_column :table_name, :column_name, :integer

However, PostgreSQL will complain:

PG::DatatypeMismatch: ERROR:  column "column_name" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

The "hint" basically tells you that you need to confirm you want this to happen, and how data shall be converted. Just say this in your migration:

change_column :table_name, :column_name, 'i...
External content

Stubbing out external services with an embedded Sinatra application

One of many useful techniques when your test suite needs to talk to a remote API.

Spreewald 0.6.6 improves the Timezone switch

When you run into time zone issues e.g. Given the time is "2011-11-11 11:11"
but Time.now => ... 13:11
then updating Spreewald to >=0.6.6 might fix it.

Geordi: Choose your firefox version for cuc

Geordi 0.16+ supports running selenium tests with project-specific firefox versions.

Just update the gem. It will still default to using the old 5.0.1 firefox. If you want another one, add a file .firefox-version to your project, containing your preferred version.

geordi cucumber will prompt (and guide) you to install the given version. You can delete any old installation sitting in /opt/firefox-for-selenium if you have one.

Enable CSRF protection in Javascript tests

You might not know that Rails disables CSRF protection in tests. This means that if you accidentally forget to send the CSRF token for non-GET requests, your tests will be green even though your application is completely broken (a failed CSRF check usually logs out the user). Rails probably does this because CSRF protection sort of requires Javascript.

You want to enable CSRF protection in Cucumber scenarios that can speak Javascript. To do so, copy the a…

Rails 3/4: How to add routes for specs only

If you want to have routes that are only available in tests (e.g. for testing obscure redirects), you can use the with_routing helper – but that one destroys existing routes which may break a specs that require them to work.

To keep both "regular" and test routes, do this:

class MyApplicationController < ActionController::Base
  def show
    render text: 'Welcome to my application'
  end
end

test_routes = Proc.new do
  get '/my_application' => 'my_application#show'
end
Rails.application.routes.ev...

ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection

I recently experienced the error ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection. Apparently this happens when there is a timeout during query execution. In order to fix this you can reconnect to your db.

Therefore either add reconnect: true to your database.yml for automatic reconnection when the error occurs or catch the error and manually and reconnect explicitly via ActiveRecord::Base.connection.reconnect!

Be aware that reconnecting will have the following impact on your current connection:

  • Any active tr…

Enable NewRelic monitoring [for Rails] on specific hosts only

If you need to enable NewRelic monitoring on certain machines within the same Rails environment, a simple solution is to utilize the respective hostnames of you machines.

For example, if you have 8 application servers (e.g. app1.example.com, app2.example.com, …) and want to enable NewRelic on app1 and app2 only, utilize those steps to do so:

  1. Put the attached file into your config directory (config/custom_new_relic_configuration.rb).
  2. Specify on which hosts NewRelic should be enabled (see NEWRELIC_HOSTS constant and list …

Fix "An error occurred while installing debugger-linecache" with Ruby 1.9.3

You're better off using debugger-ruby_core_source:

gem install debugger-ruby_core_source

If you can't do this, try the following.


Here is how to fix the following error when installing the debugger gem fails:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension

Note: The following example is for a project using Ruby 1.9.3-p448 – adjust accordingly for your project.

  1. Fetch the source for your Ruby version, if you do not yet have it:

    rvm fetch ruby-1.9.3-p448
    
  2. Install t…

External content

Speeding up ssh session creation

Establishing a new SSH connection usually takes only a few seconds, but if you’re connecting to a server multiple times in succession the overhead starts to add up. If you do a lot of Git pushing and pulling or frequently need to SSH to a dev server, you’ve probably felt the pain of waiting for SSH to connect so you can get back to doing work.

Cucumber: Detect if the current Capybara driver supports Javascript

Copy the attached file to features/support. This gets you a convenience method:

Capybara.javascript_test?

Is true for Selenium, capybara-webkit, Poltergeist and a custom driver called :chrome (which we sometimes like to use for Selenium+Chrome).

Similar sounding but completely different card: Detect if a Javascript is running under Selenium WebDriver (with Rails)

How to change the locale of a PostgreSQL cluster

There may be reasons to change the locale of your Postgres cluster. A popular one is your development system's locale being used by default (which may be annoying). Here is how to do that.

Beware: By following the steps below, you will drop and recreate your cluster. You will lose all data (including roles). Instructions below include a procedure for dumping and restoring all cluster data (including roles). While it worked at the time of writing, you should have extra backup strategies for a production database.

  1. Find the cluster you…
View
3474 cards