Friday 20 November 2009

Getting Threads in Rails

So you want threads in rails? Add the following 2 lines to your production.rb file:

config.threadsafe!
config.eager_load_paths << "#{RAILS_ROOT}/lib"

For more reading, checkout: http://m.onkey.org/2008/10/23/thread-safety-for-your-rails

Tuesday 27 October 2009

Rails: Unpacking your gems into vendor/gems

Okay you have your marvelous gem but want to put it into the vendor directly.

No worries. In this example I will be working with the "marc (0.3.0)" gem.
  1. Edit the environment.rb file and under the "Rails::Initializer.run do |config|" put the following line: config.gem 'marc'
  2. Next "rake gems:unpack GEM=marc" where GEM=gem_name - don't worry about the version number.
All done. You should have a new directory under vendor/gems/gem_name.

Troubleshooting. If this doesn't quite work, you might want to try:
  • Create the gems directory under vendor yourself
  • It may complain that it lacks the specification file. If so go to the vendor/gems/gem_name directory and run: gem specification gem_name > .specification
If this still doesn't work ... then google or bing yourself to death.

Monday 26 October 2009

Viewing RDoc for installed gems

Pretty simple:

gem server

It's then available on localhost:8808

Pretty cool. It's like having available doc for JAR files. Sort of.

Wednesday 21 October 2009

Metaprogramming in Ruby is awesome

Well I had to override the behaviour of the Oracle Enhanced Adapter when creating sequence names. By default it creates them as "tablename_seq" where our database was "sq_tablename".

After reading this post. I made my change and placed it under the config/initializers directory.

But the concept of metaprogramming in Ruby is really intriguing.

Monday 19 October 2009

Netbeans Ruby Oracle OCI Mac Problem

Okay. You are using netbeans and connecting to Oracle. You have installed the Oracle Instant Client and have set the DYLD_LIBRARY_PATH='/instantclient' in your shell. All is well if you run your rails app from the command line.

Right.

But when you run it through Netbeans you get the error telling you to some effect:

"ERROR: ActiveRecord oracle_enhanced adapter could not load ruby-oci8 library. Please install ruby-oci8 library or gem."

The explanation is that for some reason Netbeans on the mac does not pick up the settings set say in your .bashrc file. The easiest solution is to add the line:

"source ~/.bashrc" to your netbeans startup script.

Now Netbeans will pick up all your paths.

Thursday 3 September 2009

Excuting Rails Unit Tests without blowing away your database

Okay ...

I have my nice shiny rails app. I have written a heap of tests. But I don't want to blow the TEST database away each time I execute rake test:units.

Be aware when you use say "rake test" it calls "rake db:test:prepare" which essentially blows away your test database and reloads it.

There are a couple of approaches:

One: Run the test through Ruby and not Rake
-------------------------------------------
Run your test: ruby -I test test/unit/my_test.rb

The -I flag ensures that the file /test/test_helper.rb is loaded.

You could then write your own rake task to run all the unit tests.

Two: Write your own rake task to use instead of rake test:units
---------------------------------------------------------------
In this approach. The aim is to ensure that the "db:test:prepare" task is not called. You do this by clearing the prerequisites for "rake test:units"

i.e.
task :test_units do
Rake::Task['test:units'].prerequisites.clear
Rake::Task['test:units'].invoke
end

You then invoke your task: "rake test_units"
Also call "rake -T" to see your task listed.

For more details:
Check out Paul Barry's post
Jay Field's post

Tuesday 31 March 2009

Rails partials are awesome

This is a great blog post on partials.

http://addictedtonew.com/archives/149/a-bit-on-rails-partials/

Partials are so stealth!!