Variety of improvements and additions:
[selectricity] / app / controllers / elections_controller.rb
1 require 'uniq_token'
2
3 class ElectionsController < ApplicationController
4   model :raw_voter_list, :voter, :vote, :candidate
5
6
7   # general methods for dealing with elections
8   def index
9     list
10     render :action => 'list'
11   end
12
13   def list
14     @election_pages, @elections = paginate :elections, :per_page => 10
15   end
16
17   # methods for displaying, creating, and manipulating election overview
18   # data
19   def show
20     @election = Election.find(params[:id])
21   end
22
23   def new
24     @election = Election.new
25   end
26   
27   def edit
28     @election = Election.find(params[:id])
29   end
30
31   def create_election
32     @election = Election.new(params[:election])
33     if @election.save
34       flash[:notice] = 'Election was successfully created.'
35       redirect_to :action => 'new_candidates', :id => @election.id
36     else
37       render :action => 'new'
38     end
39   end
40
41   def update
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
46     else
47       render :action => 'edit'
48     end
49   end
50
51   def destroy
52     election = Election.find(params[:id]).destroy
53     redirect_to :action => 'list'
54   end
55
56   # methods fod display, adding, deleting, and manipulating candidate
57   # information for elections
58   def new_candidates
59     @election = Election.find( params[:id] )
60   end
61
62   def add_candidate
63     election = Election.find( params[:id] )
64     @candidate = Candidate.new
65     @candidate.name = params[:newcandidate] 
66     @candidate.save
67     election.candidates << @candidate
68     render :partial => 'candidate_line'
69   end
70   
71   def delete_candidate
72     candidate = Candidate.find( params[:id] )
73     candidate.destroy
74   end
75
76   def edit_candidates
77     @election = Election.find( params[:id] )
78   end
79
80   # methods for displaying, adding, deleting, and manipulating voters
81   # for a particular election
82   def new_voters
83     @election = Election.find( params[:id] )
84     if params.has_key?[:raw_voter_list]
85       process_incoming_voters( params[:raw_voter_list] )
86     end
87     @raw_voter_list = RawVoterList.new
88
89   end
90   
91   def edit_voters
92     @election = Election.find( params[:id] )
93     if params.has_key?( :raw_voter_list )
94       process_incoming_voters( params[:raw_voter_list] )
95     end
96
97     @raw_voter_list = RawVoterList.new
98   end
99   
100   def delete_voter
101     voter = Voter.find( params[:id] )
102     voter.destroy
103   end
104   
105   private
106
107     def process_incoming_voters(raw_voter_list)
108       incoming_voters = RawVoterList.new( raw_voter_list )
109       token_generator = UniqueTokenGenerator.new( 16 )
110
111       unless incoming_voters.entries.empty?
112         incoming_voters.each do |new_voter|
113
114           until new_voter.password and \
115                 Voter.find_all( [ "password = ?", new_voter.password ]).empty?
116             new_voter.password = token_generator.token
117           end
118          
119           breakpoint
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
125           else
126             new_voter.contacted = 0
127           end
128         
129           # the new voter should be in good shape. save add to the election
130           new_voter.save
131           @election.voters << new_voter
132         end
133       end
134  
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
138     end
139
140     def email_voter
141     end
142
143 end

Benjamin Mako Hill || Want to submit a patch?