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.

35 lines
855 B

  1. %w[sinatra dm-core dm-migrations haml sass coffee_script].each{ |lib| require lib }
  2. DataMapper.setup(:default, File.join("sqlite3://",settings.root,"development.db"))
  3. class Robot
  4. include DataMapper::Resource
  5. property :id, Serial
  6. property :top, Integer, default: proc { |m,p| 1+rand(6) }
  7. property :middle, Integer, default: proc { |m,p| 1+rand(4) }
  8. property :bottom, Integer, default: proc { |m,p| 1+rand(5) }
  9. end
  10. DataMapper.finalize
  11. get ('/styles.css'){ scss :styles }
  12. get('/application.js'){ coffee :application }
  13. get '/' do
  14. @robots = Robot.all
  15. haml :index
  16. end
  17. post '/build/robot' do
  18. robot = Robot.create
  19. if request.xhr?
  20. haml :robot, { layout: false, locals: { robot: robot } }
  21. else
  22. redirect to('/')
  23. end
  24. end
  25. delete '/delete/robot/:id' do
  26. Robot.get(params[:id]).destroy
  27. redirect to('/') unless request.xhr?
  28. end