From c8bfea489801c5689901b49c980ea6d51af69c1e Mon Sep 17 00:00:00 2001 From: ricola Date: Sun, 6 Apr 2025 17:04:31 -0600 Subject: [PATCH] Integrate MajorityJudgment class --- mj.rb | 4 ++-- views/votes_show.erb | 8 ++++++++ vote.rb | 8 ++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mj.rb b/mj.rb index 441db5c..7f44aec 100644 --- a/mj.rb +++ b/mj.rb @@ -22,7 +22,7 @@ class MajorityJudgment def mj s = 0 - (1..6).to_a.reverse.each do |r| + (1..7).to_a.reverse.each do |r| s = s + @count[r] if @count[r] return r if s >= @majority end @@ -31,7 +31,7 @@ class MajorityJudgment def proponents(rating = self.mj) p = 0 - (rating+1..6).to_a.reverse.each do |r| + (rating+1..7).to_a.reverse.each do |r| p = p + @count[r] if @count[r] end return p diff --git a/views/votes_show.erb b/views/votes_show.erb index 206740b..fdb8ba1 100644 --- a/views/votes_show.erb +++ b/views/votes_show.erb @@ -48,6 +48,14 @@ <% end %> +

Results

+ +
    + <% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %> +
  1. <%= candidate.name %>: <%= candidate.mj.mj %>
  2. + <% end %> +
+

New candidate

diff --git a/vote.rb b/vote.rb index bd35f3c..726fa5e 100644 --- a/vote.rb +++ b/vote.rb @@ -3,8 +3,7 @@ require 'sinatra' require 'sinatra/activerecord' require 'bcrypt' require 'securerandom' - -#set :database, 'sqlite3:db/vote.sqlite3' +require_relative 'mj' class Vote < ActiveRecord::Base has_many :candidates @@ -14,6 +13,11 @@ end class Candidate < ActiveRecord::Base belongs_to :vote has_many :ratings + + def mj + return MajorityJudgment.new(self.ratings.collect {|r| r.value }) + end + end class User < ActiveRecord::Base