sharagoz.com

Rails + SQL Anywhere 10 on Linux

I recently had to make a rails app talk to an sqlanywhere database. The app was to be deployed on a linux server that runs nginx + passenger. This is a fairly simple process, but there arent a lot of information on it on the internet so I'm writing a quick article.

Read the full post

Charles Max Wood is putting out great screencasts

I think this guy deserves more attention than he seem to be getting.

Keep up the good work Max!

Read the full post

User stamping in Rails 3

If you used the user_stamp gem in Rails 2 to maintain creator_id and updater_id columns in every table, you'll find that it doesn't work in Rails 3. I have a patched version on my Github account that works in production mode in Rails 3, but falls victim to class reloading in development mode. Instead of fixing that by breaking the old API, I'm just gonna make a quick post here on how you can implement the functionality yourself without depending on a gem. It's so simple that it's not really worth an external dependency.

Add a new file to app/models called user_stamper.rb:

class UserStamper < ActionController::Caching::Sweeper
observe Post, Comment, User #replace with your own models

def before_validation(record)
return unless current_user
record.send(:creator_id=, current_user.id) if(record.new_record? && record.respond_to?(:creator_id=))
record.send(:updater_id=, current_user.id) if(record.changed? && record.respond_to?(:updater_id=))
end

private

def current_user
controller.send(:current_user) if(controller.respond_to?(:current_user))
end
end

You'll also need this line in the ApplicationController:

cache_sweeper :user_stamper

That's all there is to it.

Read the full post

Interacting with Javascript prompts with Capybara

I tried switching from Webrat to Capybara about 4 or 5 months ago, but aborted it due to some reason I can't recall. I think it was because the "have_selector" matcher wasn't implemented yet. I decided to give it another try today hoping to be able to implement some of those pending Javascript related specs Webrat was unable to handle.

And I was not disappointed, Capybara has excellent support for the selenium-webdriver. Look at this:

find('span', :text => 'Register your email address').click
prompt = page.driver.browser.switch_to.alert
prompt.send_keys('me@example.com')
prompt.accept
Email.first.address.should eq('me@example.com')

I am clicking a non-button/link that triggers a javascript prompt. I fill out the text field in this prompt and then click the "OK" button, and it works!

I'm in love with Capybara now.

Btw, converting the existing specs created for Webrat was a very easy job.
There was some syntax changes to the "have_selector" matcher, calls to "response" had to be replaced by "page", and the "contains" matcher had to be replaced by "have_content". Easy.

Right now I can't think of a single reason to choose Webrat over Capybara. Webrat isnt even maintained anymore as far as I can tell.

Read the full post

Rails exception handler gem released

Quick announcement:
I just released a gem called "Rails Exception Handler", which is based on an earlier blog post of mine on rails exception handling. Check out the project on Github

Read the full post

Changing the repository location for gitosis

This is how I did it on my company's git server, which resides on an Arch Linux box. Obviously, it's a very good idea to make a backup before starting a procedure like this.

To change the location of the repo you need to do 3 things:
1. Add a line to gitosis.conf telling it that the repo is not in the default location
2. Move the repository folder
3. Update a symlink to gitosis.conf

In the example below I am moving the repo from /srv/gitosis/repositories to /data/gitosis/repositories. Inside /srv/gitosis there is a symlink that looks like this, which has to be updated afterwards:
.gitosis.conf -> /srv/gitosis/repositories/gitosis-admin.git/gitosis.conf

Read the full post

Doctype TV needs more love

I can't belive these guys doesn't have more subscribers. They produce high quality screencasts on web UI related topics. Check them out and spread the word!
http://www.youtube.com/user/doctypetv

Read the full post

Rolling your own exception handler in Rails 3

In Rails 2 you could override ActionController's "rescue_action_in_public" to do your own exception handling.
For Rails 3 the request handling was re-written to take full advantage of Rack, so now you need to use Rack middleware to catch and handle the exceptions manually.
For an introduction to Rack middleware you can checkout Railscasts ep. 151.

I will show you how to create a middleware app that serves as a backend for tracking exceptions in your rails apps. Catch the exception, store the relevant data in the database, and display a custom error message to the user. That's what I will cover. The front end is not the scope of this post.

Read the full post