-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.rb
82 lines (72 loc) · 1.71 KB
/
init.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'rubygems'
require 'sinatra'
require 'datamapper'
require 'sqlite3'
DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/db/tasks.sqlite")
class Task
include DataMapper::Resource
property :id, Serial
property :name, Text
property :description, Text
property :due, Date
property :category, Text
property :completed, Boolean, :default => false
end
# index
get '/' do
@tasks = Task.all(:completed => false)
haml :index
end
# new
get '/new' do
haml :new
end
# create
post '/create' do
@task = Task.new
@task.attributes = {:name => params[:name],
:description => params[:description],
#:due => params[:due],
:category => params[:category]}
if @task.save
redirect '/'
else
@errors = []
@task.errors.each { |error| @errors << error }
redirect '/new'
end
end
# edit
get '/edit/:id' do
@task = Task.get!(params[:id])
haml :edit
end
put '/edit/:id' do
@errors = params[:errors]
@task = Task.get!(params[:id])
if @task.update_attributes(:name => params[:name],
:description => params[:description],
#:due => params[:due],
:category => params[:category],
:completed => params[:completed])
redirect "/#{@task.id}"
else
redirect '/edit/:id'
end
end
# destroy
get '/destroy/:id' do
@task = Task.get!(params[:id])
@task.destroy
redirect '/'
end
# show
get '/:id' do
@task = Task.get!(params[:id])
haml :show
end
# Routes for static files
get '/main.css' do
content_type 'text/css', :charset => 'utf-8'
sass :main
end