* A bunch of cleanup of the unit tests.
* Addition of fixtures for elections and candidates.
* Cleanup of the selectricitservice model.
* Additional tweaks for testing including addition of ruby-debug for
testing.
if params[:quickvote]
@quickvote = QuickVote.new(params[:quickvote])
# store the candidate grabbed through ajax and stored in flash
- @quickvote.candidatelist = flash[:candlist]
+ @quickvote.candidate_names = flash[:candidate_names]
@quickvote.description=@quickvote.description
# try to save, if it fails, show the page again (the flash should
# still be intact
@quickvote = @quickvote.reload
render :action => 'success'
else
- flash.keep(:candlist)
+ flash.keep(:candidate_names)
end
else
# if we don't have a quickvote param, it means that the person
# here has not been hitting this page and we can clear any
- # candlist in the flash
- flash.delete(:candlist) if flash.has_key?(:candlist)
+ # candidate_names list in the flash
+ flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
@quickvote = QuickVote.new
end
end
def add_candidate
candidate_name = params[:ajax][:newcandidate]
unless candidate_name.strip.empty?
- if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
- flash[:candlist] << candidate_name unless flash[:candlist].index(candidate_name)
+ if flash.has_key?(:candidate_names) \
+ and flash[:candidate_names].instance_of?(Array)
+ unless flash[:candidate_names].index(candidate_name)
+ flash[:candidate_names] << candidate_name
+ end
else
- flash[:candlist] = [ candidate_name ]
+ flash[:candidate_names] = [ candidate_name ]
end
end
- flash.keep(:candlist)
+ flash.keep(:candidate_names)
render_partial 'candidate_list'
end
if @election
# look to see that the voter has been created and has voted in
# this election, and has confirmed their vote
- @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
- session.session_id, @election.id])[0]
+ @voter = QuickVoter.find(:all,
+ :conditions => ["session_id = ? and election_id = ?",
+ session.session_id, @election.id])[0]
# if the voter has not voted we destroy them
if @voter and not @voter.voted?
election = QuickVote.ident_to_quickvote(params[:ident])
# find out who the voter is for this election
- @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
- session.session_id, election.id])[0]
+ @voter = QuickVoter.find(:all,
+ :conditions => ["session_id = ? and election_id = ?",
+ session.session_id, election.id])[0]
if not @voter
# we have not seen this voter before. something is wrong, try
end
def change
- voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
+ voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
+ session.session_id])[0]
voter.destroy
redirect_to quickvote_url( :ident => params[:ident] )
end
def mapvoters
@map = GMap.new("map_div_id")
@map.control_init(:large_map => true, :map_type => true)
- center=nil
+ center = nil
+
QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
next unless voter.ipaddress
+
location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
next unless location.lng and location.lat
+
unless center
- center=[location.lat,location.lng]
- @map.center_zoom_init(center,4)
+ center = [location.lat, location.lng]
+ @map.center_zoom_init(center, 4)
end
- marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+" "+voter.vote.votestring)
+
+ marker = GMarker.new([location.lat,location.lng],
+ :title => "Voter",
+ :info_window => (voter.ipaddress or "unknown") \
+ + " " + voter.vote.votestring)
@map.overlay_init(marker)
end
end
+
###############################################################
# the following method pertains to displaying the results of a
# quickvote
def start_blockers
reasons = []
-
+ debugger
if self.candidates.length <= 1
reasons << "You must have at least two candidates."
end
class QuickVote < Election
+ before_validation :build_candidate_names
after_validation :create_candidates
validates_uniqueness_of :name
validates_presence_of :name
- attr_accessor :raw_candidates
+
+ attr_accessor :candidate_names
attr_accessor :reviewed
def initialize(params={})
Time.now + 30.days - 1.second
end
- def enddate
- super(30)
- end
-
def validate
- if not @raw_candidates or @raw_candidates.length < 2
+ if @candidate_names.length < 2
errors.add(nil, "You must list at least two candidates.")
end
-
- @raw_candidates.each do |c|
+
+ @candidate_names.each do |c|
unless c.instance_of? String
errors.add(nil, "Candidates must be strings")
next
errors.add(nil, "Candidate name must not be empty")
next
end
- end if @raw_candidates
-
- errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
+ end if @candidate_names
+
+ if @candidate_names and @candidate_names.uniq!
+ errors.add(nil, "Candidates must all be unique")
+ end
if name =~ /[^A-Za-z0-9]/
errors.add(:name, "must only include numbers and letters.")
if name =~ /^(create|index|confirm|change|results)$/
errors.add(:name, " is a reserved word.")
end
-
+
if enddate < startdate
- errors.add(nil, "QuickVotes can't end before they start!")
+ errors.add(nil, "QuickVotes can't end before they start")
end
-
- end
-
- def candidatelist=(candlist)
- @raw_candidates = candlist
end
def name
reviewed.to_i == 1
end
+ def build_candidate_names
+ @candidate_names ||= []
+ if @candidate_names.empty? and not candidates.empty?
+ @candidate_names = candidates.collect {|c| c.name}
+ end
+ end
+
def create_candidates
return unless errors.empty?
- @raw_candidates.each do |name|
+
+ # delete the candidates
+ candidates.each {|c| c.destroy}
+
+ # create the new list based on the names
+ @candidate_names.each do |name|
candidate = Candidate.new({:name => name})
self.candidates << candidate
end
result.candidate_names=candidates.values
result
end
+
def get_quickvote_votes(shortname)
- qv=QuickVote.ident_to_quickvote(shortname)
- votes=Array.new
+ qv = QuickVote.ident_to_quickvote(shortname)
+
unless qv
raise ArgumentError.new("Cannot find QuickVote #{shortname}")
end
- qv.votes.each do |vote|
- votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i, :vote => vote.votes, :voter_session_id => vote.voter.session_id )
+
+ qv.votes.collect do |vote|
+ VoteInfo.new(:voter_id => vote.voter.id,
+ :voter_ipaddress => vote.voter.ipaddress,
+ :vote_time => vote.time.to_i,
+ :vote => vote.votes,
+ :voter_session_id => vote.voter.session_id)
end
- return votes
end
+
def list_quickvotes()
- all=Array.new
- QuickVote.find(:all).each do |election|
- all << get_quickvote(election.name)
+ QuickVote.find(:all).collect do |election|
+ get_quickvote(election.name)
end
- return all
end
+
def get_quickvote(shortname)
- raise ArgumentError.new("Cannot find QuickVote named #{shortname}") unless election=QuickVote.ident_to_quickvote(shortname)
- return ElectionStruct.new(:id => election.id, :name => election.name, :description => election.description, :candidate_ids => election.candidates.collect {|c| c.id }, :candidate_names => election.candidates.collect {|c| c.name } )
+ unless election = QuickVote.ident_to_quickvote(shortname)
+ raise ArgumentError.new("Cannot find QuickVote named #{shortname}")
+ end
+
+ ElectionStruct.new(
+ :id => election.id,
+ :name => election.name,
+ :description => election.description,
+ :candidate_ids => election.candidates.collect {|c| c.id },
+ :candidate_names => election.candidates.collect {|c| c.name } )
end
+
def create_quickvote(election)
- qv=QuickVote.new(:name => election.name, :description => election.description)
- qv.candidatelist=election.candidate_names
+ qv = QuickVote.new(:name => election.name,
+ :description => election.description)
+ qv.candidate_names = election.candidate_names
+
if qv.save
return ""
else
<% %>
<!-- the list of candidates -->
-<% if flash[:candlist] %>
+<% if flash[:candidate_names] %>
<ul>
- <% for cand in flash[:candlist] %>
+ <% for cand in flash[:candidate_names] %>
<li><%=h cand.capitalize %></li>
<% end %>
</ul>
<% end %>
-<% form_remote_tag(:update => 'candlist',
+<% form_remote_tag(:update => 'candidate_names',
:url => { :action => 'add_candidate' },
:complete => "Field.focus('ajax_newcandidate')") do %>
<p>
<%= error_messages_for 'quickvote' %>
-<p><label for="quickvote_candidatelist">Choices</p>
+<p><label for="quickvote_candidate_names">Choices</p>
<!-- the list of candidates -->
-<div id="candlist">
+<div id="candidate_names">
<%= render :partial => 'candidate_list' %>
</div>
# Tell ActionMailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
-config.action_mailer.delivery_method = :test
\ No newline at end of file
+config.action_mailer.delivery_method = :test
+
+# start the debugger
+require 'ruby-debug'
+SCRIPT_LINES__ = {}
+Debugger.start
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-first:
- id: 1
-another:
- id: 2
+
+mika_normal:
+ id: 1
+ name: mika
+ election_id: 1
+
+mako_normal:
+ id: 2
+ name: mako
+ election_id: 1
+
+bettamax_normal:
+ id: 3
+ name: bettamax
+ election_id: 1
+
+mika_badend:
+ id: 4
+ name: mika
+ election_id: 2
+
+mako_badend:
+ id: 5
+ name: mako
+ election_id: 2
+
+bettamax_badend:
+ id: 6
+ name: bettamax
+ election_id: 2
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-first:
- id: 1
-another:
- id: 2
+quickvote_normal:
+ id: 1
+ name: acetarium
+ description: who is the winner?
+ anonymous: 1
+ startdate: 2007-08-22 12:00:00
+ enddate: 2007-09-21 11:59:59
+ active: 1
+ type: QuickVote
+ election_method: ssd
+
+quickvote_invalid_endtime:
+ id: 2
+ name: acetarium2
+ description: who is the winner?
+ anonymous: 1
+ startdate: 2007-08-22 12:00:00
+ enddate: 2003-09-21 11:59:59
+ active: 1
+ type: QuickVote
+ election_method: ssd
end
def test_create_quickvote
- post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]})
+ post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candidate_names=>["foo", "bar", "foobar"]})
assert_template "quickvote/success"
get :index, { 'ident' => "variable"}
assert_response :success
end
def test_create_quickvote_badname
- post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]})
+ post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "foobar"]})
assert_template "quickvote/create"
end
def test_create_quickvote_dupe_candidate
- post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar", "foobar"]})
+ post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "bar", "foobar"]})
assert_template "quickvote/create"
end
def test_create_quickvote_nil_candidate
- post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil})
+ post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => nil})
assert_template "quickvote/create"
- post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []})
+ post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => []})
assert_template "quickvote/create"
end
test_create_quickvote
qv=QuickVote.ident_to_quickvote('variable')
qv.description="<object>foo</object>"
- qv.candidatelist = ["<object>foo", "bar<object>", "<foobar>"]
+ qv.candidate_names = ["<object>foo", "bar<object>", "<foobar>"]
qv.save!
get :index, { 'ident' => 'variable' }
assert_response :success
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)
+ def test_default
+ assert true
end
end
require File.dirname(__FILE__) + '/../test_helper'
class ElectionTest < Test::Unit::TestCase
- fixtures :elections
-
- # Replace this with your real tests.
- def test_truth
- assert_kind_of Election, elections(:first)
+ def test_default
+ assert true
end
end
require File.dirname(__FILE__) + '/../test_helper'
class QuickVoteTest < Test::Unit::TestCase
- fixtures :quickvote
-
- def correct_enddate
- true
+ fixtures :elections, :candidates
+
+ def setup
+ @quickvote_normal = QuickVote.find(1)
+ end
+
+ def test_create_update_delete_quickvote_from_fixture
+ assert_kind_of QuickVote, @quickvote_normal
+ assert_equal 1, @quickvote_normal.id
+ assert_equal 'acetarium', @quickvote_normal.name
+ assert_equal 'who is the winner?', @quickvote_normal.description
+ #assert_equal QuickVote, @quickvote_normal.type
+ assert_equal 'ssd', @quickvote_normal.election_method
+ assert_equal '2007-08-22 12:00:00', @quickvote_normal.startdate_before_type_cast
+ assert_equal '2007-09-21 11:59:59', @quickvote_normal.enddate_before_type_cast
+
+ # make sure that the each of the three andidates
+ (1..3).each do |i|
+ assert @quickvote_normal.candidates.include?(Candidate.find(i))
+ end
+
+ # update and save
+ @quickvote_normal.name = 'foobar'
+ assert @quickvote_normal.save,
+ @quickvote_normal.errors.full_messages.join("; ")
+
+ @quickvote_normal.reload
+ assert_equal 'foobar', @quickvote_normal.name
+ end
+
+ def test_create_invalid_enddate
+ qv = QuickVote.find(2)
+ assert_equal qv.save, false, "created vote with invalid enddate"
end
-
-
-
-end
\ No newline at end of file
+end
end
def test_list_quickvotes
- result= invoke_delegated :vote, :list_quickvotes
+ result = invoke_delegated :vote, :list_quickvotes
assert_instance_of Array, result
- assert_equal result.length, 0
+ assert result.length > 0
end
def test_create_quickvote
- election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
+ election = ElectionStruct.new(
+ { :name => "TestVote",
+ :description => "Test Vote",
+ :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"] })
assert_create_quickvote_succeeds election
end
def test_cast_nil_quickvote
assert_cast_quickvote_fails nil, nil, nil
assert_cast_quickvote_fails "foo", nil, nil
- assert_cast_quickvote_fails "foo",33, []
+ assert_cast_quickvote_fails "foo", 33, []
test_create_quickvote
- assert_cast_quickvote_fails "TestVote",42,nil
- assert_cast_quickvote_fails "TestVote",nil,[]
+ assert_cast_quickvote_fails "TestVote", 42,nil
+ assert_cast_quickvote_fails "TestVote", nil, []
end
def test_cast_malformed_votelist