Configure rating scale as class instance variable of MajorityJudgment

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent 9c2adb1072
commit 9810cdb615
4 changed files with 25 additions and 13 deletions

10
mj.rb
View file

@ -1,8 +1,14 @@
class MajorityJudgment class MajorityJudgment
@values = nil
include Comparable include Comparable
attr_reader :ratings, :count, :n, :majority attr_reader :ratings, :count, :n, :majority
class << self
attr_accessor :values
end
def initialize(arg) def initialize(arg)
if arg.is_a?(Array) if arg.is_a?(Array)
@ratings = arg @ratings = arg
@ -22,7 +28,7 @@ class MajorityJudgment
def mj def mj
s = 0 s = 0
(1..7).to_a.reverse.each do |r| (1..self.class.values.length).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 +37,7 @@ class MajorityJudgment
def proponents(rating = self.mj) def proponents(rating = self.mj)
p = 0 p = 0
(rating+1..7).to_a.reverse.each do |r| (rating+1..self.class.values.length).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

View file

@ -16,7 +16,7 @@
<td><%= user.email %></td> <td><%= user.email %></td>
<% @vote.candidates.each do |candidate| %> <% @vote.candidates.each do |candidate| %>
<% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %> <% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %>
<td><%= rating.value %></td> <td><%= settings.values.select { |e| e[:id] == rating.value }.first[:label] %></td>
<% end %> <% end %>
<% end %> <% end %>
</tr> </tr>
@ -27,7 +27,7 @@
<ol> <ol>
<% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %> <% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
<li><%= candidate.name %>: <%= candidate.mj.mj %></li> <li><%= candidate.name %>: <%= settings.values.select { |e| e[:id] == candidate.mj.mj }.first[:label] %></li>
<% end %> <% end %>
</ol> </ol>

View file

@ -8,21 +8,19 @@
<form action="/votes/<%= @vote.secure_id %>/ratings" method="post"> <form action="/votes/<%= @vote.secure_id %>/ratings" method="post">
<ul> <ul>
<% @vote.candidates.each do |candidate| %> <% @vote.candidates.each do |candidate| %>
<% rating = @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate } %>
<% value = rating ? rating.value : 0 %>
<li> <li>
<p><%= candidate.name %></p> <p><%= candidate.name %></p>
<p><%= candidate.description %></p> <p><%= candidate.description %></p>
<ol> <ol>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-7" value="7" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 7 } %>checked<% end %>><label for="<%= candidate.id %>-7">Excellent</label> <% settings.values.reverse.each do |v| %>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-6" value="6" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 6 } %>checked<% end %>><label for="<%= candidate.id %>-6">Very good</label> <input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-<%= v[:id] %>" value="<%= v[:id] %>" <% if value == v[:id] %>checked<% end %>><label for="<%= candidate.id %>-<%= v[:id] %>"><%= v[:label] %></label>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-5" value="5" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 5 } %>checked<% end %>><label for="<%= candidate.id %>-5">Good</label> <% end %>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-4" value="4" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 4 } %>checked<% end %>><label for="<%= candidate.id %>-4">Mediocre</label>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-3" value="3" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 3 } %>checked<% end %>><label for="<%= candidate.id %>-3">Bad</label>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-2" value="2" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 2 } %>checked<% end %>><label for="<%= candidate.id %>-2">Very bad</label>
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-1" value="1" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 1 } %>checked<% end %>><label for="<%= candidate.id %>-1">Awful</label>
</ol> </ol>
</li> </li>
<% end %> <% end %>
</ul> </ul>
<button type="submit">Save ratings</button> <button type="submit">Save ratings</button>
</form> </form>

View file

@ -51,6 +51,14 @@ def verify_password(password, hash)
end end
enable :sessions enable :sessions
set :values, [ { :id => 1, :label => "Awful" },
{ :id => 2, :label => "Very bad" },
{ :id => 3, :label => "Bad" },
{ :id => 4, :label => "Mediocre" },
{ :id => 5, :label => "Good" },
{ :id => 6, :label => "Very good" },
{ :id => 7, :label => "Excellent" } ]
MajorityJudgment.values = settings.values
get '/' do get '/' do
redirect '/login' unless current_user redirect '/login' unless current_user