class ApplicationController < ActionController::Base
include AuthenticatedSystem
helper :user
- model :user
+ require_dependency "user"
end
# look to see that the voter has been created and has voted in
# this election, and has confirmed their vote
- @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
+ @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
session.session_id, @election.id])[0]
# if the voter has not voted we destroy them
election = QuickVote.ident_to_quickvote(params[:ident])
# find out who the voter is for this election
- @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
+ @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
session.session_id, election.id])[0]
if not @voter
end
def change
- voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
+ voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
voter.destroy
redirect_to quickvote_url( :ident => params[:ident] )
end
def index
password = params[:id]
password = params[:vote][:password] if params[:vote]
- if @voter = FullVoter.find_all( [ "password = ?", password ] )[0]
+ if @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0]
render :action => 'fullvote'
end
end
private
def authenticate
password = params[:id]
- @voter = FullVoter.find_all( [ "password = ?", password ] )[0]
+ @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0]
end
end
end
def quickvote?
- type == 'QuickVote'
+ self.class == 'QuickVote'
end
def active?
def create_password
token_generator = UniqueTokenGenerator.new( 16 )
until password and not password.empty? \
- and Voter.find_all( [ "password = ?", password ]).empty?
+ and Voter.find(:all, :conditions => [ "password = ?", password ]).empty?
self.password = token_generator.token
end
end
def validate
if not @raw_candidates or @raw_candidates.length < 2
- errors.add(nil, "You must list at least two candidates.")
+ errors.add(nil, "You must list at least two candidates.")
end
+ @raw_candidates.each do |c|
+ unless c.instance_of? String
+ errors.add(nil, "Candidates must be strings")
+ next
+ end
+ c.strip!
+ if c.length == 0
+ errors.add(nil, "Candidate name must not be empty")
+ next
+ end
+ end if @raw_candidates
+
+ errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
+
if name =~ /[^A-Za-z0-9]/
errors.add(:name, "must only include numbers and letters.")
end
if ident.match(/^\d+$/)
quickvote = QuickVote.find(ident)
else
- quickvote = QuickVote.find_all(["name = ?", ident])[0]
+ quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]
end
return quickvote
if election
candidates=election.candidates.collect { |c| c.id }
vote_list[0].each do |vote|
- raise ArgumentError.new "Invalid Candidate ID #{vote}" unless candidates.index(vote)
+ raise ArgumentError.new("Invalid Candidate ID #{vote}") unless candidates.index(vote)
end
- raise ArgumentError.new "You must rank all candidates" unless candidates.length <= vote_list[0].length
- raise ArgumentError.new "Please rank each candidate only once" if vote_list[0].uniq!
+ raise ArgumentError.new("You must rank all candidates") unless candidates.length <= vote_list[0].length
+ raise ArgumentError.new("Please rank each candidate only once") if vote_list[0].uniq!
voter = QuickVoter.new
voter.election = election
voter.ipaddress = "XMLRPC Request"
voter.vote.confirm!
voter.save!
else
- raise ArgumentError.new "Cannot find election #{election_name}"
+ raise ArgumentError.new("Cannot find election #{election_name}")
end
end
def quickvote_candidate_ids_to_names(shortname, id_list)
end
def list_quickvotes()
all=Array.new
- QuickVote.find_all.each do |election|
+ QuickVote.find(:all).each do |election|
all << get_quickvote(election.name)
end
return all
end
def get_quickvote(shortname)
return ElectionStruct.new unless election=QuickVote.ident_to_quickvote(shortname)
- return ElectionStruct.new (:id => election.id, :name => election.name, :description => election.description, :candidate_ids => election.candidates.collect {|c| c.id }, :candidate_names => election.candidates.collect {|c| c.name } )
+ return ElectionStruct.new(:id => election.id, :name => election.name, :description => election.description, :candidate_ids => election.candidates.collect {|c| c.id }, :candidate_names => election.candidates.collect {|c| c.name } )
end
def create_quickvote(election)
qv=QuickVote.new(:name => election.name, :description => election.description)
super
token_generator = UniqueTokenGenerator.new( 16 )
- until not token.empty? and Token.find_all( [ "token = ?", token ]).empty?
+ until not token.empty? and Token.find(:all, :conditions => [ "token = ?", token ]).empty?
self.token = token_generator.token
end
election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
assert_create_quickvote_succeeds election
end
+ def test_quickvote_proper_results
+ election = ElectionStruct.new :name => "favdev", :description => "Who is your favorite developer?", :candidate_names => ["mako", "jdong", "justin"]
+ assert_create_quickvote_succeeds election
+ reflection = invoke_delegated :vote, :get_quickvote, "favdev"
+ candidates = {}
+ reflection.candidate_names.each_with_index do |name, index|
+ candidates[name] = reflection.candidate_ids[index]
+ end
+ 25.times do |t|
+ vote = [candidates["jdong"], candidates["mako"], candidates["justin"]]
+ invoke_delegated :vote, :cast_quickvote, "favdev", "1:#{t}", [vote]
+ end
+ 5.times do |t|
+ vote = [candidates["mako"], candidates["justin"], candidates["jdong"]]
+ invoke_delegated :vote, :cast_quickvote, "favdev", "2:#{t}", [vote]
+ end
+ 10.times do |t|
+ vote = [candidates["justin"], candidates["mako"], candidates["jdong"]]
+ invoke_delegated :vote, :cast_quickvote, "favdev", "3:#{t}", [vote]
+ end
+ results=invoke_delegated(:vote, :get_quickvote_results, "favdev")
+ assert_equal results.approval_winners, [candidates["mako"]]
+ assert_equal results.borda_winners, [candidates["jdong"]]
+ assert_equal results.plurality_winners, [candidates["jdong"]]
+ assert_equal results.condorcet_winners, [candidates["jdong"]]
+ assert_equal results.ssd_winners, [candidates["jdong"]]
+ assert_equal results.errors.length, 0
+ end
private
def assert_create_quickvote_succeeds(election)
# Checks if a created quickvote is identical when retrieved