Strange Symphonies The best way to predict the future is to invent it

4Jun/070

Fixtures Without Rails

So continuing my series of ActiveRecord Without Rails, I thought I would inspect another popular feature of ActiveRecord, Fixtures.

Fixtures are a great way to populate your database with an expected result set. It makes it easy to prepare your database for testing and presentation to your client.

Why would you do this? Well you've already got ActiveRecord without Rails working, and probably are using the Migrations without Rails. So why not continue with fixtures?

Note:

  • I won't go into getting ActiveRecord working without Rails, please refer to that post. It's dead simple.
  • All these commands are executed from the root directory of your non-rails application.

Creating the Fixtures Directory

The fixtures will need to be stored somewhere, so we'll just create the necessary directory for them to enjoy a rather peaceful life.

mkdir -p test/fixtures

Setting up the Rakefile

Now we'll need a Rakefile. If your not familiar with Rakefiles just create a file called: Rakefile File

Note:

  • If you were following my ActiveRecord Migrations Without Rails, this is the same Rakefile. I've changed the default task to execute to load the fixtures instead. You can opt to continue using the migrate task if you want. Just remember to call either rake migrate or rake fixtures, depending on which task you would like call, that isn't your default task.
  • If you were not following my ActiveRecord Migrations Without Rails, you can ignore the migrate task.

require 'active_record'
require 'active_record/fixtures'
require 'yaml'
require 'erb'

task :default => :fixtures # Your choice

desc "Load fixtures into the current database.  Load specific fixtures using FIXTURES=x,y"
task :fixtures => :environment do
  fixtures = ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(File.dirname(__FILE__), 'test', 'fixtures', '*.{yml,csv}'))
  fixtures.each do |fixture_file|
    Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
  end
end

desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
task :migrate => :environment do
  ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
end

task :environment do
  ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))
  ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
end

Your First Fixture

Now we'll create our first fixture, and save it into test/fixtures/users.yml File. The filename corresponds to the table for where the fixtures will be loaded into.

aizatto:
  name: Ezwan Aizat Bin Abdullah Faiz

Warning:

  • Loading fixtures is destructive. It will destroy everything in the table.
  • This task loads all fixtures into the database. We'll cover specifying individual fixtures later.

Now the fixture is ready for loading and we execute:

rake

Loading Specific Fixtures

By default all fixtures are loaded into the database. Rather than loading all the fixtures, you can also specify which fixtures you want to load by appending them to a FIXTURES argument.

rake FIXTURES=users,roles

Embedded Ruby in Fixtures

In the same way like Rails, you can also continue to embed ruby inside your fixtures, giving you more flexibility to your hour models will be loaded.

aizatto:
  name: Ezwan Aizat Bin Abdullah Faiz
  created_at: <%= 1.day.ago %>

Conclusion

So by now you've probably accumulated quite a bit of knowledge in using ActiveRecord and its features outside of Rails.

So lets hear it, what else would you like to see?

Related posts

Tags

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.