3 class ElectionsController < ApplicationController
4 model :raw_voter_list, :voter, :vote, :candidate
7 # general methods for dealing with elections
10 render :action => 'list'
14 @election_pages, @elections = paginate :elections, :per_page => 10
17 # methods for displaying, creating, and manipulating election overview
20 @election = Election.find(params[:id])
24 @election = Election.new
28 @election = Election.find(params[:id])
32 @election = Election.new(params[:election])
34 flash[:notice] = 'Election was successfully created.'
35 redirect_to :action => 'new_candidates', :id => @election.id
37 render :action => 'new'
42 @election = Election.find(params[:id])
43 if @election.update_attributes(params[:election])
44 flash[:notice] = 'Election was successfully updated.'
45 redirect_to :action => 'show', :id => @election
47 render :action => 'edit'
52 election = Election.find(params[:id]).destroy
53 redirect_to :action => 'list'
56 # methods fod display, adding, deleting, and manipulating candidate
57 # information for elections
59 @election = Election.find( params[:id] )
63 election = Election.find( params[:id] )
64 @candidate = Candidate.new
65 @candidate.name = params[:newcandidate]
67 election.candidates << @candidate
68 render :partial => 'candidate_line'
72 candidate = Candidate.find( params[:id] )
77 @election = Election.find( params[:id] )
80 # methods for displaying, adding, deleting, and manipulating voters
81 # for a particular election
83 @election = Election.find( params[:id] )
84 if params.has_key?[:raw_voter_list]
85 process_incoming_voters( params[:raw_voter_list] )
87 @raw_voter_list = RawVoterList.new
92 @election = Election.find( params[:id] )
93 if params.has_key?( :raw_voter_list )
94 process_incoming_voters( params[:raw_voter_list] )
97 @raw_voter_list = RawVoterList.new
101 voter = Voter.find( params[:id] )
107 def process_incoming_voters(raw_voter_list)
108 incoming_voters = RawVoterList.new( raw_voter_list )
109 token_generator = UniqueTokenGenerator.new( 16 )
111 unless incoming_voters.entries.empty?
112 incoming_voters.each do |new_voter|
114 until new_voter.password and \
115 Voter.find_all( [ "password = ?", new_voter.password ]).empty?
116 new_voter.password = token_generator.token
120 if incoming_voters.email == 0
121 new_voter.contacted = 1
122 elsif incoming_voters.email == 1
123 email_voter( new_voter )
124 new_voter.contacted = 1
126 new_voter.contacted = 0
129 # the new voter should be in good shape. save add to the election
131 @election.voters << new_voter
135 # reset the next time to have a the same default value for emailing
136 @raw_voter_list = RawVoterList.new
137 @raw_voter_list.email = incoming_voters.email