A Weekend on Rails - Day One

Posted by blackrat on November 17, 2006

Since I’ve been laid up and not supposedto be working, Ruby on Rails seemed like an interesting, low-impact thing to do. I’m a complete n00b when it comes to Ruby, so it was pretty much a jump in at the deep-end for me. I’d like to think that I was swimming with Ruby rather than sinking within a matter of hours,and I’d recoded a file autostore/autorename utility in a couple of days.

I picked up the basics of array handling, regular expression manipulation, and some of the syntax of Ruby learnt, I still hadn’t quite made the leap of faith into the “Convention rather than Configuration” paradigm, so I figured it was time to get more deeply immersed in the full Ruby/Rails methodologies by building a Rails based utility.

I like to do something useful when trying out something new. Not production ready coding, but enought to move a personal project ahead and I was writing a TV programme recording prioritisation module for my next stage refinement of the PVR project.
Requirements for this were to have an input which contains the Programme name, a single aka (if appropriate), imdb and tv.com id number, and a recording priority. At this top level, I didn’t care about episodes, season information or anything else. Next extension was going to be for Genres, but I didn’t want to worry about that yet.

I already have XML files which contain the information, and I’d decided to use SQLite V3 as the database, so the Rails task I set myself was to convert the XML to the database.

As I have complete control over the XML file, I didn’t require any validation. If there was a problem, I could trash the database, correct the source file and run again.

The form of the xml file was:

<programmes>
  <programme priority=”3″ name=”Time Gentlemen Please”>
  <programme priority=”4″ name=”Time Trumpet”>
  <programme priority=”1″ name=”Torchwood”>
  <programme priority=”1″ name=”Torchwood Declassified”>
</programmes>

with possible additional attributes of aka, imdb_com, tv_com which contain other programme names, and the imdb.com and tv.com ids for the programme.

I’d read a number of Rails articles about moving image files into the database, and decided that I would do this as an import file from a webform.

Since this is my first Rails program, I’ve decided to post it here so I can point and snigger at it later. Feel free to do the same. Assumption is that all of the build prerequisites have already been installed, and I can skip straight to code generation and development work.

First step is to create the rails application framework for this, which goes by the name of BorgTest. Since I’m using SQLite3, I have a slightly modified commandline for the creation.

rails –database=sqlite3 BorgTest

Checking inside the config/database.yml shows that the correct database is being used

# SQLite version 3.x
#   gem install sqlite3-ruby

development:
  adapter: sqlite3
  database: db/development.sqlite3

# Warning: The database defined as ‘test’ will be erased and
# re-generated from your development database when you run ‘rake’.
# Do not set this db to the same as development or production.

test:
  adapter: sqlite3
  database: db/test.sqlite3

production:
  adapter: sqlite3
  database: db/production.sqlite3

Next thing to do is generate a model and controller for the database and associated views.

script/generate controller admin
script/generate model programme admin

Since I already know the exact format of the XML file, I’m going to create a database table with exactly the same columns as the names of the XML attributes.

db/migrate/001_create_programmes.rb

class CreateProgrammes < ActiveRecord::Migration
  def self.up
    create_table :programmes do |t|
      t.column :name, :string
      t.column :aka, :string, :default => “”
      t.column :priority, :integer, :default => 0
      t.column :tv_com, :string, :default => “”
      t.column :imdb_com, :string, :default => “”
    end
  end
  def self.down
    drop_table :programmes
  end
end

Save the above file and run

rake migrate

to create the initial table. Add the scaffold :programme to the app/controllers/admin_controller.rb file

class AdminController << ApplicationController
  scaffold :programme
end

Fire up script/server and point the webrowser to http://localhost:3000/admin and check that it is working.

Ok. Initial build completed and I had the basic scaffolded CRUD application. Next post will cover the addition of the controller and model to actually upload the file and perform the import.

Trackbacks

Use this link to trackback from your own site.

Comments

You must be logged in to leave a response.