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