Search_spinner

Capistrano: Different usernames for each server

If you have different users for different servers, don’t use set :user. Encode the username into the server definition instead:

server "username@servername.tld", :app, :web, :cron, :db, :primary => true

Machinist's #make breaks on has_many associations when defining method `empty?`

Observed on Rails 2.3 and machinist 1.0.6

Like the title says, when you define the method empty? like in the following example, you may not longer use collection.make.

class Book

  has_many :pages

  def empty?
    pages.empty?
  end

end

Assuming

b1 = Book.find(1)
b2 = Book.find(2)

instead of expected

b1.pages.make #=> #<Page id: 1, book_id: 1>
b2.pages.make #=> #<Page id: 2, book_id: 2>

you’ll get

b1.pages.make #=> #<Page id: 1, book_id: 3>
b2.pages.make #=> #<Page id: 2,...

Backup your Mac to an encrypted local hard drive

There are many blog posts on encrypting backups, but none works for local drives.

#How to
- Encrypt the external backup drive using TrueCrypt or PGP or similar.
- Mount it. If possible, let this happen automatically.
- Tell Time Machine to use it for backup.

#What is NOT working
- Backing up to disk images as described in this blog post. Apparently, sparsebundle images & co. ar…

Accept or deny JavaScript confirmation dialogs in Capybara/Selenium

These methods are available to you:

page.driver.browser.switch_to.alert.accept
page.driver.browser.switch_to.alert.dismiss
page.driver.browser.switch_to.alert.text # the confirmation text

Spreewald gives you steps like these:

When I confirm the browser dialog
When I cancel the browser dialog

Also see Type text into Javascript prompt dialogs in Capybara/Selenium.

External content

mojombo/grit - GitHub

Grit gives you object oriented read/write access to Git repositories via Ruby.

External content

Rails 3.1.0 has been released!

jQuery as new default Javascript library, streaming response support, attr_accessible with roles, prepared statements, easier migrations.

Compose a regular expression from other RegExp objects

You can create a Regexp object from existing Regexp objects by using the interpolation syntax you know from strings:

re1 = /x/
re2 = /a#{re1}b/
'aaxbb' =~ re2 # => 1

Select an unknown option with Capybara

When you don’t know which options are available, but need to have an option selected, use this step.

When /^I select the second option from "([^"]*)"$/ do |id|
  second_option = find(:xpath, "//*[@id='#{id}']/option[2]").text
  select(second_option, :from => id)
end
External content

xdissent/ievms - GitHub

Microsoft provides virtual machine disk images to facilitate website testing in multiple versions of IE, regardless of the host operating system. Unfortunately, setting these virtual machines up without Microsoft’s VirtualPC can be extremely difficult. These scripts aim to facilitate that process using VirtualBox on Linux or OS X. With a single command, you can have IE6, IE7, IE8 and IE9 running in separate virtual machines.

ActiveRecord 3+ auto-converts times to UTC by default. Hilarity ensues.

Remember how Rails 2 came with an awesome feature that broke all code using Time.now or Time.parse?

This behavior is now the default for Rails 3. Disable it by adding the following to your config/application.rb:

config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false    

Let a Rails 3 application make a request to itself

Ever wondered how Rails talks to itself in a Cucumber feature? In Rails 3 you can do it like this:

def rack_env(path)
  { "rack.input" => {},
    "PATH_INFO"=>"#{path}",
    "REQUEST_METHOD"=>"GET" }
end

request = rack_env('/users/new')
response = Rails.application.call(request)
status, headers, body = response

puts status # e.g. 200
puts headers.inspect # hash of headers
puts body.body # html of response body

Instead of Rails.application you can also call any Rack application.

External content

Web Upd8: Ubuntu / Linux blog

Great blog with daily news and HOWTOs for Ubuntu, Linux and Gnome.

Turn off SSL for scenarios with Selenium

Selenium does not speak SSL because it uses WEBrick that doesn’t. When you use Selenium for Cucumber scenarios that visit pages with SSL, they will fail.

To turn off SSL only for scenarios that are executed on WEBrick, put this method into your application controller.

def ensure_proper_protocol
  request.headers['SERVER_SOFTWARE'].andand.include?('WEBrick') || super
end

dbconsole in Rails 3 requires the environment as the first argument

There is a bug in Rails 3’s dbconsole script, which makes the following command open a database console for the development environment:

rails dbconsole -p test

You need to write this instead:

rails dbconsole test -p

When Balsamiq Mockups won't let you load an image file

Did you check Copy to Projects Asset as … and there is an existing file with the same name in your project folder?

Render custom 404 page in Rails 2

Simple

Tell the application controller how to handle exceptions, here a RecordNotFound error.
Do this with the following line:

rescue_from ActiveRecord::RecordNotFound, :with => :render_404

This will call the method render_404 whenever a RecordNotFound error occurs (you could pass a lambda instead of a symbol, too).
Now write this method:

def render_404
  render 'errors/404', :status => '404'
end

Finally create a 404 document views/errors/errors.html.haml.

%h1 Record Not Found

.error The record ...

When Rails no longer renders changes in view templates or Sass stylesheets

Do you have page caching enabled for the development environment and there are cached pages lying around in public/?

Ruby: Convert a time string to your local time zone

If you have a time given in a different time zone than your local one, parsing will convert it for you:

>> Time.parse('September 2nd, 3pm PST')
=> 2011-09-03 01:00:00 +0200

Note that in pure Ruby you need to require "tzinfo" (Ruby 1.9) or require "time" (Ruby 1.8) for Time.parse to be available.

2722 cards