Archive for the 'Rails' Category

Google Product Search Xml Generator

April 16th, 2010 by pyrat

I have been working on a google base product search xml feed generator. It is in the form of a semi-customizable rake task. I have released this as a kind of rails plugin.

rake googlebase:generate generates an xml file of your products.

Configuration

To configure, add Rails.root/config/google_base.yml

Example google_base.yml file in files/google_base.yml

As the structure of the xml is dependant on your product models I have left it to the programmer to modify the google.rake file and set the correct instance methods for returning product information, along with the named_scope for returning live products.

TODO:

Instead of editing this rake file manually it would be good to add a plugin type class method which can take the configuration of your products and generate this automatically.

Its is available on github

Jquery Assertion for Rails

April 10th, 2010 by pyrat

When doing functional testing of ajax actions it is nice to be able to test the format of the javascript returned to the browser.

When using rjs I was using the excellent arts testing library, now I have moved to raw jquery instead. The method below provides a short hand way to test simple jquery methods. it can be built upon for sure but this little regex is quite powerful.

  def assert_jquery(effect, id)
    assert_match(/\$\(['"]+##{id}['"]+\).#{effect}\(.*\);/, @response.body)
  end

Consider the jquery

$("#announcement").hide();

  assert_jquery('hide', 'announcement')

This currently only works for id methods and when jquery isnt in compatibility mode. Also integration testing using something like env.js and harmony is a better way of javascript testing. Things like blue ridge actually integrate the env.js framework into a rails plugin and are a great solution if you have to do some real heavy lifting.

However, this method brings maximum results for minimum effort.

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.
  • To access session_id you first have to call the session. Also, the method of accessing the session id has changed.

The following 2 lines are required to get the session id.

session[:session_id]
id = request.session_options[:id]

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