-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreddit.rb
111 lines (93 loc) · 2.71 KB
/
reddit.rb
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
106
107
108
109
110
111
%w{sinatra data_mapper haml sinatra/reloader dm-timestamps time-ago-in-words uri}.each { |lib| require lib}
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/example.db")
class Link
include DataMapper::Resource
property :id, Serial
property :title, String, :required => true
property :url, Text, :required => true, :format => :url
property :points, Integer, :default => 0
property :created_at, Time
has n, :votes
attr_accessor :score
def score
time_elapsed = (Time.now - self.created_at) / 3600
((self.points - 1) / (time_elapsed + 2) ** 1.8).real
end
def self.all_sorted_desc
all.sort_by(&:score).reverse
end
end
class Vote
include DataMapper::Resource
property :id, Serial
property :ip_address, String
property :created_at, Time
belongs_to :link
validates_uniqueness_of :ip_address, :scope => :link_id, :message => "You have already voted for this link."
end
DataMapper.finalize.auto_upgrade!
get '/' do
@links = Link.all :order => :id.desc
haml :index
end
get '/hot' do
@links = Link.all_sorted_desc
haml :index
end
post '/' do
Link.create(:title => params[:title], :url => params[:url])
redirect back
end
put '/:id/vote/:type' do
if params[:type].to_i.abs == 1
l = Link.get params[:id]
if l.votes.new(:ip_address => request.ip).save
l.update(:points => l.points + params[:type].to_i)
end
end
redirect back
end
__END__
@@ layout
%html
%head
%link(rel="stylesheet" href="/css/bootstrap.css")
%link(rel="stylesheet" href="/css/style.css")
%body
.container
#main
.title Learn Sinatra
.options
%a{:href => ('/')} New
|
%a{:href => ('/hot')} Hot
= yield
@@ index
#links-list
[email protected] do |l|
.row
.span3
%span.span
%form{:action => "#{l.id}/vote/1", :method => "post"}
%input{:type => "hidden", :name => "_method", :value => "put"}
%input{:type => "submit", :value => "⇡"}
%span.points
#{l.points}
%span.span
%form{:action => "#{l.id}/vote/-1", :method => "post"}
%input{:type => "hidden", :name => "_method", :value=> "put"}
%input{:type => "submit", :value => "⇣"}
.span6
.row
%span.link-title
%h3
%a{:href => (l.url)} #{l.title}
.link-host (#{URI.parse(l.url).host})
.row
%span.link-age
Submitted #{l.created_at.ago_in_words}
#add-link
%form{:action => "/", :method => "post"}
%input{:type => "text", :name => "title", :placeholder => "Title"}
%input{:type => "text", :name => "url", :placeholder => "Url"}
%input{:type => "submit", :value => "Submit"}