From: Date: Thu, 13 Jul 2006 20:23:20 +0000 (-0400) Subject: Added a series of files so that elections can be created and edited and X-Git-Url: https://projects.mako.cc/source/selectricity/commitdiff_plain/5a8b533b5abec8dc24674e4ef084b0b9779da8af?ds=sidebyside Added a series of files so that elections can be created and edited and such. There is still no functionality for running votes yet. --- diff --git a/app/controllers/elections_controller.rb b/app/controllers/elections_controller.rb index 2b1f282..696028e 100644 --- a/app/controllers/elections_controller.rb +++ b/app/controllers/elections_controller.rb @@ -1,4 +1,8 @@ class ElectionsController < ApplicationController + model :raw_voter_list, :voter, :vote, :candidate + + + # general methods for dealing with elections def index list render :action => 'list' @@ -8,6 +12,8 @@ class ElectionsController < ApplicationController @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 @@ -15,6 +21,10 @@ class ElectionsController < ApplicationController def new @election = Election.new end + + def edit + @election = Election.find(params[:id]) + end def create_election @election = Election.new(params[:election]) @@ -26,6 +36,23 @@ class ElectionsController < ApplicationController 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 @@ -44,30 +71,60 @@ class ElectionsController < ApplicationController 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 diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 41d743b..44a4d63 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -1,2 +1,3 @@ class Candidate < ActiveRecord::Base + belongs_to :election end diff --git a/app/models/election.rb b/app/models/election.rb index be828d0..29bf1b0 100644 --- a/app/models/election.rb +++ b/app/models/election.rb @@ -1,7 +1,19 @@ 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 diff --git a/app/models/raw_voter_list.rb b/app/models/raw_voter_list.rb new file mode 100644 index 0000000..2f7cb28 --- /dev/null +++ b/app/models/raw_voter_list.rb @@ -0,0 +1,22 @@ +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 diff --git a/app/models/voter.rb b/app/models/voter.rb new file mode 100644 index 0000000..a63ec67 --- /dev/null +++ b/app/models/voter.rb @@ -0,0 +1,3 @@ +class Voter < ActiveRecord::Base + belongs_to :election +end diff --git a/app/models/votes.rb b/app/models/votes.rb new file mode 100644 index 0000000..782f514 --- /dev/null +++ b/app/models/votes.rb @@ -0,0 +1,2 @@ +class Votes < ActiveRecord::Base +end diff --git a/app/views/elections/_candidate_list.rhtml b/app/views/elections/_candidate_list.rhtml new file mode 100644 index 0000000..ca8a5cc --- /dev/null +++ b/app/views/elections/_candidate_list.rhtml @@ -0,0 +1,7 @@ +<% %> + diff --git a/app/views/elections/_candidates_form.rhtml b/app/views/elections/_candidates_form.rhtml index d739dbf..814b38f 100644 --- a/app/views/elections/_candidates_form.rhtml +++ b/app/views/elections/_candidates_form.rhtml @@ -1,13 +1,6 @@

Please enter candidates for <%= @election.name %>.

