public
Description: The micro app I built for my Rails talk -- Microapp for social good - help pay Jeff's bills
Homepage:
Clone URL: git://github.com/kastner/jeff4good.git
jeff4good / good.rb
100644 45 lines (37 sloc) 0.925 kb
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
require 'rubygems'
require 'sinatra'
require 'activerecord'
 
Dir["models/*"].each { |m| require m.gsub(/.rb$/,'') }
 
before do
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3", :database => "db.sqlite3"
  )
  
  # always send utf-8
  header "Content-Type" => "text/html; charset=utf-8"
  
  # set css body id
  @body_id = "home"
end
 
get "/" do
  @stuffs = Stuff.find(:all)
  total_pledged = Stuff.sum('worth', :conditions => "pledged_to IS NOT NULL")
  @percent = ((total_pledged || 0) / 10_000) * 100
  erb :index
end
 
post "/" do
  @stuff = Stuff.new(
    :email => params[:email],
    :thing => params[:thing],
    :worth => params[:worth]
  )
  @stuff.save
  redirect "/"
end
 
put "/pledge/:id" do
  @stuff = Stuff.find(params[:id])
  @stuff.update_attributes(
    :pledged_to => params[:email],
    :pledged_at => Time.now
  )
  redirect "/"
end