Fixing "Lock obtain timed out: SimpleFSLock" for Solr

If your application raises an error like ...
Couldn't connect to the Solr server at http://127.0.0.1:8983/solr. 500 "Lock_obtain_timed_out_SimpleFSLock(...)"

... and if your jetty.request.log contains entries such as ...
127.0.0.1 - - [13/09/2011:12:42:23 +0000] "POST /solr/update HTTP/1.1" 500 4412
127.0.0.1 - - [13/09/2011:13:37:03 +0000] "POST /solr/update HTTP/1.1" 500 4309

... you probably have a lucene-...-write.lock file lying around in your Solr data directory that is not being cleaned up properly. This causes...

A few git tips you didn't know about

Random list of useful git commands for advanced users. I found some of them useful.

Line wrap text from a Ubuntu Linux terminal

You can use fold:

fold -sw 60

You can now paste your texts. fold will echo them back, word-wrapped after 60 columns. Exit with Ctrl+C or Ctrl+D.

You can also use files for input and output:

fold -sw 60 input.txt > output.txt

Open a Nautilus window in your terminal's working directory

In order to open a Nautilus window in your terminal's working directory, you can say:

nautilus .

Dragging a file into your terminal pastes the file path

When you drag a file from a Nautilus window into a terminal window, the file's path will be pasted into the terminal. This also works with multiple files.

Auto-generate state_machine graphs as PNG images

The state_machine gem comes with a rake task that lets you generate PNG graphs from any model using state_machine.

Install the required dependencies like this:

sudo apt-get install graphviz
sudo gem install ruby-graphviz

You can now generate a graph like this:

rake state_machine:draw CLASS=ModelUsingStateMachine

Replace ModelUsingStateMachine with the name of your model class.


If it the raketask does not exist for you, add to Rakefile (in your pr...

"Address already in use" with autossh

If you get an error "Adress already in use" with autossh check with lsof or netstat if something already listen on the Ports you want to use.

There are three Ports you have to look at. If your Command look like this:

/usr/local/bin/autossh -f -M 5100 -g -N -C -L 8080:127.0.0.1:80 example.com -i ~/.ssh/id_rsa -l user

The following three ports need to be available:

  1. 8080: The Port you want the tunnel to listen
  2. 5100: The Autossh Monitoring Port
  3. 5101: Autossh also uses the Monitoring Port +1

Defining custom errors in Ruby

class Errormaster
  CoffeeIsOut = Class.new(StandardError)
  
  # is prettier than
  class CoffeeIsOut < StandardError; end
end

Reference such an error class with Errormaster::CoffeeIsOut.

How to install a frozen version of Firefox for your Selenium tests

Whenever Firefox updates, all your Cucumber features that use Selenium break. This is annoying.

In order to remedy this, version 0.5.0 of our geordi gem comes with a script that helps you create an unchanging version of Firefox for your Selenium tests. In particular, this new copy of Firefox will have the following properties:

  • It won't update itself with a newer version
  • It can co-exist with your regular Firefox installation (which you can update at will)
  • It will use a profile separate from the one...

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

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

Note

If your regular expression contains backreferences like \1, they may no longer refer to the correct capture group after concatentation.

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

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,...

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

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

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...

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.

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?

How to 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

Note that recent versions of Selenium will automatically dismiss confirmation dialogs. [Here is how to fix that](https://makandracards.com/makandra/617366-configure-selenium-webdriv...

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    

Rendering a 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:

# application_controller.rb

  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...

mojombo/grit - GitHub

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

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.

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/?