-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
198 lines (169 loc) · 4.6 KB
/
test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
require 'rtasklib'
require 'sinatra'
class App < Sinatra::Base
configure do
register Sinatra::Partial
end
before { load_task_warrior }
get '/' do
redirect '/tasks', 303
end
get '/tasks' do
@tasks = get_tasks params
@title = 'Recent Tasks'
@header = "#{@project} Listing"
@priorities = priority_key
content = partial :index
haml :section, locals: { content: content, new: {name: 'New Task', href: '/tasks/new?project=' + @project.to_s} }
end
def get_tasks(params)
if @project = params[:project]
project = if project == 'Unassigned'
nil
else
@project
end
@tw.some(dom: {project: project}).sort_by(&:urgency).reverse
else
@tw.all.sort_by(&:urgency).reverse
end
end
def display_attributes
[
:description,
:project,
:price,
:status,
:due,
:priority,
:end,
:modified
]
end
post '/tasks/done/:id' do
id = params[:id]
project = params[:project]
@tw.done!(ids: id)
path = (project == 'nil' || project == nil || project == '') ? '/' : '/tasks?project=' + project
redirect path, 302
end
post '/status' do
id = params[:id]
status = params[:status]
@tw.modify!('status', 'completed', ids: id)
end
post '/retask' do
id = params[:id]
system("task #{id} modify status:pending")
redirect '/done', 302
end
get '/done' do
@title = 'Completed Tasks'
@tasks = @tw.some(dom: {status: 'completed' }, active: false).sort_by(&:end).reverse
@header = 'Listing'
@priorities = priority_key
content = partial :index
haml :section, locals: { content: content}
end
get '/tasks/new' do
@action = '/tasks/create'
@shopping_items = format_shopping_items
@projects = format_for_view(projects)
@project = params[:project]
@priorities = priority_key
@priority = 'L'
@description = ''
@price = nil
@header = 'New Task'
@submit = 'Create Task'
content = partial :form
haml :section, locals: { content: content }
end
get '/test/' do
content_type :json
format_shopping_items
end
def format_shopping_items
tasks = @tw.some(dom: { project:'Shopping' }, active: false)
tasks.map do |task|
{title: task.description, price: task.price, project: task.project}
end.to_json
end
get '/tasks/edit/:id' do
id = params[:id]
task = @tw.some(ids: id).first
@action = "/tasks/update/#{id}"
@projects = format_for_view(projects)
@project = task.project
@priorities = priority_key
@priority = task.priority
@price = task.price
@description = task.description
@header = 'New Task'
@submit = 'Update Task'
content = partial :form
haml :section, locals: { content: content }
end
post '/tasks/update/:id' do
id = params[:id]
description = "'#{params[:description]}'"
due = params[:due_date] || nil
priority = params[:priority] || 'L'
project = "'#{params[:project]}'" || nil
project = nil if project == "'Unassigned'"
price = params[:price]
[:description, :due, :priority, :project, :price].each do |attr|
@tw.modify!(attr, eval(attr.to_s), ids: id)
end
redirect '/tasks', 302
end
def format_for_view(projects)
index = projects.find_index(nil)
projects[index] = 'Unassigned'
projects
end
post '/tasks/create' do
description = params[:description]
due_date = params[:due_date] || nil
priority = params[:priority] || 'L'
project = "'#{params[:project]}'" || nil
project = nil if project == "'Unassigned'"
price = params[:price]
@tw.add!(description, dom: {due: due_date, project: project, priority: priority, price: price})
redirect '/', 302
end
get '/projects' do
@items = format_for_view projects
@title = 'Projects'
@header = 'Listing'
content = partial :list
haml :section, locals: { content: content }
end
post '/tasks/remove/:id' do
id = params[:id]
@tw.delete!(ids: id)
redirect '/tasks', 302
end
get '/tasks/:id' do
id = params[:id]
@model = @tw.some(ids: id).first
@display_attributes = display_attributes
@header = 'Task Information'
content = partial 'tasks/show'
haml :section, locals: { content: content }
end
def load_task_warrior
@tw = Rtasklib::TW.new('~/.task')
end
def projects
@tw.all(active: false).map(&:project).uniq
end
def priority_key
{'H' => 'High',
'M' => 'Medium',
'L' => 'Low'}
end
def shopping_tasks
@tw.some(dom: { project: 'Shopping' }, active: false)
end
end