Permalink
Browse files

now using sinatra::default

  • Loading branch information...
1 parent 2ab6998 commit 72d6ed8d99c27b1b792f3aa2f8b6d8692058fbcb @igorgue committed Nov 22, 2009
Showing with 69 additions and 71 deletions.
  1. +68 −70 app.rb
  2. +1 −1 config.ru
View
138 app.rb
@@ -3,102 +3,100 @@
require 'sinatra'
require 'sequel'
-module SinatraBlog
- class App < Sinatra::Default
- dir = File.dirname(File.expand_path(__FILE__))
+class App < Sinatra::Default
+ dir = File.dirname(File.expand_path(__FILE__))
- set :public, "#{dir}/media"
- set :static, true
+ set :public, "#{dir}/media"
+ set :static, true
- configure do
- DB = Sequel.sqlite('blog.db')
+ configure do
+ DB = Sequel.sqlite('blog.db')
- DB.create_table? :posts do
- primary_key :id
- varchar :title
- varchar :body
- end
-
- DB.create_table? :tags do
- primary_key :id
- varchar :title
- end
-
- load "models/blog.rb"
+ DB.create_table? :posts do
+ primary_key :id
+ varchar :title
+ varchar :body
end
- helpers do
- include Rack::Utils
- alias :unsafe :escape_html
+ DB.create_table? :tags do
+ primary_key :id
+ varchar :title
end
- # index
- get '/' do
- @posts = Post.all
+ load "models/blog.rb"
+ end
- erb :index
- end
+ helpers do
+ include Rack::Utils
+ alias :unsafe :escape_html
+ end
- # new post form
- get '/posts/new' do
- erb :new
- end
+ # index
+ get '/' do
+ @posts = Post.all
- get '/posts/:id?' do
- @post = Post.find :id => params[:id]
+ erb :index
+ end
- if @post
- if params[:edit] == "true"
- erb :edit
- else
- erb :show
- end
- else
- redirect "/"
- end
- end
+ # new post form
+ get '/posts/new' do
+ erb :new
+ end
- # create a new post
- post '/posts' do
- post = Post.find_or_create :title => params[:title], :body => params[:body]
+ get '/posts/:id?' do
+ @post = Post.find :id => params[:id]
- if post
- redirect "/posts/#{post.id}"
+ if @post
+ if params[:edit] == "true"
+ erb :edit
else
- redirect "/"
+ erb :show
end
+ else
+ redirect "/"
end
+ end
- put '/posts/:id' do
- post = Post.find :id => params[:id]
+ # create a new post
+ post '/posts' do
+ post = Post.find_or_create :title => params[:title], :body => params[:body]
- if post
- post.title = params[:title]
- post.body = params[:body]
+ if post
+ redirect "/posts/#{post.id}"
+ else
+ redirect "/"
+ end
+ end
- post.save
- end
+ put '/posts/:id' do
+ post = Post.find :id => params[:id]
+
+ if post
+ post.title = params[:title]
+ post.body = params[:body]
- redirect "/posts/#{params[:id]}"
+ post.save
end
- delete '/posts/:id' do
- post = Post.find :id => params[:id]
+ redirect "/posts/#{params[:id]}"
+ end
- if post
- post.destroy
- end
+ delete '/posts/:id' do
+ post = Post.find :id => params[:id]
- redirect "/"
+ if post
+ post.destroy
end
- # error handling
- error do
- erb :status_500
- end
+ redirect "/"
+ end
- not_found do
- erb :status_404
- end
+ # error handling
+ error do
+ erb :status_500
+ end
+
+ not_found do
+ erb :status_404
end
end
View
2 config.ru
@@ -1,2 +1,2 @@
require "app"
-run SinatraBlog::App
+run App

0 comments on commit 72d6ed8

Please sign in to comment.