Larrikinism

It is the Confession, not the Priest that gives us absolution

Archive | RSS
October 21, 2010 at 6:13am
Home

rxvl: The making of

I’ve been meaning for some time to write my own blogging engine and now that I finally got the time I went for it. It was meant to be a learning excercise so I tried to use tools and libraries that I’d never used before. I ended up chosing Sinatra for the framework to write it in with an SQLite DB and using DataMapper as the ORM. Also, I intended to host it on Heroku. In this article I’ve described everything from setting up my development environment for sinatra to finally deploying it on Heroku. Enjoy!

Setting up

Our first goal is to get a basic, “Hello World!” Sinatra app running with passenger and apache. You most probably have apache and passenger setup, so feel free to skip the next bit. For those who don’t, you should. Setting up passenger is the easiest thing. I’ve written about it here . Set up a VirtualHost to point to a directory named public in the directory where we’re going to have our sinatra app (/path/to/app/public). Create this empty directory. No special configuration is required in the apcache config file just treat it like a normal rails app. Now in /path/to/app lets create the sinatra app in the file app.rb. For a basic Hello World! application it’ll include something like this:

          get '/' do
           
"Hello World!"
         
end

Next we need the rackup file, Sinatra being a rack framework. This is my config.ru:

          require 'sinatra'
         
require 'app.rb'
          root_dir
= File.dirname(__FILE__)
          ENV
['RACK_ENV'] ||= 'development'
         
set :environment, ENV['RACK_ENV'].to_sym
         
set :root, root_dir
          disable
:run
          run
Sinatra::Application

And that’s it. Point your browser to the ServerName that you’d specified in that VirtualHost config and you should see Hello World!

DataMapper

Now lets start writing the actual app. We start as with a normal rails app with the models. As you can imagine, we’d need a post model. You would also usually use a comments model but I want to use Disqus for my commenting system so I’ll demonstrate inter-model relationships with a category model. Post and Category have a has and belongs to many relationship. I’ll show you how to do that with DataMapper. So first the migrations. Create db directory and create the following migrations.rb file:

          DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3:///home/sapna/src/testing/db/datastore.sqlite3")
         
class Post
           include
DataMapper::Resource
           property
:id, Serial
           property
:title, String, :required => true
           property
:slug, String
           property
:file_name, String, :required => true
           property
:created_at, DateTime
           has n
, :categorizations
           has n
, :categories, :through => :categorizations
         
end
         
class Category
           include
DataMapper::Resource
           property
:id, Serial
           property
:name, String
           has n
, :categorizations
           has n
, :posts, :through => :categorizations
         
end
         
class Categorization
           include
DataMapper::Resource
           property
:id, Serial
         
           belongs_to
:category
           belongs_to
:post
         
end

The explanation:

  • The first line defines the link to the database. I’ve used an sqlite database here. Setting up a connection to a mysql or postgres DB should be no different. the ENV[‘DATABASE_URL’] bit is for heroku. My local database is the datastore.sqlite3 file. Once this is delpoyed it’ll get the information of the DB URL from heroku. DataMapper::Resource is a module that you will have to include in all your model classes. Since the migration is the first time I’m defining the model class Post I’m including it there.
  • Next the property lines. These as you might have guessed are used to define the feilds of the post. Since I’m defining a blogging engine to read the post from a file rather than a database there are some differences between the design here and a conventional Post.
  • Note here that unlike ActiveRecord the id field isn’t automatically created for you you’ll have to define it with the type Serial. DataMapper takes care of the automatic updation of the field if its of type Serial. Now coming to the HABTM relationship. Category as you can see has only one field: name. Categorization is the mapping table. it has only one property which is the id. Now relationships are defined with the has and belongs_to methods. If you’d wanted to define a one to many relationship from post to category the post class would have
  • has n :categories

    and the Category class would have

    belongs_to :post

    Extending the principle, you can see how what is shown above would work. This gives you Category.new.posts and Post.new.categories.