such. There is still no functionality for running votes yet.
class ElectionsController < ApplicationController
+ model :raw_voter_list, :voter, :vote, :candidate
+
+
+ # general methods for dealing with elections
def index
list
render :action => 'list'
@election_pages, @elections = paginate :elections, :per_page => 10
end
+ # methods for displaying, creating, and manipulating election overview
+ # data
def show
@election = Election.find(params[:id])
end
def new
@election = Election.new
end
+
+ def edit
+ @election = Election.find(params[:id])
+ end
def create_election
@election = Election.new(params[:election])
end
end
+ def update
+ @election = Election.find(params[:id])
+ if @election.update_attributes(params[:election])
+ flash[:notice] = 'Election was successfully updated.'
+ redirect_to :action => 'show', :id => @election
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ election = Election.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+
+ # methods fod display, adding, deleting, and manipulating candidate
+ # information for elections
def new_candidates
@election = Election.find( params[:id] )
end
candidate.destroy
end
- def edit
- @election = Election.find(params[:id])
+ def edit_candidates
+ @election = Election.find( params[:id] )
end
- def edit_candidates
+ # methods for displaying, adding, deleting, and manipulating voters
+ # for a particular election
+ def new_voters
@election = Election.find( params[:id] )
+ if params.has_key?[:raw_voter_list]
+ process_incoming_voters( params[:raw_voter_list] )
+ end
+ @raw_voter_list = RawVoterList.new
+
end
def edit_voters
@election = Election.find( params[:id] )
+ if params.has_key?( :raw_voter_list )
+ process_incoming_voters( params[:raw_voter_list] )
+ end
+
+ @raw_voter_list = RawVoterList.new
end
- def update
- @election = Election.find(params[:id])
- if @election.update_attributes(params[:election])
- flash[:notice] = 'Election was successfully updated.'
- redirect_to :action => 'show', :id => @election
- else
- render :action => 'edit'
- end
+ def delete_voter
+ voter = Voter.find( params[:id] )
+ voter.destroy
end
+
+ private
- def destroy
- election = Election.find(params[:id]).destroy
- redirect_to :action => 'list'
- end
+ def process_incoming_voters(raw_voter_list)
+ incoming_voters = RawVoterList.new( raw_voter_list )
+
+ unless incoming_voters.entries.empty?
+ incoming_voters.each do |new_voter|
+
+ if incoming_voters.email == 0
+ new_voter.contacted = 1
+ elsif incoming_voters.email == 1
+ email_voter( new_voter )
+ new_voter.contacted = 1
+ else
+ new_voter.contacted = 0
+ end
+
+ # the new voter should be in good shape. save add to the election
+ new_voter.save
+ @election.voters << new_voter
+ end
+ end
+
+ # reset the next time to have a the same default value for emailing
+ @raw_voter_list = RawVoterList.new
+ @raw_voter_list.email = incoming_voters.email
+ end
end
class Candidate < ActiveRecord::Base
+ belongs_to :election
end
class Election < ActiveRecord::Base
has_many :candidates
+ has_many :voters
+ has_many :votes
validates_presence_of :name, :description
+ require 'date'
+
+ def startdate
+ read_attribute( :startdate ) || DateTime.now
+ end
+
+ def enddate
+ read_attribute( :enddate ) || DateTime.now + 14
+ end
+
def destroy
self.candidates.each do |candidate|
candidate.destroy
--- /dev/null
+class RawVoterList
+
+ attr_accessor :email
+ attr_accessor :input_addresses
+
+ include Enumerable
+
+ def initialize(params={})
+ @email = params[:email] || 1
+ @input_addresses = params[:input_addresses] || String.new
+ end
+
+ def email=(email)
+ @email = email
+ end
+
+ def each
+ @input_addresses.split("\n").each do |address|
+ yield Voter.new( { :email => address } )
+ end
+ end
+end
--- /dev/null
+class Voter < ActiveRecord::Base
+ belongs_to :election
+end
--- /dev/null
+class Votes < ActiveRecord::Base
+end
--- /dev/null
+<% %>
+<ul id="candidate_list">
+ <% @election.candidates.each do |candidate| %>
+ <% @candidate = candidate %>
+ <%= render :partial => 'candidate_line' %>
+ <% end %>
+</ul>
<p>Please enter candidates for <strong><%= @election.name %></strong>.</p>
- <ul id="candidates_list">
- <% @election.candidates.each do |candidate| %>
- <% @candidate = candidate %>
- <%= render :partial => 'candidate_line' %>
- <% end %>
- </ul>
-
- <%= form_remote_tag(:update => "candidates_list",
+ <%= form_remote_tag(:update => "candidate_list",
:url => { :action => :add_candidate, :id => @election.id },
:position => "top" ) %>
<p><label for="election_anonymous">Anonymous Vote</label>
<%= check_box 'election', 'anonymous', {}, 1, 0 %></p>
+
+<p>Election/Voting Start Date<br />
+<%= datetime_select :election, :startdate %></p>
+
+<p>Election/Voting End Date<br />
+<%= datetime_select :election, :enddate %></p>
+
<!--[eoform:election]-->
--- /dev/null
+<%
+# basic election information template
+-%>
+
+<% unless @election.voters.empty? %>
+ <p>The following voters are currently registered for this election:</p>
+
+ <ul>
+ <% @election.voters.each do |voter| %>
+ <div id="voter<%= voter.id %>">
+ <li><%= voter.email %>
+ <% if @edit %>
+ <%= link_to_remote "Delete",
+ :complete => "Element.remove('voter#{voter.id}')",
+ :url => { :action => :delete_voter, :id => voter.id } %>
+ <% end %>
+ </li>
+ </div>
+ <% end %>
+ </ul>
+<% end %>
+
+
--- /dev/null
+<p>Please enter a list of new email addresses of potential voters.</p>
+
+<%= text_area :raw_voter_list, :input_addresses %>
+
+<p>Email voters:
+<%= select :raw_voter_list, :email, [ [ 'Never', 0 ],
+ [ 'Now', 1 ],
+ [ 'Vote Start', 2 ] ] %>
+</p>
+<%= submit_tag "Add" %>
<% @edit = true %>
<h1><strong><%= @election.name %>:</strong> Edit Candidates</h1>
+<p>The following are valid options or candidates in this election:</p>
+
+<ul>
+<% @election.candidates.each do |candidate| %>
+ <% @candidate = candidate %>
+ <%= render :partial => 'candidate_line' %>
+<% end %>
+</ul>
+
<%= render :partial => 'candidates_form' %>
<%= button_to "Done!", :action => 'show', :id => @election %>
<% @edit = true %>
<h1><strong><%= @election.name %>:</strong> Edit Voter Rolls</h1>
-<%= button_to "Done!", :action => 'show', :id => @election %>
+<%= render :partial => 'voter_list' %>
+
+<%= form_tag :action => 'edit_voters', :id => @election.id %>
+<%= render :partial => 'voters_form' %>
+<%= end_form_tag %>
+
+<%= button_to 'Done!', :action => 'show', :id => @election.id %>
--- /dev/null
+<% @edit = true %>
+<h1><strong><%= @election.name %>:</strong> Enter List of Voter Email Addresses</h1>
+
+<%= render :partial => 'voter_list' %>
+
+<%= form_tag :action => 'new_voters', :id => @election.id %>
+<%= render :partial => 'voters_form' %>
+<%= end_form_tag %>
+<% %>
<h1>Information On <%= @election.name %></h1>
<h2>Overview <%= link_to "edit", :action => 'edit', :id => @election.id %></h2>
<h2>Candidates <%= link_to "edit", :action => 'edit_candidates', :id => @election.id %></h2>
-
-The following are valid options or candidates in this election:
-
-<ul>
-<% @election.candidates.each do |candidate| %>
- <% @candidate = candidate %>
- <%= render :partial => 'candidate_line' %>
-<% end %>
-</ul>
+<%= render :partial => 'candidate_list' %>
<h2>Voters <%= link_to "edit", :action => 'edit_voters', :id => @election.id %></h2>
+
+<%= render :partial => 'voter_list' %>
name varchar(100) NOT NULL,
description TEXT NOT NULL,
anonymous tinyint NOT NULL DEFAULT 0,
+ startdate datetime NOT NULL,
+ enddate datetime,
primary key (id)
);
drop table if exists voters;
create table voters (
id int NOT NULL auto_increment,
- username varchar(100) NOT NULL,
+ email varchar(100) NOT NULL,
password varchar(100) NOT NULL,
+ contacted tinyint NOT NULL DEFAULT 0,
+ election_id int NOT NULL,
+ constraint fk_election_voter foreign key (election_id) references election(id),
primary key (id)
);
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+ id: 1
+another:
+ id: 2
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+ id: 1
+another:
+ id: 2
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+ id: 1
+another:
+ id: 2
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+first:
+ id: 1
+another:
+ id: 2
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class CandidateTest < Test::Unit::TestCase
+ fixtures :candidates
+
+ # Replace this with your real tests.
+ def test_truth
+ assert_kind_of Candidate, candidates(:first)
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class RawVoterListTest < Test::Unit::TestCase
+ fixtures :raw_voter_lists
+
+ # Replace this with your real tests.
+ def test_truth
+ assert_kind_of RawVoterList, raw_voter_lists(:first)
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class VoterTest < Test::Unit::TestCase
+ fixtures :voters
+
+ # Replace this with your real tests.
+ def test_truth
+ assert_kind_of Voter, voters(:first)
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class VotesTest < Test::Unit::TestCase
+ fixtures :votes
+
+ # Replace this with your real tests.
+ def test_truth
+ assert_kind_of Votes, votes(:first)
+ end
+end