Sitepoint demo app for learning to use Sinatra
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

62 lines
1.1 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. require 'sinatra'
  2. require 'data_mapper'
  3. require 'haml'
  4. require 'sass'
  5. class Task
  6. include DataMapper::Resource
  7. property :id, Serial
  8. property :name, String, required: true
  9. property :completed_at, DateTime
  10. belongs_to :list
  11. end
  12. class List
  13. include DataMapper::Resource
  14. property :id, Serial
  15. property :name, String, required: true
  16. has n, :tasks, :constraint => :destroy
  17. end
  18. DataMapper.finalize
  19. get '/styles.css' do
  20. content_type 'text/css', :charset => 'utf-8'
  21. scss :styles
  22. end
  23. get '/' do
  24. @lists = List.all(:order => [:name])
  25. haml :index
  26. end
  27. post '/:id' do
  28. List.get(params[:id]).tasks.create params['task']
  29. redirect '/'
  30. end
  31. delete '/task/:id' do
  32. Task.get(params[:id]).destroy
  33. redirect '/'
  34. end
  35. put '/task/:id' do
  36. task = Task.get params[:id]
  37. task.completed_at = task.completed_at.nil? ? Time.now : nil
  38. task.save
  39. redirect '/'
  40. end
  41. get '/:task' do
  42. @task = params[:task].split('-').join(' ').capitalize
  43. # haml :task
  44. end
  45. post '/new/list' do
  46. List.create params['list']
  47. redirect '/'
  48. end
  49. delete '/list/:id' do
  50. List.get(params[:id]).destroy
  51. redirect '/'
  52. end