Brandon Harris | Simple Sinatra ActiveRecord App with Migrations

Simple Sinatra ActiveRecord App with Migrations

I make a lot of stuff using Ruby on Rails, so why bother with Sinatra? Well, its fast, has a very small footprint, and it’s fun.

I decided to build a simple posting app as a test for Sinatra. My example doesn’t do much, but it was fun to learn about all the little parts of Rails that I take for granted everyday.
The first thing I did was start with the hello world example.

1 require 'rubygems'
2 require 'sinatra'
3 get '/' do
4 "This is my first Sinatra test."
5 end

I fired up the server

ruby test.rb

Cool.

I really like Haml, so I created a file test/views/index.haml

%h1 This is my first Sinatra test.

and edited my hello world code:

1 require 'rubygems'
2 require 'sinatra'
3 require 'haml'
4 get '/' do
5   haml :index
6 end

Fired up the server and once again was justified in my simple attempts. Cool, what about databases? We all know and love ActiveRecord, so lets start there.

 1 require 'rubygems'
 2 require 'sinatra'
 3 require 'haml'
 4 require 'active_record'
 5 
 6 ActiveRecord::Base.establish_connection(
 7   :adapter => 'sqlite3',
 8   :dbfile =>  'db/test.sqlite3.db'
 9 )
10 
11 #Models
12 class Post < ActiveRecord::Base
13 end
14 
15 get '/' do
16  @posts = Post.all
17  haml :index
18 end
19 
20 #Posts
21 get '/posts' do
22   @posts = Post.all
23   haml :'posts/index'
24 end
25 
26 get '/posts/new' do
27   @post = Post.new
28   haml :'posts/new'
29 end
30 
31 post '/posts' do
32   @post = Post.new(params[:post])
33   if @post.save
34     redirect "/posts/#{@post.id}"
35   else
36     "There was a problem saving that..."
37   end
38 end
39 
40 get '/posts/:id' do
41   @post = Post.find(params[:id])
42   haml :'posts/show'
43 end

This will work, if you manually create your own database in sqlite. But I really like migrations. So I created a Rakefile.

 1 namespace :db do
 2   task :environment do
 3     require 'active_record'
 4     ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :dbfile =>  'db/test.sqlite3.db'
 5   end
 6 
 7   desc "Migrate the database"
 8   task(:migrate => :environment) do
 9     ActiveRecord::Base.logger = Logger.new(STDOUT)
10     ActiveRecord::Migration.verbose = true
11     ActiveRecord::Migrator.migrate("db/migrate")
12   end
13 end

You have to manually create your migration file in db/migrations, and when you run rake db:migrate this rakefile will work for you. Of course, you will have to edit this particular one to use a different database other than sqlite. Perhaps you could create a configuration file using YAML?

You can see where this is going. Sinatra is very lightweight, yet it gives you the elements that you are used to in Rails. If you wanted to use something other than ActiveRecord, you can go ahead and do that easily. Don’t like Migrations? Don’t use them.

I’ll be using it, and keeping it in my toolbelt. Try it out, it’s refreshingly simple if you spend your days toiling around in Rails.

I did some minor cleanup on the above code and threw it on my github which you can get here.

Tags

  • e (1)