Archive for the 'Rails' Category

Install Mysql Gem on Snow Leopard and Macports

March 8th, 2010 by pyrat

export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

Rails 2.3 Upgrade Notes

February 27th, 2010 by pyrat


In Need of Love and Care

I have written a number of rails applications over the past few years. While new projects are using rails 2.3 I have a number of older applications which I should have upgraded before but havent. Anyway, here is some little notes to take into account when upgrading to rails 2.3. Then from here you are placed well for the upcoming release of rails 3.

  • to_param method issues (fixnum to string)
  • test_helper modifications – class name (ActiveSupport::TestCase and all unit tests need to extend this)
  • add include ActionController::TestProcess to test_helper.rb
  • to silence spec warnings for old unpacked gems – Rails::VendorGemSourceIndex.silence_spec_warnings = true
  • sessions

ActionController::Base.session = {
  :key => '_session_name',
  :secret => 'd00cda5710eab8sdfsdf6c5fe165780074fa5027f50168a0bd0ae2832c05bb4f804a07228b220b67e'
}

In an initializer instead of environment.rb (some plugins access the session in a different manner.)

  • Truncate format is deprecated, update to include :length hash key.
  • If you are defining constants in environment.rb, move them to an initializer to make them play better with tests.

Christmas Card Labels Application

December 8th, 2009 by pyrat

santa

I have been keen to get my hands dirty with ExtJS, so jumped at the chance to make a christmas card list application which can print the list to labels; to stick on envelopes.

I have finally become mature enough to have a christmas card list along with my girlfriend, which is useful for keeping track of friends both in Trondheim and the United Kingdom.

To get it up and running on heroku it is pretty swift –

  git clone git://github.com/pyrat/christmas-card-labels.git
  cd christmas-card-labels
  sudo gem install heroku
  heroku create
  git push heroku master
  heroku rake db:migrate

You are now good to go, you may need to tweak the css (labels.css) to match your specific labels which can be a painful experience.

Default username / password is god / jul which is norwegian for merry christmas!

There is a demo version online

Configurable ActiveResource Site Variable

September 16th, 2009 by pyrat

flickr

Documentation on ActiveResource is fairly thin on the ground.

If you are using ActiveResource in the real world it is likely that you will want to use different webservices depending on what environment you are in. (Staging, Development, CI, Production etc.)

Heres how to do it. This assumes the REST api has been put under the api namespace and has been setup with HTTP Authentication.

config/initializers/config.rb

  raw_config = File.read(RAILS_ROOT + "/config/config.yml")
  GLOBAL = YAML.load(raw_config)
  APP_CONFIG = GLOBAL[Rails.env]

config/config.yml

  development:
      webservice: http://username:password@localhost:3001/api/
  test:
      webservice: http://username:password@localhost:3001/api/
  staging:
      webservice: https://username:password@staging.com/api/
  production:
      webservice: https://username:password@production.com/api/

app/models/incometype.rb_

  class IncomeType < ActiveResource::Base
    self.site = APP_CONFIG['webservice']
  end

Here is a great railscast on the subject.

Git Submodule Gotcha

August 12th, 2009 by pyrat

The gotcha comes when I switch branches back and forth between the “master” (no submodules, but with checked in source) and “dev” (where my submodule work is). Here’s what happens:

git checkout master

error: Untracked working tree file ‘vendor/plugins/acts_as_list/lib/active_record/acts/list.rb’ would be overwritten by merge.

Here’s a workaround…
To go back to a branch w/o submodules

rm -rf vendor/plugins
git checkout master
git checkout vendor/plugins

To go back to a branch w/ submodules

git checkout dev
git submodule update --init

Another gotcha, when I tried to merge dev into master I got “fatal: cannot read object… It is a submodule!” A solution is:

git merge -s  resolve

Original Article

Using mod_expires to speed up serving pages with apache

May 21st, 2009 by pyrat

This is rails specific to a certain extend and involves setting the expires header on static asset to make browsers cache. This can be dangerous so is best used with timestamped css and javascript assets.

By default: rails helpers will add a timestamp onto the end of the asset so when you redeploy the file the browsers will recache the file because the timestamp has changed.

eg.

  <script src="/javascripts/all.js?1242919746" type="text/javascript"></script>

To setup mod_expires apache on debian based:

  sudo a2enmod expires

Then in your config

  ExpiresActive On
  <FilesMatch "\.(js|css)$">
    ExpiresDefault "access plus 1 year"
  </FilesMatch>

You can easily add image types to this as well but often css loads images and designer often forget to use these helper methods. So in the real world just doing it with js and css makes it safer to do with maximum performance gain.

Installing git from source ubuntu

May 14th, 2009 by pyrat

When you have an old version of ubuntu but want a new version of git. And potentially have it already installed.

sudo apt-get update
sudo apt-get remove git-core
sudo apt-get install tcl8.4 tk8.4
sudo apt-get build-dep git-core
 
wget http://kernel.org/pub/software/scm/git/git-1.6.3.1.tar.gz
tar -zxvf git-1.6.3.1.tar.gz
cd git-1.6.3.1
./configure
make
sudo make install
 
 
sudo ln -s /usr/local/bin/git /usr/bin/git

Installing ImageMagick / Rmagick on Ubuntu Hardy

May 14th, 2009 by pyrat

Mainly to help me remember in the future. Might be useful to you also..

  sudo apt-get update
  sudo apt-get install imagemagick
  sudo apt-get install libmagick9-dev
  sudo gem install rmagick

Introducing Daily Photo – Scheduled Uploads to Flickr

May 9th, 2009 by pyrat

Daily Photo Scheduled Flickr Uploads
Daily Photo

Introducing daily photo a wee web application which lets you upload photos to flickr on a daily basis. This allows you to make the most of RSS feeds and social applications that link to your flickr account.

Daily photo will also be useful if you run a photo blog which is powered off flickr. The ability to decide when photos appear is functionality which flickr is sorely missing.

Daily photo is a wee app, written in hours rather than days. Functionality is as minimal as possible while getting the job done. Steps to getting your flickr uploads scheduled are as follows:

  • Go to daily photo
  • Sign up for an account
  • Check your email and confirm your email address
  • You will be redirected to flickr where you confirm that daily photo can have access to your account for uploading of photos.
  • Upload photos to daily photo with names, descriptions and tags. Define on what day you want the photo to be uploaded to flickr.
  • Sit back watch your flickr account update automagically!

Front page
Front page

Your Photos upload queue
Your Photos upload queue

So if you have a need for daily photo, your welcome! Fill your boots.

libxml-ruby on ubuntu

May 7th, 2009 by pyrat

  sudo apt-get install libxml2 libxml2-dev
  sudo gem install libxml-ruby