From fd484b33a3920e0031007c5c2f1dec61913cbab5 Mon Sep 17 00:00:00 2001
From:
Date: Sun, 10 Sep 2006 15:39:06 -0400
Subject: [PATCH] A few major changes:
* Contiue with upgrade to Rails 1.1
* Large variety of fixes to various bits and pieces.
* Rudimentary (and broken) support for email in two different areas.
---
app/controllers/election_controller.rb | 20 +++++++++++-----
app/controllers/site_controller.rb | 9 +++----
app/controllers/user_controller.rb | 5 ++++
app/models/candidate.rb | 1 +
app/models/election.rb | 20 +++++++++++++++-
app/models/user.rb | 1 +
app/models/voter.rb | 9 +++----
app/models/voter_notify.rb | 14 +++++++++++
app/views/election/_candidate_form.rhtml | 3 +++
app/views/election/_candidates_form.rhtml | 9 -------
app/views/election/_voter_list.rhtml | 10 ++++----
app/views/election/_voters_form.rhtml | 7 ++++--
app/views/election/edit_candidate.rhtml | 10 ++++----
app/views/election/edit_candidates.rhtml | 13 ++++++++--
app/views/election/show.rhtml | 29 +++++++++++++++++++++++
app/views/voter_notify/votestart.rhtml | 29 +++++++++++++++++++++++
config/environment.rb | 7 ++++++
public/stylesheets/vb.css | 6 +++++
test/unit/voter_notify_test.rb | 27 +++++++++++++++++++++
19 files changed, 192 insertions(+), 37 deletions(-)
create mode 100644 app/models/voter_notify.rb
delete mode 100644 app/views/election/_candidates_form.rhtml
create mode 100644 app/views/voter_notify/votestart.rhtml
create mode 100644 test/unit/voter_notify_test.rb
diff --git a/app/controllers/election_controller.rb b/app/controllers/election_controller.rb
index a0549d4..46da56e 100644
--- a/app/controllers/election_controller.rb
+++ b/app/controllers/election_controller.rb
@@ -48,9 +48,17 @@ class ElectionController < ApplicationController
end
end
- def destroy
- election = Election.find(params[:id]).destroy
- redirect_to :action => 'list'
+ def start_election
+ @election = Election.find(params[:id])
+
+ @election.voters.each do |voter|
+ email = VoterNotify.create_votestart(voter)
+ render(:text => "" + email.encoded + "
")
+ breakpoint
+ break
+ end
+
+ #@election.activate!
end
# methods fod display, adding, deleting, and manipulating candidate
@@ -97,12 +105,12 @@ class ElectionController < ApplicationController
def update_candidate
@candidate = Candidate.find(params[:id])
+ @election = @candidate.election
if @candidate.update_attributes(params[:candidate])
- flash[:notice] = 'Candidate information was successfully updated.'
- redirect_to :action => 'edit_candidates', :id => @candidate.election
+ redirect_to :action => 'edit_candidates', :id => @candidate.election.id
else
- render :action => 'edit_candidates'
+ render :action => 'edit_candidate'
end
end
diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb
index 8d2f8b2..f2eb153 100644
--- a/app/controllers/site_controller.rb
+++ b/app/controllers/site_controller.rb
@@ -3,10 +3,11 @@ class SiteController < ApplicationController
model :user, :election
def index
- @current_elections = session[:user].elections.sort do |a,b|
- b.enddate <=> a.enddate
+ if session[:user]
+ session[:user] = User.find(session[:user].id)
+ @current_elections = session[:user].elections.sort do |a,b|
+ b.enddate <=> a.enddate
+ end
end
-
-
end
end
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb
index f88f7d5..25f827c 100644
--- a/app/controllers/user_controller.rb
+++ b/app/controllers/user_controller.rb
@@ -1,3 +1,8 @@
class UserController < ApplicationController
layout 'vb'
+
+ def home
+ redirect_to :controller => 'site', :action => 'index'
+ end
+
end
diff --git a/app/models/candidate.rb b/app/models/candidate.rb
index 7af978a..7b6e802 100644
--- a/app/models/candidate.rb
+++ b/app/models/candidate.rb
@@ -1,6 +1,7 @@
class Candidate < ActiveRecord::Base
belongs_to :election
validates_uniqueness_of :name
+ validates_presence_of :name
def <=>(other)
self.name <=> other.name
diff --git a/app/models/election.rb b/app/models/election.rb
index 69038e4..ba466b4 100644
--- a/app/models/election.rb
+++ b/app/models/election.rb
@@ -21,5 +21,23 @@ class Election < ActiveRecord::Base
end
super
end
-
+
+ def start_blockers
+ reasons = []
+
+ if self.candidates.length <= 1
+ reasons << "You must have at least two candidates."
+ end
+
+ if self.voters.length <= 1
+ reasons << "You must have at least two voters."
+ end
+
+ reasons
+ end
+
+ def activate!
+ self.active = 1
+ end
+
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 62c5d1d..4f25e06 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,5 +1,6 @@
class User < ActiveRecord::Base
include LoginEngine::AuthenticatedUser
+
has_many :elections
def name
diff --git a/app/models/voter.rb b/app/models/voter.rb
index e9b3e9e..c4700cb 100644
--- a/app/models/voter.rb
+++ b/app/models/voter.rb
@@ -2,11 +2,12 @@ class Voter < ActiveRecord::Base
belongs_to :election
has_one :vote
- def initialize(args)
- super(args)
-
+ before_create :create_password
+
+ def create_password
token_generator = UniqueTokenGenerator.new( 16 )
- until password and Voter.find_all( [ "password = ?", password ]).empty?
+ until password and not password.empty? \
+ and Voter.find_all( [ "password = ?", password ]).empty?
self.password = token_generator.token
end
end
diff --git a/app/models/voter_notify.rb b/app/models/voter_notify.rb
new file mode 100644
index 0000000..09f7899
--- /dev/null
+++ b/app/models/voter_notify.rb
@@ -0,0 +1,14 @@
+class VoterNotify < ActionMailer::Base
+
+ def votestart(voter)
+ @subject = "[%s] Election In Progress!" % voter.election.name
+ @recipients = voter.email
+ @from = MAIL_CONFIG[:from]
+ @sent_on = Time.now
+ @body = { :voter => voter }
+ end
+
+ def reminder(voter)
+ end
+
+end
diff --git a/app/views/election/_candidate_form.rhtml b/app/views/election/_candidate_form.rhtml
index e9770b8..6e0fb1f 100644
--- a/app/views/election/_candidate_form.rhtml
+++ b/app/views/election/_candidate_form.rhtml
@@ -1,3 +1,5 @@
+<% %>
+
New candidate name:
<%= text_field :candidate, :name %>
@@ -6,3 +8,4 @@
Candidate picture (optional and < 100x100 pixels):
<%= file_field :candidate, :picture %>
+
diff --git a/app/views/election/_candidates_form.rhtml b/app/views/election/_candidates_form.rhtml
deleted file mode 100644
index 4528923..0000000
--- a/app/views/election/_candidates_form.rhtml
+++ /dev/null
@@ -1,9 +0,0 @@
-<% %>
-
-<%= form_tag( { :action => :add_candidate, :id => @election.id },
- :multipart => true ) %>
-
-<%= render_partial 'candidate_form' %>
-
-<%= submit_tag "Add Candidate" %>
-<%= end_form_tag %>
diff --git a/app/views/election/_voter_list.rhtml b/app/views/election/_voter_list.rhtml
index 2a39b8c..c55afdb 100644
--- a/app/views/election/_voter_list.rhtml
+++ b/app/views/election/_voter_list.rhtml
@@ -1,6 +1,8 @@
-<%
-# basic election information template
--%>
+<% %>
+
+<% if @election.voters.length == 0 %>
+There are currently no voters registered for this election.
+<% else %>
The following voters are currently registered for this election:
@@ -16,4 +18,4 @@
<% end %>
-
+<% end %>
diff --git a/app/views/election/_voters_form.rhtml b/app/views/election/_voters_form.rhtml
index 620b508..3b01c00 100644
--- a/app/views/election/_voters_form.rhtml
+++ b/app/views/election/_voters_form.rhtml
@@ -1,10 +1,13 @@
-Please enter a list of new email addresses of potential voters.
+Please enter a list of new email addresses of voters (one email
+address per line).
<%= text_area :raw_voter_list, :input_addresses %>
+
-<%= submit_tag "Add" %>
+<%= submit_tag "Add Voters" %>
diff --git a/app/views/election/edit_candidate.rhtml b/app/views/election/edit_candidate.rhtml
index 9bc9954..e465ae9 100644
--- a/app/views/election/edit_candidate.rhtml
+++ b/app/views/election/edit_candidate.rhtml
@@ -1,9 +1,9 @@
Editing <%= @candidate.name %>
-<%= error_messages_on :candidate %>
-
-<%= form_tag :action => 'update_candidate', :id => @candidate.id %>
- <%= render :partial => 'candidate_form' %>
- <%= submit_tag "Done!" %>
+<%= error_messages_for :candidate %>
+<%= form_tag( { :action => :update_candidate, :id => @candidate.id },
+ :multipart => true ) %>
+<%= render :partial => 'candidate_form' %>
+<%= submit_tag "Save" %>
<%= end_form_tag %>
diff --git a/app/views/election/edit_candidates.rhtml b/app/views/election/edit_candidates.rhtml
index b5f06ff..dbcc3f8 100644
--- a/app/views/election/edit_candidates.rhtml
+++ b/app/views/election/edit_candidates.rhtml
@@ -1,4 +1,6 @@
-<%= @election.name %>: Edit Candidates
+<%= @election.name %>: Edit/Add Candidates
+
+<%= error_messages_for :candidate %>
<% unless @election.candidates.empty? %>
The following are valid options or candidates in this election:
@@ -11,6 +13,13 @@
<% else %>
There are no candidates registered for this election.
<% end %>
+
Please enter new candidates below.
-<%= render :partial => 'candidates_form' %>
+
+<%= form_tag( { :action => :add_candidate, :id => @election.id },
+ :multipart => true ) %>
+<%= render :partial => 'candidate_form' %>
+<%= submit_tag "Add Candidate" %>
+<%= end_form_tag %>
+
<%= button_to "Done!", :action => 'show', :id => @election %>
diff --git a/app/views/election/show.rhtml b/app/views/election/show.rhtml
index f389059..90c93ea 100644
--- a/app/views/election/show.rhtml
+++ b/app/views/election/show.rhtml
@@ -32,7 +32,36 @@
<% unless @election.voters.empty? %>
<%= render :partial => 'voter_list' %>
+ <%= link_to "Add or remove voters.", :action => 'edit_voters', :id => @election.id %>
<% else %>
There are currently no voters registered for this election.
<%= link_to "Add some!", :action => 'edit_voters', :id => @election.id %>
<% end %>
+
+Start Election
+
+<% if @election.start_blockers.length > 0 %>
+ Your election cannot be started for the following reasons:
+
+ <% for reason in @election.start_blockers %>
+ - <%= reason %>
+ <% end %>
+
+<% else %>
+Please check eveything carefully on this page before starting this
+election. Once you begin the election, you will not be able to
+add or change candidates, modify the voting lists, or change the
+election end time.
+
+When you begin the election, the following will happen:
+
+
+ - The election will be "frozen" so that further edits to the
+ candidate list and voting list cannot occur.
+ - All voters will be emailed notifying them that the election has
+ begun and of their unique login token.
+
+
+<%= button_to 'Start Election!', :action => 'start_election', :id => @election.id %>
+
+<% end %>
diff --git a/app/views/voter_notify/votestart.rhtml b/app/views/voter_notify/votestart.rhtml
new file mode 100644
index 0000000..c2730bf
--- /dev/null
+++ b/app/views/voter_notify/votestart.rhtml
@@ -0,0 +1,29 @@
+Voter!
+
+This is an automated message sent by votingbooth.mako.cc.
+
+You have been listed as a voter in an upcoming election.
+
+The election title: <%= @voter.election.name %>
+
+To read more about the election, the candidates, and to vote, you will
+need to use the following token to log in to votingboth.mako.cc:
+ <%= @voter.password %>
+
+Alternatively, you can just click this URL:
+
+If you have any questions or if you feel you have recieved this message
+in error, you should contact:
+
+ <%= @voter.election.user.name %> <<%= @voter.election.user.email %>>
+ (The initiator of this election)
+
+Alternatively, if you feel there is a technical error, please contact:
+
+ help@votingbooth.mako.cc
+ (VotingBooth Tech Support)
+
+Thanks and happy voting!
+VotingBooth Staff
+
+
diff --git a/config/environment.rb b/config/environment.rb
index 22cfe50..dcc9fad 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -50,6 +50,8 @@ end
# inflect.uncountable %w( fish sheep )
# end
+MAIL_CONFIG = { :from => 'VotingBooth '}
+
# Include your application configuration below
require 'uniq_token'
require 'randarray'
@@ -57,6 +59,11 @@ require 'rubyvote'
module LoginEngine
config :salt, "voothingboat"
+ config :email_from, MAIL_CONFIG[:from]
end
Engines.start :login
+
+# action mailer configuration
+ActionMailer::Base.delivery_method = :sendmail
+ActionMailer::Base.default_charset = "utf-8"
diff --git a/public/stylesheets/vb.css b/public/stylesheets/vb.css
index 176fd28..9f0d7c8 100644
--- a/public/stylesheets/vb.css
+++ b/public/stylesheets/vb.css
@@ -101,3 +101,9 @@ a:active { color: #FFFFFF; text-decoration: none; background: #0259C4; }
font-size: 12px;
font-weight: bold; }
+.fieldWithErrors {
+ display: inline;
+}
+.fieldWithErrors input, .fieldWithErrors select {
+ background-color: #ffdfdf;
+}
diff --git a/test/unit/voter_notify_test.rb b/test/unit/voter_notify_test.rb
new file mode 100644
index 0000000..6e5f802
--- /dev/null
+++ b/test/unit/voter_notify_test.rb
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'voter_notify'
+
+class VoterNotifyTest < Test::Unit::TestCase
+ FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
+ CHARSET = "utf-8"
+
+ include ActionMailer::Quoting
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+
+ @expected = TMail::Mail.new
+ @expected.set_content_type "text", "plain", { "charset" => CHARSET }
+ end
+
+ private
+ def read_fixture(action)
+ IO.readlines("#{FIXTURES_PATH}/voter_notify/#{action}")
+ end
+
+ def encode(subject)
+ quoted_printable(subject, CHARSET)
+ end
+end
--
2.39.5