]> projects.mako.cc - selectricity/blob - app/controllers/elections_controller.rb
696028e1287baabe6f3431ea12c337bcee0e4238
[selectricity] / 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   private
104
105     def process_incoming_voters(raw_voter_list)
106       incoming_voters = RawVoterList.new( raw_voter_list )
107
108       unless incoming_voters.entries.empty?
109         incoming_voters.each do |new_voter|
110
111           if incoming_voters.email == 0
112             new_voter.contacted = 1
113           elsif incoming_voters.email == 1
114             email_voter( new_voter )
115             new_voter.contacted = 1
116           else
117             new_voter.contacted = 0
118           end
119         
120           # the new voter should be in good shape. save add to the election
121           new_voter.save
122           @election.voters << new_voter
123         end
124       end
125  
126       # reset the next time to have a the same default value for emailing
127       @raw_voter_list = RawVoterList.new
128       @raw_voter_list.email = incoming_voters.email
129     end
130 end

Benjamin Mako Hill || Want to submit a patch?