From 03dfaadb823c2b3553a476a72612d44dc368e84f Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Mon, 10 Oct 2016 00:20:20 +0300 Subject: [PATCH] Add a simple summary export --- app/controllers/home_controller.rb | 4 ++++ app/models/summary.rb | 23 +++++++++++++++++++++++ app/views/home/summary.json.jbuilder | 1 + config/routes.rb | 7 ++++++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/models/summary.rb create mode 100644 app/views/home/summary.json.jbuilder diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 40e2f29..cc6e68e 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -11,4 +11,8 @@ class HomeController < ApplicationController def export @talk_preferences = TalkPreference.this_years.eager_load(:selected_talks) end + + def summary + @summary = Summary.new params[:summary] + end end diff --git a/app/models/summary.rb b/app/models/summary.rb new file mode 100644 index 0000000..d55f7aa --- /dev/null +++ b/app/models/summary.rb @@ -0,0 +1,23 @@ +class Summary + def initialize(params = {}) + @talk_id, @other_talk_ids = params[:talk_id], params[:other_talk_ids] + @votes_count = SelectedTalk.where(talk_id: @talk_id).count + @all_votes_count = TalkPreference.joins(:selected_talks).where(selected_talks: {talk_id: @other_talk_ids << @talk_id}).count + end + + def summary + { + talk_id: @talk_id, + votes: @votes_count, + all_votes: @all_votes_count, + per_cent: Rational(@votes_count, @all_votes_count).to_f, + conflicts: conflicts + } + end + + def conflicts + @other_talk_ids.map do |right| + [right, ConflictCoefficient.new(@talk_id, right).conflicts] + end.to_h + end +end diff --git a/app/views/home/summary.json.jbuilder b/app/views/home/summary.json.jbuilder new file mode 100644 index 0000000..525c250 --- /dev/null +++ b/app/views/home/summary.json.jbuilder @@ -0,0 +1 @@ +json.merge! @summary.summary diff --git a/config/routes.rb b/config/routes.rb index c9ec5d5..2a0925f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,11 @@ Rails.application.routes.draw do root to: 'home#index' get 'ratings' => 'home#ratings' get 'export' => 'home#export' - resources :conflicts + get 'summary' => 'home#summary' + resources :conflicts do + collection do + get 'pivot' + end + end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end