]> projects.mako.cc - selectricity-live/blob - app/controllers/elections_controller.rb
339c77bc8991fe80ef38ab6afddd53e5ca53974d
[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     @voting_rolls = []
109     @election.voters.each do |voter|
110       if voter.vote and voter.vote.confirmed?
111         @voting_rolls << voter
112       end
113     end
114   end
115   
116   private
117     def randomize_order
118     end
119
120     def process_incoming_voters(raw_voter_list)
121       incoming_voters = RawVoterList.new( raw_voter_list )
122
123       unless incoming_voters.entries.empty?
124         incoming_voters.each do |new_voter|
125
126           if incoming_voters.email == 0
127             new_voter.contacted = 1
128           elsif incoming_voters.email == 1
129             email_voter( new_voter )
130             new_voter.contacted = 1
131           else
132             new_voter.contacted = 0
133           end
134         
135           # the new voter should be in good shape. save add to the election
136           new_voter.save
137           @election.voters << new_voter
138         end
139       end
140  
141       # reset the next time to have a the same default value for emailing
142       @raw_voter_list = RawVoterList.new
143       @raw_voter_list.email = incoming_voters.email
144     end
145
146     def email_voter
147     end
148
149 end

Benjamin Mako Hill || Want to submit a patch?