
Untitled
By: a guest on
Jun 26th, 2012 | syntax:
None | size: 1.09 KB | hits: 12 | expires: Never
# app.rb
['sinatra', 'sinatra/sequel', 'sass', 'logger', 'rack-flash'].each{|gem|require gem}
enable :sessions
use Rack::Flash
set :database, 'mysql://root:root@localhost/todo_app'
puts "the posts table doesn't exist" if !database.table_exists?('posts')
migration "create the posts table" do
database.create_table :posts do
primary_key :id
Integer :priority
String :title, :text => true
timestamp :created_at, :null => false
end
end
class Post < Sequel::Model
end
get '/' do
@posts = Post.order(:priority).all
@notice = flash[:notice]
erb :index
end
post '/create' do
if !params[:title].empty?
Post.insert(:title => params[:title])
flash[:notice] = "Todo Created"
else
flash[:notice] = "Todo needs title"
end
redirect '/'
end
post '/sortList' do
params[:item].each_with_index do |id, index|
Post[id].update(:priority => index+1)
end
end
get '/:id/delete' do
if params[:id]
@post = Post[params[:id]].delete
flash[:notice] = "Todo deleted"
else
end
redirect '/'
end
get '/style.css' do
scss :style
end