Initial import of anne-survey Camping app.

This commit is contained in:
Bram Senders 2010-05-18 15:07:50 +02:00
commit 8c694249ec
1 changed files with 202 additions and 0 deletions

202
anne-survey.rb Normal file
View File

@ -0,0 +1,202 @@
require "pathname"
Camping.goes :Anne
IMAGE_DIR = Pathname.new(__FILE__).dirname + "images"
module Anne::Models
class User < Base
end
class Vote < Base
belongs_to :user
end
class BasicFields < V 1.0
def self.up
create_table User.table_name do |t|
t.string :study, :gender, :spaciousness, :email
t.integer :study_year
t.boolean :colorblind, :prize, :results
t.timestamps
end
create_table Vote.table_name do |t|
t.string :image, :choice
t.integer :user_id
end
end
def self.down
drop_table User.table_name
drop_table Vote.table_name
end
end
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 }
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
end
end
module Anne::Controllers
class Index
def get
render :intro
end
end
class Start
def get
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
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
end
def post(user_id)
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
end
end
class ImageX
def get(file)
unless file =~ /\.\./
headers["Content-Type"] = 'image/jpeg'
headers['X-Sendfile'] = (IMAGE_DIR + file).to_s
else
@status = 403
"You're not allowed to retrieve #{file}!"
end
end
end
class FinishN
def get(user_id)
@user_id = user_id
return redirect(VoteN, @user_id) unless next_image.first.nil?
render :finish
end
end
end
module Anne::Views
def layout
xhtml_strict do
head do
title { "Anne's hippe enqueteshizzle" }
end
body do
self << yield
end
end
end
def intro
p "Lalala! [Introtekst hier]"
p "(Jij bent #{@env["REMOTE_ADDR"]})"
a "Start de enquête", :href => R(Start)
end
def make_select(label_name, name, options)
p do
label label_name, :for => name
select :name => name do
options.each { |option_str| option option_str }
end
end
end
def start
p "Zeer persoonlijke vragen"
form :action => R(Start), :method => :post do
make_select "Studie", "user_study", ["nvt", "Foo", "Bar"]
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"]
input :type => "submit", :value => "Start de enquete"
end
end
def vote(image, progress = 0)
h1 "Welk beeld is ruimtelijker?"
img :src => R(ImageX, image), :width => 640
form :action => R(VoteN, @user_id), :method => :post do
input :type => "hidden", :name => "image", :value => image
input :type => "submit", :name => "left", :value => "Links"
input :type => "submit", :name => "either", :value => "Geen van beide"
input :type => "submit", :name => "right", :value => "Rechts"
end
p "Woep, je bent al op #{progress}%!"
end
def finish
h1 "Je bent geweldig!"
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
p do
label "E-mailadres", :for => "email"
input :type => "text", :name => "email"
end
p do
input :type => "checkbox", :name => "prize", :checked => true
label "Ik wil een vette prijs winnen!", :for => "prize"
end
p do
input :type => "checkbox", :name => "results", :checked => true
label "Ik ben een nieuwsgierig aagje en ik wil het naadje van je kous weten!", :for => "results"
end
input :type => "submit", :value => "Okee, ik doe normaal!"
end
end
end
def Anne.create
Anne::Models.create_schema
end