From 0fbe3243c41090d73c3ac4585dcddf5ce011fad7 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 15 Oct 2015 02:01:03 +0300 Subject: [PATCH] Conflict coefficients --- app/controllers/home_controller.rb | 4 ++++ app/models/conflict_coefficient.rb | 25 ++++++++++++++++++++ app/models/talk_preference.rb | 4 ++++ app/views/home/_conflict_coefficient.csv.erb | 1 + app/views/home/conflicts.csv.erb | 3 +++ config/routes.rb | 1 + 6 files changed, 38 insertions(+) create mode 100644 app/models/conflict_coefficient.rb create mode 100644 app/views/home/_conflict_coefficient.csv.erb create mode 100644 app/views/home/conflicts.csv.erb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 1485932..ac1cfab 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -11,4 +11,8 @@ class HomeController < ApplicationController @talks = Talk.all @talk_preferences = TalkPreference.all end + + def conflicts + @conflict_coefficients = ConflictCoefficient.all + end end diff --git a/app/models/conflict_coefficient.rb b/app/models/conflict_coefficient.rb new file mode 100644 index 0000000..cb6df0d --- /dev/null +++ b/app/models/conflict_coefficient.rb @@ -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 diff --git a/app/models/talk_preference.rb b/app/models/talk_preference.rb index 523424c..fe68f93 100644 --- a/app/models/talk_preference.rb +++ b/app/models/talk_preference.rb @@ -9,6 +9,10 @@ class TalkPreference < ActiveRecord::Base talks.include? id.to_s end + def include_all?(ids) + ids.all? { |id| include? id } + end + private def assign_new_unique_id diff --git a/app/views/home/_conflict_coefficient.csv.erb b/app/views/home/_conflict_coefficient.csv.erb new file mode 100644 index 0000000..ea34f37 --- /dev/null +++ b/app/views/home/_conflict_coefficient.csv.erb @@ -0,0 +1 @@ +<%= CSV.generate_line([conflict_coefficient.left, conflict_coefficient.right, conflict_coefficient.conflicts, conflict_coefficient.per_cent]).html_safe -%> diff --git a/app/views/home/conflicts.csv.erb b/app/views/home/conflicts.csv.erb new file mode 100644 index 0000000..8f3c4ad --- /dev/null +++ b/app/views/home/conflicts.csv.erb @@ -0,0 +1,3 @@ +<%- headers = ['left', 'right', 'conflicts', '%'] -%> +<%= CSV.generate_line headers -%> +<%= render partial: 'conflict_coefficient', collection: @conflict_coefficients %> diff --git a/config/routes.rb b/config/routes.rb index 1a04cd5..8fbcb1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,4 +3,5 @@ Rails.application.routes.draw do root to: 'home#index' get 'ratings' => 'home#ratings' get 'export' => 'home#export' + get 'conflicts' => 'home#conflicts' end