- - - <%= form_remote_tag(:update => "candidates_list", + <%= form_remote_tag(:update => "candidate_list", :url => { :action => :add_candidate, :id => @election.id }, :position => "top" ) %> diff --git a/app/views/elections/_overview_form.rhtml b/app/views/elections/_overview_form.rhtml index b3715e1..4a76b81 100644 --- a/app/views/elections/_overview_form.rhtml +++ b/app/views/elections/_overview_form.rhtml @@ -9,5 +9,12 @@

<%= check_box 'election', 'anonymous', {}, 1, 0 %>

+ +

Election/Voting Start Date
+<%= datetime_select :election, :startdate %>

+ +

Election/Voting End Date
+<%= datetime_select :election, :enddate %>

+ diff --git a/app/views/elections/_voter_list.rhtml b/app/views/elections/_voter_list.rhtml new file mode 100644 index 0000000..c1035d7 --- /dev/null +++ b/app/views/elections/_voter_list.rhtml @@ -0,0 +1,23 @@ +<% +# basic election information template +-%> + +<% unless @election.voters.empty? %> +

The following voters are currently registered for this election:

+ + +<% end %> + + diff --git a/app/views/elections/_voters_form.rhtml b/app/views/elections/_voters_form.rhtml new file mode 100644 index 0000000..620b508 --- /dev/null +++ b/app/views/elections/_voters_form.rhtml @@ -0,0 +1,10 @@ +

Please enter a list of new email addresses of potential voters.

+ +<%= text_area :raw_voter_list, :input_addresses %> + +

Email voters: +<%= select :raw_voter_list, :email, [ [ 'Never', 0 ], + [ 'Now', 1 ], + [ 'Vote Start', 2 ] ] %> +

+<%= submit_tag "Add" %> diff --git a/app/views/elections/edit_candidates.rhtml b/app/views/elections/edit_candidates.rhtml index 2809d10..0c416d5 100644 --- a/app/views/elections/edit_candidates.rhtml +++ b/app/views/elections/edit_candidates.rhtml @@ -1,5 +1,14 @@ <% @edit = true %>

<%= @election.name %>: Edit Candidates

+

The following are valid options or candidates in this election:

+ + + <%= render :partial => 'candidates_form' %> <%= button_to "Done!", :action => 'show', :id => @election %> diff --git a/app/views/elections/edit_voters.rhtml b/app/views/elections/edit_voters.rhtml index 64ec446..36de3eb 100644 --- a/app/views/elections/edit_voters.rhtml +++ b/app/views/elections/edit_voters.rhtml @@ -1,4 +1,10 @@ <% @edit = true %>

<%= @election.name %>: Edit Voter Rolls

-<%= 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 %> diff --git a/app/views/elections/new_voters.rhtml b/app/views/elections/new_voters.rhtml new file mode 100644 index 0000000..b750085 --- /dev/null +++ b/app/views/elections/new_voters.rhtml @@ -0,0 +1,8 @@ +<% @edit = true %> +

<%= @election.name %>: Enter List of Voter Email Addresses

+ +<%= render :partial => 'voter_list' %> + +<%= form_tag :action => 'new_voters', :id => @election.id %> +<%= render :partial => 'voters_form' %> +<%= end_form_tag %> diff --git a/app/views/elections/show.rhtml b/app/views/elections/show.rhtml index 0ef2fc5..954e480 100644 --- a/app/views/elections/show.rhtml +++ b/app/views/elections/show.rhtml @@ -1,3 +1,4 @@ +<% %>

Information On <%= @election.name %>

Overview <%= link_to "edit", :action => 'edit', :id => @election.id %>

@@ -10,14 +11,8 @@

Candidates <%= link_to "edit", :action => 'edit_candidates', :id => @election.id %>

- -The following are valid options or candidates in this election: - - +<%= render :partial => 'candidate_list' %>

Voters <%= link_to "edit", :action => 'edit_voters', :id => @election.id %>

+ +<%= render :partial => 'voter_list' %> diff --git a/db/create.sql b/db/create.sql index e6013cf..b7b50bf 100644 --- a/db/create.sql +++ b/db/create.sql @@ -22,6 +22,8 @@ create table elections ( name varchar(100) NOT NULL, description TEXT NOT NULL, anonymous tinyint NOT NULL DEFAULT 0, + startdate datetime NOT NULL, + enddate datetime, primary key (id) ); @@ -43,8 +45,11 @@ create table candidates ( 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) ); diff --git a/test/fixtures/candidates.yml b/test/fixtures/candidates.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/candidates.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/fixtures/raw_voter_lists.yml b/test/fixtures/raw_voter_lists.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/raw_voter_lists.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/fixtures/voters.yml b/test/fixtures/voters.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/voters.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml new file mode 100644 index 0000000..8794d28 --- /dev/null +++ b/test/fixtures/votes.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +first: + id: 1 +another: + id: 2 diff --git a/test/unit/candidate_test.rb b/test/unit/candidate_test.rb new file mode 100644 index 0000000..b640f0c --- /dev/null +++ b/test/unit/candidate_test.rb @@ -0,0 +1,10 @@ +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 diff --git a/test/unit/raw_voter_list_test.rb b/test/unit/raw_voter_list_test.rb new file mode 100644 index 0000000..3ed0984 --- /dev/null +++ b/test/unit/raw_voter_list_test.rb @@ -0,0 +1,10 @@ +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 diff --git a/test/unit/voter_test.rb b/test/unit/voter_test.rb new file mode 100644 index 0000000..0b41048 --- /dev/null +++ b/test/unit/voter_test.rb @@ -0,0 +1,10 @@ +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 diff --git a/test/unit/votes_test.rb b/test/unit/votes_test.rb new file mode 100644 index 0000000..1baaa1b --- /dev/null +++ b/test/unit/votes_test.rb @@ -0,0 +1,10 @@ +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