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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -48,6 +48,14 @@
|
|||
<% end %>
|
||||
</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>
|
||||
|
||||
<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 '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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue