Added session management, and wrapped lines.

* Use Camping::Session to store the user variable, instead of passing it
   through the URL.
 * Redirect depending on whether or not a user variable is set.
 * Wrapped lines so that lines do not exceed 80 characters.
This commit is contained in:
Bram Senders 2010-05-18 22:47:14 +02:00
parent 3ae3dd4336
commit f10145a66b
1 changed files with 44 additions and 36 deletions

View File

@ -1,5 +1,6 @@
require "pathname"
require "markaby"
require "camping/session"
Markaby::Builder.set(:indent, 2)
@ -7,6 +8,13 @@ Camping.goes :Anne
IMAGE_DIR = Pathname.new(__FILE__).dirname + "images"
module Anne
include Camping::Session
secret "JeMoeder"
end
module Anne::Models
class User < Base
@ -41,11 +49,14 @@ end
module Anne::Helpers
def next_image
all_images = Pathname.glob(IMAGE_DIR + "*.jpg").map { |img| img.basename.to_s }
voted_images = Anne::Models::Vote.find(:all, :conditions => { :user_id => @user_id }).map { |vote| vote.image }
all_images = Pathname.glob(IMAGE_DIR + "*.jpg").map { |img|
img.basename.to_s }
voted_images = Anne::Models::Vote.find(:all, :conditions => {
:user_id => @state["user"].id }).map { |vote| vote.image }
remaining_images = all_images - voted_images
return nil, 100 if remaining_images.empty?
return remaining_images.sort_by { rand }.first, 100 - (remaining_images.length * 100.0 / all_images.length).to_i
return remaining_images.sort_by { rand }.first, 100 -
(remaining_images.length * 100.0 / all_images.length).to_i
end
end
@ -60,50 +71,46 @@ module Anne::Controllers
class Start
def get
return redirect Vote if @state["user"]
render :start
end
def post
user = User.create( :study => @input.user_study,
:study_year => @input.user_study_year,
:gender => @input.user_gender,
:colorblind => @input.user_colorblind == "ja",
:spaciousness => @input.user_spaciousness )
redirect VoteN, user.id
@state["user"] = User.create(
:study => @input.user_study,
:study_year => @input.user_study_year,
:gender => @input.user_gender,
:colorblind => @input.user_colorblind == "ja",
:spaciousness => @input.user_spaciousness)
redirect Vote
end
end
class VoteN
def get(user_id)
@user_id = user_id
begin
User.find(@user_id)
image, progress = next_image
return redirect FinishN, @user_id if image.nil?
render :vote, image, progress
rescue ActiveRecord::RecordNotFound
@status = 500
"Unknown user id #@user_id"
end
class Vote
def get
return redirect Index if not @state["user"]
image, progress = next_image
return redirect Finish if image.nil?
render :vote, image, progress
end
def post(user_id)
def post
choice = if @input.left then "left"
elsif @input.right then "right"
else "either"
end
vote = Vote.create( :user_id => user_id,
:image => @input.image,
:choice => choice )
redirect VoteN, user_id
vote = Anne::Models::Vote.create( :user_id => @state["user"].id,
:image => @input.image,
:choice => choice )
redirect Vote
end
end
class ImageX
def get(file)
unless file =~ /\.\./
headers["Content-Type"] = 'image/jpeg'
headers['X-Sendfile'] = (IMAGE_DIR + file).to_s
headers["Content-Type"] = "image/jpeg"
headers["X-Sendfile"] = (IMAGE_DIR + file).to_s
else
@status = 403
"You're not allowed to retrieve #{file}!"
@ -111,10 +118,10 @@ module Anne::Controllers
end
end
class FinishN
def get(user_id)
@user_id = user_id
return redirect(VoteN, @user_id) unless next_image.first.nil?
class Finish
def get
return redirect Index if not @state["user"]
return redirect Vote unless next_image.first.nil?
render :finish
end
end
@ -130,13 +137,13 @@ module Anne::Controllers
end
module Anne::Views
def layout
xhtml_strict do
head do
title "Anne's hippe enqueteshizzle"
p @state["user"].inspect
link :rel => "stylesheet", :type => "text/css",
:media => "screen", :href => "/style.css"
end
@ -174,7 +181,8 @@ module Anne::Views
make_select "Studiejaar", "user_study_year", ["nvt"] + (1998..2010).to_a
make_select "Geslacht", "user_gender", ["", "man", "vrouw"]
make_select "Kleurenblind?", "user_colorblind", ["nee", "ja"]
make_select "Ruimtelijk ingesteld?", "user_spaciousness", ["neutraal", "ja", "nee"]
make_select "Ruimtelijk ingesteld?", "user_spaciousness", ["neutraal",
"ja", "nee"]
input :type => "submit", :value => "Start de enquete"
end
@ -183,7 +191,7 @@ module Anne::Views
def vote(image, progress = 0)
h2 "Welk beeld is ruimtelijker?"
img :src => R(ImageX, image), :width => "80%"
form :action => R(VoteN, @user_id), :method => :post do
form :action => R(Vote), :method => :post do
input :type => "hidden", :name => "image", :value => image
input :type => "submit", :name => "left", :value => "Links"
input :type => "submit", :name => "right", :value => "Rechts"
@ -196,7 +204,7 @@ module Anne::Views
p "Nu ga ik een stukje op m'n motor rijden. Doei!"
p "Maar we hebben nog wat zeer persoonlijke vragen:"
form :action => R(FinishN, @user_id), :method => :post do
form :action => R(Finish), :method => :post do
p do
label "E-mailadres", :for => "email"
input :type => "text", :name => "email"