4a85c293b5ea4b424cf78c852e39ca45496feaec
[selectricity-live] / app / controllers / elections_controller.rb
1 class ElectionsController < ApplicationController
2   model :raw_voter_list, :voter, :vote, :candidate
3
4
5   # general methods for dealing with elections
6   def index
7     list
8     render :action => 'list'
9   end
10
11   def list
12     @election_pages, @elections = paginate :elections, :per_page => 10
13   end
14
15   # methods for displaying, creating, and manipulating election overview
16   # data
17   def show
18     @election = Election.find(params[:id])
19   end
20
21   def new
22     @election = Election.new
23   end
24   
25   def edit
26     @election = Election.find(params[:id])
27   end
28
29   def create_election
30     @election = Election.new(params[:election])
31     if @election.save
32       flash[:notice] = 'Election was successfully created.'
33       redirect_to :action => 'new_candidates', :id => @election.id
34     else
35       render :action => 'new'
36     end
37   end
38
39   def update
40     @election = Election.find(params[:id])
41     if @election.update_attributes(params[:election])
42       flash[:notice] = 'Election was successfully updated.'
43       redirect_to :action => 'show', :id => @election
44     else
45       render :action => 'edit'
46     end
47   end
48
49   def destroy
50     election = Election.find(params[:id]).destroy
51     redirect_to :action => 'list'
52   end
53
54   # methods fod display, adding, deleting, and manipulating candidate
55   # information for elections
56   def new_candidates
57     @election = Election.find( params[:id] )
58   end
59
60   def add_candidate
61     election = Election.find( params[:id] )
62     @candidate = Candidate.new
63     @candidate.name = params[:newcandidate] 
64     @candidate.save
65     election.candidates << @candidate
66     render :partial => 'candidate_line'
67   end
68   
69   def delete_candidate
70     candidate = Candidate.find( params[:id] )
71     candidate.destroy
72   end
73
74   def edit_candidates
75     @election = Election.find( params[:id] )
76   end
77
78   # methods for displaying, adding, deleting, and manipulating voters
79   # for a particular election
80   def new_voters
81     @election = Election.find( params[:id] )
82     if params.has_key?[:raw_voter_list]
83       process_incoming_voters( params[:raw_voter_list] )
84     end
85     @raw_voter_list = RawVoterList.new
86
87   end
88   
89   def edit_voters
90     @election = Election.find( params[:id] )
91     if params.has_key?( :raw_voter_list )
92       process_incoming_voters( params[:raw_voter_list] )
93     end
94
95     @raw_voter_list = RawVoterList.new
96   end
97   
98   def delete_voter
99     voter = Voter.find( params[:id] )
100     voter.destroy
101   end
102   
103   def summary_results 
104   end
105   
106   def detailed_results
107     @election = Election.find( params[:id] )
108
109     @voter_list = []
110     @vote_list = []
111     @election.voters.each do |voter|
112       if voter.vote and voter.vote.confirmed?
113         @voter_list << voter.email
114         @vote_list << voter.vote
115       end
116     end
117
118     @vote_list.sort!
119     @vote_list.sort! { |a,b| a.token <=> b.token }
120     #breakpoint
121
122   end
123   
124   private
125     def randomize_order
126     end
127
128     def process_incoming_voters(raw_voter_list)
129       incoming_voters = RawVoterList.new( raw_voter_list )
130
131       unless incoming_voters.entries.empty?
132         incoming_voters.each do |new_voter|
133
134           if incoming_voters.email == 0
135             new_voter.contacted = 1
136           elsif incoming_voters.email == 1
137             email_voter( new_voter )
138             new_voter.contacted = 1
139           else
140             new_voter.contacted = 0
141           end
142         
143           # the new voter should be in good shape. save add to the election
144           new_voter.save
145           @election.voters << new_voter
146         end
147       end
148  
149       # reset the next time to have a the same default value for emailing
150       @raw_voter_list = RawVoterList.new
151       @raw_voter_list.email = incoming_voters.email
152     end
153
154     def email_voter
155     end
156
157 end

Benjamin Mako Hill || Want to submit a patch?