]> projects.mako.cc - selectricity-live/commitdiff
The defeats lsit partial contains the display logic for the mehtod added to condorcet...
author<jlsharps@mit.edu> <>
Thu, 16 Aug 2007 21:01:38 +0000 (17:01 -0400)
committer<jlsharps@mit.edu> <>
Thu, 16 Aug 2007 21:01:38 +0000 (17:01 -0400)
app/views/quickvote/_defeats_list.rhtml [new file with mode: 0644]
app/views/quickvote/_pref_table.rhtml
app/views/quickvote/results.rhtml
lib/rubyvote/condorcet.rb

diff --git a/app/views/quickvote/_defeats_list.rhtml b/app/views/quickvote/_defeats_list.rhtml
new file mode 100644 (file)
index 0000000..248c294
--- /dev/null
@@ -0,0 +1,15 @@
+<% victories, tied =@election.condorcet_result.list_defeats -%>
+<%candidates = @election.candidates.sort.collect {|candidate| candidate.id}%>
+<% names = Hash.new -%>
+<% candidates.each do |candidate| -%>
+       <%names[candidate] = Candidate.find(candidate).name -%>
+<% end -%>
+
+<% victories.each do |victory| -%>
+<%= names[victory[0]] %> beat <%= names[victory[1]] %> by <%= victory[2]%> 
+votes.<br />
+<% end -%>
+
+<% tied.each do |tie| -%>
+<%= names[tie[0]]%> tied with <%= names[tie[1]]%><br />
+<% end -%>
index dfcb6cca9ea31438ad480ce56b8814761743b1b8..32f5de4bc81254afef3a36fbae12a82ab331d21b 100644 (file)
@@ -1,13 +1,17 @@
-<% candidates = @election.candidates.sort.collect {|candidate| candidate.id} -%>
+<% candidates = @election.candidates.sort.collect {|candidate| candidate.id}-%>
+<% names = Hash.new -%>
+<% candidates.each do |candidate| -%>
+       <%names[candidate] = Candidate.find(candidate).name -%>
+<% end -%>
 <table class="voterbox">
   <tr>
        <td> </td>
        <% candidates.each do |candidate| -%>
 <table class="voterbox">
   <tr>
        <td> </td>
        <% candidates.each do |candidate| -%>
-         <th><%= candidate -%></th>
+         <th><%= names[candidate] -%></th>
        <% end -%>
 <% candidates.each do |winner| -%>
   <tr>
        <% end -%>
 <% candidates.each do |winner| -%>
   <tr>
-       <th><%= winner %></th>
+       <th><%= names[winner] %></th>
   <% candidates.each do |loser| -%> 
     <% if winner == loser -%>
       <td> -- </td>
   <% candidates.each do |loser| -%> 
     <% if winner == loser -%>
       <td> -- </td>
@@ -16,4 +20,5 @@
     <% end -%>
   <% end -%>
  </tr>
     <% end -%>
   <% end -%>
  </tr>
-<%end -%>
\ No newline at end of file
+<% end -%>
+
index 3b3a54d9ef68e11128b22891956d9bdb05cdf9fd..cba32d08f66761ffad948f64a949e0feb884505f 100644 (file)
@@ -1,3 +1,4 @@
+<%  %>
 <%require 'IPAddr' %>
 <h1>Results</h1>
 
 <%require 'IPAddr' %>
 <h1>Results</h1>
 
@@ -167,6 +168,7 @@ by several other names.</p>
 <% end %>
 </table>
 
 <% end %>
 </table>
 
+<%= render :partial => 'defeats_list' %>
 <%= render :partial => 'pref_table' %>
 
 <%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %><br />
 <%= render :partial => 'pref_table' %>
 
 <%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %><br />
index bf4e548da45bcbb6d9c3ebfa57269d065bf09b6a..715399566e270db367d311c9a26d2f7c08fd1bcf 100644 (file)
@@ -153,7 +153,29 @@ class CondorcetResult < ElectionResult
     super(voteobj)
     @matrix = voteobj.votes
   end
     super(voteobj)
     @matrix = voteobj.votes
   end
-
+  
+  def list_defeats
+    victors = Array.new
+    ties = Array.new
+    candidates = @matrix.keys.sort
+    
+    candidates.each do |candidate|
+      candidates.each do |challenger|
+        next if candidate == challenger
+        diff = @matrix[candidate][challenger] - @matrix[challenger][candidate]
+        if diff > 0 
+          victors << [candidate, challenger, diff]
+        elsif diff == 0 && ties.include?([challenger, candidate]) == false
+          ties << [candidate, challenger] 
+        end
+      end
+    end
+    
+    victories = victors.sort {|a,b| b[2] <=> a[2]}
+    
+    return victories, ties    
+  end
+        
   protected
   def defeats(candidates=nil, votes=nil)
     candidates = @election.candidates unless candidates
   protected
   def defeats(candidates=nil, votes=nil)
     candidates = @election.candidates unless candidates
@@ -182,7 +204,7 @@ class PureCondorcetResult < CondorcetResult
   end
 
   protected
   end
 
   protected
-
+  
   def condorcet
     votes = @election.votes
     candidates = @election.candidates
   def condorcet
     votes = @election.votes
     candidates = @election.candidates

Benjamin Mako Hill || Want to submit a patch?