-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcise.rb
More file actions
515 lines (458 loc) · 15 KB
/
concise.rb
File metadata and controls
515 lines (458 loc) · 15 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# encoding: utf-8
require 'camping/session'
require 'htmlentities/string'
Camping.goes :Concise
module Concise
include Camping::Session
end
module Concise::Models
class Question < Base
validates_presence_of :body
validates_length_of :body, :maximum => 150
has_many :responses, :order => '((positive_votes / (positive_votes + negative_votes)) * 10) DESC, LENGTH(body) ASC'
end
class Response < Base
belongs_to :question
belongs_to :user
has_many :votes
validates_presence_of :body
validates_length_of :body, :maximum => 150
def score
((positive_votes.to_f / (positive_votes + negative_votes)) * 10).round
end
end
class User < Base
validates_uniqueness_of :name
has_many :responses
has_many :votes
end
class Vote < Base
belongs_to :user
belongs_to :response
end
class CreateConcise < V 0.1
def self.up
create_table :concise_questions do |t|
t.column :body, :string
t.datetime :created_at, :updated_at
end
create_table :concise_responses do |t|
t.column :user_id, :integer
t.column :question_id, :integer
t.column :body, :string
t.integer :positive_votes, :negative_votes
t.datetime :created_at, :updated_at
end
create_table :concise_users do |table|
table.string :name, :password, :email
end
create_table :concise_votes do |table|
table.integer :user_id, :response_id
table.boolean :positive
end
end
end
end
module Concise::Controllers
class Index < R '/'
def get
@new_questions = Question.find(:all, :order => 'created_at DESC', :limit => 20) || []
@popular_questions = Question.find(:all, :select => "concise_questions.*, COUNT(concise_responses.id) AS occurences",
:joins => :responses, :group => "concise_questions.id", :order => "occurences DESC", :limit => 20) || []
render :index
end
end
class Create
def get
#return unless admin?
return unless logged_in?
@question = Question.new
render :create
end
def post
#return unless admin?
return unless logged_in?
@question = Question.new(:body => input.body)
if @question.save
add_success("Question created")
redirect "/#{@question.id}"
else
@question.errors.full_messages.each { |msg| add_error(msg) }
render :create
end
end
end
class Update < R '/(\d+)/update'
def get(id)
#return unless admin?
return unless logged_in?
@question = Question.find_by_id(id.to_i)
render :update
end
def post(id)
#return unless admin?
return unless logged_in?
@question = Question.find_by_id(id.to_i)
if @question.update_attributes(:body => input.body)
add_success("Question updated")
redirect "/#{@question.id}"
else
@question.errors.full_messages.each { |msg| add_error(msg) }
render :update
end
end
end
class Destroy < R '/(\d+)/destroy'
def get(id)
return unless admin?
@question = Question.find_by_id(id.to_i).destroy
add_success("Question deleted")
redirect '/'
end
end
class Login < R '/login', '/logout'
def post
login input.username, input.password
if logged_in?
add_success("Logged in!")
redirect(Index)
else
add_error("Your username or password was incorrect.")
get
end
end
def get
logout
render :login
end
end
Logout = Login
class Signup
def get
@user = User.new
render :signup
end
def post
@user = User.new(:name => input.name, :password => input.password, :email => input.email)
if @user.save
add_success("User created.")
redirect '/login'
else
@user.errors.full_messages.each { |msg| add_error(msg) }
render :signup
end
end
end
class Show < R '/(\d+)'
def get(id)
@question = Question.find(id.to_i)
@resp = Response.new
render :show
end
def post(id)
return unless ensure_logged_in
@question = Question.find(id.to_i)
@resp = Response.new(:question => @question, :user_id => current_user.id, :body => input.body, :positive_votes => 1, :negative_votes => 1)
if @resp.save
add_success("Response posted")
redirect "/#{id}"
else
@resp.errors.full_messages.each { |msg| add_error(msg) }
render :show
end
end
end
class VoteUp < R '/responses/(\d+)/up'
def get(id)
return unless ensure_logged_in
@resp = Response.find(id.to_i)
existing_vote = @resp.votes.find_by_user_id(current_user.id)
if existing_vote
if existing_vote.positive?
add_error("You've already voted up this response.")
else
existing_vote.toggle!(:positive)
@resp.negative_votes -= 1
@resp.positive_votes += 1
@resp.save
add_success("Your vote has been changed from down to up.")
end
else
@resp.votes.create(:user => current_user, :positive => true)
@resp.increment!(:positive_votes)
add_success("Your vote has been cast.")
end
redirect R(Show, @resp.question)
end
end
class VoteDown < R '/responses/(\d+)/down'
def get(id)
return unless ensure_logged_in
@resp = Response.find(id.to_i)
existing_vote = @resp.votes.find_by_user_id(current_user.id)
if existing_vote
if !existing_vote.positive?
add_error("You've already voted down this response.")
else
existing_vote.toggle!(:positive)
@resp.negative_votes += 1
@resp.positive_votes -= 1
@resp.save
add_success("Your vote has been changed from up to down.")
end
else
@resp.votes.create(:user => current_user, :positive => false)
@resp.increment!(:negative_votes)
add_success("Your vote has been cast.")
end
redirect R(Show, @resp.question)
end
end
class Style < R '/style.css'
def get
@headers["Content-Type"] = "text/css; charset=utf-8"
@body = %{
* { margin: 0; padding: 0; font-family: Georgia, "Times New Roman", serif; font-size: 100%; }
body { background-color: #ffffff; color: #000000; padding: 1em; }
h1 { font-size: 200%; margin: 0 0; font-weight: bold; }
h1 a { color: #000000; text-decoration: none; }
h2 { font-size: 140%; margin: 0.8em 0 0.5em 0; font-weight: bold; }
h3 { margin: 0.8em 0; font-weight: bold; }
p { margin: 0 0 1em 0; }
ul { margin: 1em 0 1em 1.5em; }
label { width: 5em; display: block; float: left; text-align: right; padding: 0.2em 0.3em 0 0; }
input { width: 20em; }
input.submit { margin-left: 5.3em; width: 8em; }
form div { clear: left; margin: 0.5em; }
#header { padding-bottom: 0.5em; border-bottom: 1px solid #a0a0a0; margin-bottom: 1em; position: relative; height: 3em; }
#nav { float: right; }
#success { background-color: #008000; border: 1px solid #00ff00; padding: 0.5em; color: #ffffff; margin-bottom: 1em; }
#success ul, #errors ul { list-style-type: none; margin: 0; }
#errors { background-color: #a00000; border: 1px solid #ff0000; padding: 0.5em; color: #ffffff; margin-bottom: 1em; }
#footer { margin: 1em 0 0 0; padding-top: 1em; border-top: 1px solid #a0a0a0; }
.column { margin-right: 1em; float: left; width: 33%; }
.clear { clear: both; }
}
end
end
class ShowUser < R '/users/(.+)'
def get(name)
@user = User.find_by_name(name)
render :yours
end
end
#static stuff follows
class About
def get
render :about
end
end
end
module Concise::Helpers
def add_success(msg)
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
@state[:flash][:success] << msg
end
def add_error(msg)
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
@state[:flash][:errors] << msg
end
def label_for name, record = nil, attr = name, options = {}
errors = record && !record.valid? && record.errors.on(attr)
label name.to_s.humanize, { :for => name }, options.merge(errors ? { :class => :error } : {})
end
# login system
def current_user
@current_user ||= Concise::Models::User.find(@state.user_id) unless @state.user_id.blank?
end
alias logged_in? current_user
def login name, password
@current_user = Concise::Models::User.find_by_name_and_password input.name, input.password
@state.user_id = @current_user.id if @current_user
end
def logout
@state.user_id = nil
end
def ensure_logged_in
unless logged_in?
add_error("You must be logged in to do that.")
redirect '/'
end
logged_in?
end
def admin?
current_user.id == 1
end
def nice_date_time(date)
return date.strftime("%d/%m/%Y %I:%M%p")
end
def h(text)
CGI::escapeHTML(text)
end
end
module Concise::Views
def layout
xhtml_transitional do
head do
title "Concise"
link :rel => 'stylesheet', :type => 'text/css', :href => '/style.css', :media => 'screen'
end
body do
div.header! do
links = [a('Home', :href => '/'), a('About', :href => '/about')] + if logged_in?
["Logged in as #{a current_user.name, :href => R(ShowUser, current_user.name)}", a('Post a question', :href => '/create'), a('Logout', :href => '/logout')]
else
[a('Login', :href => '/login'), a('Signup', :href => '/signup')]
end
div.nav! { links.join(" | ") }
h1 { a("Concise", :href => '/') }
end
div.main! do
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
unless @state[:flash][:success].empty?
div.success! do
ul { @state[:flash][:success].each { |s| li { h s } } }
end
end
@state[:flash][:success] = []
unless @state[:flash][:errors].empty?
div.errors! do
p { "There were some errors:" }
ul { @state[:flash][:errors].each { |s| li { h s } } }
end
end
@state[:flash][:errors] = []
self << yield
end
p.footer! { "Created by #{a "Brenton Fletcher", :href => "http://blog.bloople.net"} in #{File.stat(__FILE__).size} bytes of code. " +
"<a href='mailto:i@bloople.net'>Email me</a>! Powered by " +
"#{a "Ruby", :href => "http://ruby-lang.org"} on #{a "Camping", :href => "http://github.com/camping/camping/"}." }
end
end
end
def index
div.column do
h2 { "Most recent questions" }
ul.concise do
@new_questions.each do |q|
li { a q.body, :href => R(Show, q) }
end
end
end
div.column do
h2 { "Most popular questions" }
ul.concise do
@popular_questions.each do |q|
li { a q.body, :href => R(Show, q) }
end
end
end
div.clear { " " }
end
def show
h2 { "Question" }
p { h @question.body }
if logged_in?# and admin?
h2 { "Modify Question" }
p { "#{a 'Edit', :href => R(Update, @question)} • #{a 'Delete', :href => R(Destroy, @question)}" }
end
h2 { "Responses" }
unless @question.responses.empty?
@question.responses.each do |response|
div.response do
p { "#{h response.body} • #{a response.user.name, :href => R(ShowUser, response.user.name)} • #{logged_in? ? (a '+', :href => R(VoteUp, response)) : ''} #{response.score} #{logged_in? ? (a '-', :href => R(VoteDown, response)) : ''}" }
end
end
else
p { "No responses yet. Hey, why not add one?" }
end
h3 { "Post a response" }
if logged_in?
form :method => :post, :action => R(Show, @question) do
div do
label_for :body, @resp
input.text :name => :body, :type => :text, :size => 40, :value => @resp.body
end
div do
input.submit :type => :submit, :value => 'Post response'
end
end
else
p { "You could post a response if you were #{a 'logged in', :href => '/login'}!" }
end
end
def _form
div do
label_for :body, @question
input.text :name => :body, :type => :text, :size => 40, :value => @question.body
end
div do
input.submit :type => :submit, :value => 'Save'
end
end
def create
h2 { "Create Question" }
form :method => :post, :action => '/create' do
_form
end
end
def update
h2 { "Update Question" }
form :method => :post, :action => R(Update, @question) do
_form
end
end
def login
form :action => R(Login), :method => :post do
div do
label_for :name
input :name => :name, :type => :text
end
div do
label_for :password
input :name => :password, :type => :password
end
div do
input.submit :type => :submit, :name => :login, :value => 'Login'
end
end
end
def signup
form :action => R(Signup), :method => :post do
div do
label_for :name
input :name => :name, :type => :text
end
div do
label_for :password
input :name => :password, :type => :password
end
div do
label_for :email
input :name => :email, :type => :text
end
div do
input.submit :type => :submit, :name => :login, :value => 'Signup'
end
end
end
def yours
h2 { "#{h @user.name.pluralize} responses"}
ul do
@user.responses.each { |r| li { a r.question.body, :href => R(Show, r.question) } }
li { "#{logged_in? and current_user.id == @user.id ? 'You haven\'t' : "#{h @user.name} hasn't"} created any responses yet." } if @user.responses.empty?
end
end
#static stuff follows
def about
h2 { "About Concise" }
p { "Concise is a list of questions and an attempt to answer those questions elegantly in as few words as possile." }
p { "The questions are (currently) posted by the moderator of this site, #{a 'Brenton Fletcher', :href => "http://blog.bloople.net"}. You can ask for a question to be posted on this site by #{a 'emailing Brenton', :href => "mailto:i@bloople.net"}. Brenton will attempt to post questions covering a range of topics, so that there's something for everyone." }
p { "The answers are supplied by you, the user. For each question, the answers are displayed in order of score, then by how short they are. You can vote up an answer if you think it's good (and you can vote the answer down if you think it's bad!)." }
p { "Answers are limited in length to 140 characters - just like Twitter. Hopefully this site can, like Twitter, help people express themselves eloquently in a short space."}
end
end
def Concise.create
Concise::Models.create_schema
ActiveRecord::Base.default_timezone = :utc
end