public
Last active

Robot Factory part 1 - Adding and Deleting Resources in Sinatra

  • Download Gist
main.rb
Ruby
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
%w[sinatra dm-core dm-migrations slim sass].each{ |lib| require lib }
 
DataMapper.setup(:default, ENV['DATABASE_URL'] || File.join("sqlite3://",settings.root, "development.db"))
 
class Robot
include DataMapper::Resource
property :id, Serial
property :top, Integer, :default => proc { 1+rand(6) }
property :middle, Integer, :default => proc { 1+rand(4) }
property :bottom, Integer, :default => proc { 1+rand(5) }
end
 
DataMapper.finalize
 
########### Routes ###########
get('/styles.css'){ scss :styles }
 
get '/' do
@robots = Robot.all
slim :index
end
 
post '/build/robot' do
robot=Robot.create
if request.xhr?
slim :robot, { :layout=>false, :locals => { :robot => robot } }
else
redirect to('/')
end
end
 
delete '/delete/robot/:id' do
Robot.get(params[:id]).destroy
redirect to('/') unless request.xhr?
end
 
__END__
########### Views ###########
@@layout
doctype html
html
head
meta charset="utf-8"
title Robot Factory
link rel="shortcut icon" href="/fav.ico"
link href="http://fonts.googleapis.com/css?family=Megrim|Ubuntu&v2" rel='stylesheet'
link rel="stylesheet" media="screen, projection" href="/styles.css"
/[if lt IE 9]
script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"
body
== yield
footer role="contentinfo"
p Building Quality Robots since 2011
 
@@index
h1 Robot Factory
form.build action="/build/robot" method="POST"
input.button type="submit" value="Build A Robot!"
-if @robots.any?
ul#robots
- @robots.each do |robot|
==slim :robot, :locals => { :robot => robot }
- else
h2 You Need To Build Some Robots!
@@robot
li.robot
img src="https://s3.amazonaws.com/daz4126/top#{robot.top}.png"
img src="https://s3.amazonaws.com/daz4126/middle#{robot.middle}.png"
img src="https://s3.amazonaws.com/daz4126/bottom#{robot.bottom}.png"
form.destroy action="/delete/robot/#{robot.id}" method="POST"
input type="hidden" name="_method" value="DELETE"
input type="submit" value="×"
 
@@styles
html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote, pre,abbr,address,cite,code,del,dfn,em,img,ins,kbd,q,samp,small,strong,sub,sup,var,b,i,dl,dt, dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article, aside, canvas, details,figcaption,figure,footer,header,hgroup,menu,nav,section, summary,time,mark,audio,video{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;line-height:1;}
 
body{font-family:ubuntu,sans;}
 
footer{display:block;margin-top:20px;border-top:3px solid #4b947d;padding:10px;}
 
h1{color:#95524C;margin:5px 40px;font-size:72px;font-weight:bold;font-family:Megrim,sans;}
.button{
background:#4b7194;color:#fff;
text-transform:uppercase;
border-radius:12px;border:none;
font-weight:bold;font-size:16px;
padding: 6px 12px;margin-left:40px;
cursor:pointer;
&:hover{background:#54A0E7;}}
#robots{list-style:none;overflow:hidden;margin:20px;}
 
.robot{
float:left;
width:100px;padding:10px 0;
position:relative;
form{display:none;position:absolute;top:0;right:0;}
&:hover form{display:block;}
 
form input{background:rgba(#000,0.7);padding:0 4px;color:white;cursor:pointer; font-size:32px;font-weight:bold;text-decoration:none;border-radius:16px;line-height:0.8;border:none; }
 
img{display:block;padding:0 10px;}}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.