Sinatra To Do List in 60 Lines of Code

One of the nice things about Sinatra is you can build apps in any way you want and you only need one file.

I’ve been playing around over the weekend and managed to build a very simple To Do list app, very similar to SuperDo. But it only uses one file and exactly 60 lines of code - including views!

It works like a charm, using a REST api to create, update and delete tasks. Completed tasks are crossed out and blank tasks aren’t allowed (although there’s no error message). Oh, and it also uses HTML5, so it must be super cool!

I don’t think I can make this any shorter - anybody got any suggestions?

require 'sinatra'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Task
  include DataMapper::Resource
  property :id,           Serial
  property :name,         String, :required => true
  property :completed_at, DateTime
get '/' do
  @tasks = Task.all
  haml :index, :format => :html5
post '/' do
  Task.create(:name => params[:name])
  redirect '/'   
get '/:id' do
  @task = Task.get(params[:id])
  haml :edit, :format => :html5
put '/:id' do
  task = Task.get(params[:id])
  task.completed_at = params[:completed] ? : nil = (params[:name]) ? (redirect '/') : (redirect '/' +
delete '/:id' do
  redirect '/' 
@@ layout
!!! 5
    %title To Do List
    %style li.completed{text-decoration:line-through;}    
    %h1 <a title="Show Tasks" href="/">To Do List</a>
    = yield
@@ index
%form(action="/" method="POST")
  %input#name(type="text" name="name")
  %input(type="submit" value="Add Task!")
  -@tasks.each do |task|
    %li{:class => (task.completed_at ? "completed":  nil)}
@@ edit
%form(action="#{}" method="POST")
  %input(name="_method" type="hidden" value="PUT")
  %input#name(type="text" name="name"value="#{}")
  %input#completed{:name => "completed",:type => "checkbox",:value => "done",:checked => (@task.completed_at ? "checked":  nil)}
  %input(name="commit" type="submit" value="Update")
%form(action="#{}" method="POST")
  %input(name="_method" type="hidden" value="DELETE")
  %input(type="submit" value="Delete") or <a href="/">Cancel</a>
blog comments powered by Disqus