Conflict coefficients

This commit is contained in:
Petko Bordjukov 2015-10-15 02:01:03 +03:00
parent cfcde965fe
commit 0fbe3243c4
6 changed files with 38 additions and 0 deletions

View File

@ -11,4 +11,8 @@ class HomeController < ApplicationController
@talks = Talk.all @talks = Talk.all
@talk_preferences = TalkPreference.all @talk_preferences = TalkPreference.all
end end
def conflicts
@conflict_coefficients = ConflictCoefficient.all
end
end end

View File

@ -0,0 +1,25 @@
class ConflictCoefficient
attr_reader :left, :right, :conflicts
def self.all
talk_combinations = Talk.all.map(&:id).combination(2)
talk_preferences = TalkPreference.all
talk_preferences_count = TalkPreference.count
talk_combinations.map do |talks|
conflicts = talk_preferences.select do |talk_preference|
talk_preference.include_all? talks
end.count
ConflictCoefficient.new talks.first, talks.last, conflicts, talk_preferences_count
end
end
def initialize(left, right, conflicts, total_votes)
@left, @right, @conflicts, @total_votes = left, right, conflicts, total_votes
end
def per_cent
Rational(100 * @conflicts, @total_votes).to_f
end
end

View File

@ -9,6 +9,10 @@ class TalkPreference < ActiveRecord::Base
talks.include? id.to_s talks.include? id.to_s
end end
def include_all?(ids)
ids.all? { |id| include? id }
end
private private
def assign_new_unique_id def assign_new_unique_id

View File

@ -0,0 +1 @@
<%= CSV.generate_line([conflict_coefficient.left, conflict_coefficient.right, conflict_coefficient.conflicts, conflict_coefficient.per_cent]).html_safe -%>

View File

@ -0,0 +1,3 @@
<%- headers = ['left', 'right', 'conflicts', '%'] -%>
<%= CSV.generate_line headers -%>
<%= render partial: 'conflict_coefficient', collection: @conflict_coefficients %>

View File

@ -3,4 +3,5 @@ Rails.application.routes.draw do
root to: 'home#index' root to: 'home#index'
get 'ratings' => 'home#ratings' get 'ratings' => 'home#ratings'
get 'export' => 'home#export' get 'export' => 'home#export'
get 'conflicts' => 'home#conflicts'
end end