Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 1.09 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # app.rb
  2. ['sinatra', 'sinatra/sequel', 'sass', 'logger', 'rack-flash'].each{|gem|require gem}
  3.  
  4. enable :sessions
  5. use Rack::Flash
  6.  
  7. set :database, 'mysql://root:root@localhost/todo_app'
  8.  
  9. puts "the posts table doesn't exist" if !database.table_exists?('posts')
  10.  
  11. migration "create the posts table" do
  12.  database.create_table :posts do
  13.   primary_key :id
  14.   Integer     :priority
  15.   String      :title, :text => true
  16.   timestamp   :created_at, :null => false
  17.   end
  18. end
  19.  
  20. class Post < Sequel::Model
  21. end
  22.  
  23. get '/' do
  24.  @posts = Post.order(:priority).all
  25.  @notice = flash[:notice]
  26.  erb :index
  27. end
  28.  
  29. post '/create' do
  30.  if !params[:title].empty?
  31.   Post.insert(:title => params[:title])
  32.   flash[:notice] = "Todo Created"
  33.  else
  34.   flash[:notice] = "Todo needs title"
  35.  end
  36.  redirect '/'
  37. end
  38.  
  39. post '/sortList' do
  40.  params[:item].each_with_index do |id, index|
  41.         Post[id].update(:priority => index+1)
  42.  end
  43. end
  44.  
  45. get '/:id/delete' do
  46.  if params[:id]
  47.   @post = Post[params[:id]].delete
  48.   flash[:notice] = "Todo deleted"
  49.  else
  50.  end
  51.  redirect '/'
  52. end
  53.  
  54. get '/style.css' do
  55.  scss :style
  56. end