Integrate MajorityJudgment class
This commit is contained in:
parent
4693e7bf73
commit
c8bfea4898
3 changed files with 16 additions and 4 deletions
4
mj.rb
4
mj.rb
|
|
@ -22,7 +22,7 @@ class MajorityJudgment
|
||||||
|
|
||||||
def mj
|
def mj
|
||||||
s = 0
|
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]
|
s = s + @count[r] if @count[r]
|
||||||
return r if s >= @majority
|
return r if s >= @majority
|
||||||
end
|
end
|
||||||
|
|
@ -31,7 +31,7 @@ class MajorityJudgment
|
||||||
|
|
||||||
def proponents(rating = self.mj)
|
def proponents(rating = self.mj)
|
||||||
p = 0
|
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]
|
p = p + @count[r] if @count[r]
|
||||||
end
|
end
|
||||||
return p
|
return p
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,14 @@
|
||||||
<% end %>
|
<% end %>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
<h2>Results</h2>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
|
||||||
|
<li><%= candidate.name %>: <%= candidate.mj.mj %></li>
|
||||||
|
<% end %>
|
||||||
|
</ol>
|
||||||
|
|
||||||
<h2>New candidate</h2>
|
<h2>New candidate</h2>
|
||||||
|
|
||||||
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||||
|
|
|
||||||
8
vote.rb
8
vote.rb
|
|
@ -3,8 +3,7 @@ require 'sinatra'
|
||||||
require 'sinatra/activerecord'
|
require 'sinatra/activerecord'
|
||||||
require 'bcrypt'
|
require 'bcrypt'
|
||||||
require 'securerandom'
|
require 'securerandom'
|
||||||
|
require_relative 'mj'
|
||||||
#set :database, 'sqlite3:db/vote.sqlite3'
|
|
||||||
|
|
||||||
class Vote < ActiveRecord::Base
|
class Vote < ActiveRecord::Base
|
||||||
has_many :candidates
|
has_many :candidates
|
||||||
|
|
@ -14,6 +13,11 @@ end
|
||||||
class Candidate < ActiveRecord::Base
|
class Candidate < ActiveRecord::Base
|
||||||
belongs_to :vote
|
belongs_to :vote
|
||||||
has_many :ratings
|
has_many :ratings
|
||||||
|
|
||||||
|
def mj
|
||||||
|
return MajorityJudgment.new(self.ratings.collect {|r| r.value })
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue