Skip to content

Commit

Permalink
New Sinatra app instead of Monrb app as instructed by the Heroku guy …
Browse files Browse the repository at this point in the history
…at CodeConf 2011

Signed-off-by: Ilya Shindyapin <ilya@shindyapin.com>
  • Loading branch information
license2e committed Apr 10, 2011
1 parent 095e19c commit 5966ff3
Show file tree
Hide file tree
Showing 24 changed files with 7,306 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gems
Expand Up @@ -2,4 +2,7 @@ sinatra
tilt
data_mapper
dm-postgres-adapter
dm-sqlite-adapter
dm-sqlite-adapter
haml
content_for2
json
4 changes: 1 addition & 3 deletions .gitignore
@@ -1,3 +1 @@
.idea
db/*
tmp/*
.idea
30 changes: 30 additions & 0 deletions README.markdown
@@ -0,0 +1,30 @@
# Here is a sample/template sinatra application ready for deployment to [Heroku](http://heroku.com).

**NOTE** This is for heroku.com and not herokugarden.com

* Create an acount in seconds at [Heroku](http://heroku.com/signup).
* Install the gem `sudo gem install heroku`.
* If you do not have an SSH key
you'll need to [generate
one](http://heroku.com/docs/index.html#_setting_up_ssh_public_keys)
and [tell Heroku about
it](http://heroku.com/docs/index.html#_manage_keys_on_heroku)
* Clone this repo `git clone git://github.com/sinatra/heroku-sinatra-app [appname]`
* `cd /path/to/project`
* `heroku create [optional-app-name]` (You can rename your app with `heroku rename`)
* `git push heroku master`

**NOTES**

The small amount of code is heavily commented. If you have questions
or comments please use [the channels provided](http://www.sinatrarb.com/contributing.html) by the Sinatra community as
we at Heroku are heavily involved in Sinatra. Please use the Heroku [mailing list](http://groups.google.com/group/heroku) if you
have non Sinatra questions.

See [these guidelines](http://www.sinatrarb.com/contributing.html) for
contributing or bugs.

Happy Development and Deploying!

Sincerely,
The Heroku Team
107 changes: 107 additions & 0 deletions codeconf-2011-open-seats.rb
@@ -0,0 +1,107 @@
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-migrations'
require 'dm-timestamps'
require 'logger'
require 'haml'
require 'sinatra/content_for2'
require 'json/pure'

configure :development do
DataMapper::Logger.new('tmp/seatr-debug.log', :debug)
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/db/development.db")
end

configure :production do
# Configure stuff here you'll want to
# only be run at Heroku at boot

# TIP: You can get you database information
# from ENV['DATABASE_URI'] (see /env route below)
end

class Seat
include DataMapper::Resource

property :id, Serial
property :loc, String
property :twitter, String
property :taken, Boolean, :default => true
property :created, DateTime
property :updated, DateTime

end

DataMapper.finalize
#DataMapper.auto_migrate!
DataMapper.auto_upgrade!

#=end

get "/" do
@seats = Seat.all
@title = "Open Seats"
if @seats.count == 0 then
redirect '/create'
end
haml :home
end

get "/create" do
rows = 17
cols = 21

for i in 0..rows
for j in 0..cols

s = Seat.new(
:loc => "seat-#{i}-#{j}",
:taken => true,
:created => Time.now
)

s.save

end
end

redirect '/'

end

get "/seats.json" do
@seats = Seat.all(:order => [ :loc.asc ])
data = {}

currentRow = nil
temp = nil
@seats.each do |seat|
=begin
if seat.row != currentRow then
data[seat.row] = {}
currentRow = seat.row
end
=end
data[seat.loc] = {}

#data[seat.row][seat.col] = seat.taken ? (seat.twitter ? seat.twitter : "x") : nil
data[seat.loc]["taken"] = seat.taken
data[seat.loc]["twitter"] = seat.twitter

end

content_type :json
data.to_json
end

get "/update/:loc/mark/:taken[/]?" do
seat = Seat.first(:loc => params[:loc])
#"#{seat.inspect}"
seat.update(:taken => (params[:taken] != 'open'), :twitter => params[:taken], :updated => Time.now)
if seat.save then
#redirect '/'
"#{seat.taken}"
end
end
6 changes: 6 additions & 0 deletions config.ru
@@ -0,0 +1,6 @@
require 'codeconf-2011-open-seats'

## There is no need to set directories here anymore;
## Just run the application

run Sinatra::Application
1 change: 1 addition & 0 deletions db/.gitignore
@@ -0,0 +1 @@
*.db
53 changes: 53 additions & 0 deletions public/css/style.css
@@ -0,0 +1,53 @@
body {
background: #111111;
color: #ddd;
font-family: "Helvetica Neue", Helvetica, Liberation Sans, Arial, sans-serif;
}

#page {
width: 960px;
margin: 0 25px;
}

h1{
color: #aaa;
text-align: center;
}

h1 img {
width: 72px;
height: 72px;
}

h1 span {
font-size: 2em;
}

td {
width: 40px;
height: 40px;
text-align: center;
background-color: #FF8500;
}

td img {
width: 36px;
height: 36px;
}

td.filled {
background-color: #333333;
}

td.isle {
background: none;
width: 72px;
}

td.seat {
cursor: pointer;
}

#mark-seat-form {
display: none;
}
Empty file added public/images/.gitignore
Empty file.
Empty file added public/js/.gitignore
Empty file.
104 changes: 104 additions & 0 deletions public/js/app.js
@@ -0,0 +1,104 @@
$(function () {


$('#mark-seat-form button').bind('click', function () {
var value = $('#mark-seat-form input[name=status]:checked')[0].value;
var seatId = $('#mark-seat-form input[name=seat-id]').val();
if (value === 'twitter') {
value = $('#mark-seat-form input[name=twitter-username]').val();
}

var url = '/update/'+seatId+'/mark/'+ value +'/';
$.get(url, updateFromServer);
console.log(url);
$('#mark-seat-form').slideUp('slow');
return false;
});

var markSeat = function(seat, seatId) {

var taken = 'taken';
var $seat = $(seat);
var value = null;
if ( $seat.hasClass('filled') ) {
value = 'nottaken';
} else {
value = 'taken';
}

$('#mark-seat-form input[name=seat-id]').val(seatId);
$('#mark-seat-form').slideDown();

};

var rows = 17;
var cols = 21;
var i, j;
var tr, td, data;


for (i = 0; i < rows; i++) {
tr = $('<tr></tr>');
for (j = 0; j < cols; j++) {

var seatId = 'seat-'+i+'-'+j;
td = $('<td id="'+seatId+'"></td>');
if (j === 10) {
td.addClass('isle');
} else {
td.addClass('seat').click( (function (seatId) { return function () { markSeat(this, seatId); }; }(seatId)) );
}

tr.append(td);
}

$('#openseats').append(tr);
}



var displayData = function (data) {
var i, j;
var td;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
var seatId = 'seat-'+i+'-'+j;
td = $('#'+seatId);
//console.log(data[seatId]);
if (data[seatId].taken) {
td.addClass('filled');
if (data[seatId].twitter) {
if (td.find('img').length === 0) {
//console.log(data[seatId].twitter);
td.append('<img>');
}
var imgSrc ='https://api.twitter.com/1/users/profile_image/' + data[seatId].twitter;
//console.log(imgSrc);
td.find('img').attr({
'src': imgSrc,
'title': '@'+data[seatId].twitter,
'alt': '@'+data[seatId].twitter
}).css('opacity', .35).hover(function(){
$(this).css('opacity', 1);
}, function(){
$(this).css('opacity', .35);
});
}
} else {
td.removeClass('filled').find('img').remove();
}
}

}
};

var updateFromServer = function () {
$.getJSON('/seats.json', function (d) { data = d; displayData(d); });
};


updateFromServer();
//setInterval( updateFromServer, 10000);

});

16 changes: 16 additions & 0 deletions public/js/jquery-1.5.2.min.js

Large diffs are not rendered by default.

Binary file added public/js/jquery-mobile/images/ajax-loader.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/js/jquery-mobile/images/icons-18-black.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/js/jquery-mobile/images/icons-18-white.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/js/jquery-mobile/images/icons-36-black.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/js/jquery-mobile/images/icons-36-white.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5966ff3

Please sign in to comment.