Browse Source

Convert app to manage lists of tasks

- modify existing routes
  - create new routes
  - modify index view
  - create list view
  - create additional styling
master
jimi 13 years ago
parent
commit
2f45a6df06
  1. 23
      main.rb
  2. 12
      public/styles.css
  3. 13
      views/index.haml
  4. 10
      views/list.haml

23
main.rb

@ -9,17 +9,24 @@ class Task
property :id, Serial property :id, Serial
property :name, String, required: true property :name, String, required: true
property :completed_at, DateTime property :completed_at, DateTime
belongs_to :list
end
class List
include DataMapper::Resource
property :id, Serial
property :name, String, required: true
has n, :tasks, :constraint => :destroy
end end
DataMapper.finalize DataMapper.finalize
get '/' do get '/' do
@tasks = Task.all
@lists = List.all(:order => [:name])
haml :index haml :index
end end
post '/' do
Task.create params[:task]
post '/:id' do
List.get(params[:id]).tasks.create params['task']
redirect '/' redirect '/'
end end
@ -39,3 +46,13 @@ get '/:task' do
@task = params[:task].split('-').join(' ').capitalize @task = params[:task].split('-').join(' ').capitalize
# haml :task # haml :task
end end
post '/new/list' do
List.create params['list']
redirect '/'
end
delete '/list/:id' do
List.get(params[:id]).destroy
redirect '/'
end

12
public/styles.css

@ -4,7 +4,6 @@
.tasks{ .tasks{
padding:0; padding:0;
list-style:none; list-style:none;
width:400px;
} }
.task{ .task{
position:relative; position:relative;
@ -34,3 +33,14 @@ form.delete input{
cursor:pointer; cursor:pointer;
border:none; border:none;
} }
.lists{
padding:0;
list-style:none;
overflow:hidden;
}
.list{
float: left;
width:23%;
margin:0 1%;
border-top:solid 5px #ccc;
}

13
views/index.haml

@ -1,7 +1,6 @@
%form{ action: '/', method: "POST" }
%input{ type: "text", name: "task[name]" }
%input.button{ type: "submit", value: "New Task >>" }
%h2 My Tasks
%ul.tasks
- @tasks.each do |task|
= render(:haml, :task, locals: { task: task })
%form{ action: '/new/list', method: "POST" }
%input{ type: "text", name: "list[name]" }
%input.button{ type: "submit", value: "+ List" }
%ul.lists
- @lists.each do |list|
= render(:haml, :list, locals: { list: list })

10
views/list.haml

@ -0,0 +1,10 @@
%li.list
%h2= list.name
%form.new(action="/#{list.id}" method="POST")
%input(type="text" name="task[name]")
%ul.tasks
- list.tasks.each do |task|
= render(:haml, :task, locals: { task: task })
%form.destroy(action="/list/#{list.id}" method="POST")
%input(type="hidden" name="_method" value="DELETE")
%input(type="submit" value="✗")