From 3d349b24ddccc0ce66b72de9d9fd4b4326d8fe75 Mon Sep 17 00:00:00 2001 From: Date: Thu, 16 Aug 2007 17:01:38 -0400 Subject: [PATCH] The defeats lsit partial contains the display logic for the mehtod added to condorcet.rb taht helps print out a list of who defeated who and by how much. I also modified the tab;e in _pref_table.rhtml to use names as the table headers and notcandidate id's. Finally, results.rhtml has been updated to display them. --- app/views/quickvote/_defeats_list.rhtml | 15 ++++++++++++++ app/views/quickvote/_pref_table.rhtml | 13 +++++++++---- app/views/quickvote/results.rhtml | 2 ++ lib/rubyvote/condorcet.rb | 26 +++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 app/views/quickvote/_defeats_list.rhtml diff --git a/app/views/quickvote/_defeats_list.rhtml b/app/views/quickvote/_defeats_list.rhtml new file mode 100644 index 0000000..248c294 --- /dev/null +++ b/app/views/quickvote/_defeats_list.rhtml @@ -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.
+<% end -%> + +<% tied.each do |tie| -%> +<%= names[tie[0]]%> tied with <%= names[tie[1]]%>
+<% end -%> diff --git a/app/views/quickvote/_pref_table.rhtml b/app/views/quickvote/_pref_table.rhtml index dfcb6cc..32f5de4 100644 --- a/app/views/quickvote/_pref_table.rhtml +++ b/app/views/quickvote/_pref_table.rhtml @@ -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 -%> <% candidates.each do |candidate| -%> - + <% end -%> <% candidates.each do |winner| -%> - + <% candidates.each do |loser| -%> <% if winner == loser -%> @@ -16,4 +20,5 @@ <% end -%> <% end -%> -<%end -%> \ No newline at end of file +<% end -%> + diff --git a/app/views/quickvote/results.rhtml b/app/views/quickvote/results.rhtml index 3b3a54d..cba32d0 100644 --- a/app/views/quickvote/results.rhtml +++ b/app/views/quickvote/results.rhtml @@ -1,3 +1,4 @@ +<% %> <%require 'IPAddr' %>

Results

@@ -167,6 +168,7 @@ by several other names.

<% end %>
<%= candidate -%><%= names[candidate] -%>
<%= winner %><%= names[winner] %> --
+<%= render :partial => 'defeats_list' %> <%= render :partial => 'pref_table' %> <%= image_tag( graph_url( :action => 'votes_per_day', :id => @election ) ) %>
diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index bf4e548..7153995 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -153,7 +153,29 @@ class CondorcetResult < ElectionResult 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 @@ -182,7 +204,7 @@ class PureCondorcetResult < CondorcetResult end protected - + def condorcet votes = @election.votes candidates = @election.candidates -- 2.30.2