Add a Summary and Ranking
This commit is contained in:
parent
4fe67060c9
commit
31c7a43a18
|
@ -0,0 +1,11 @@
|
||||||
|
class SummariesController < ApplicationController
|
||||||
|
def show
|
||||||
|
@summary = Summary.new summary_params
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def summary_params
|
||||||
|
params.require(:summary).permit(talk_ids: [])
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,45 @@
|
||||||
|
RankingEntry = Struct.new :talk_id, :votes, :place
|
||||||
|
|
||||||
|
class Ranking
|
||||||
|
include ActiveModel::Model
|
||||||
|
|
||||||
|
attr_accessor :talk_ids
|
||||||
|
|
||||||
|
def ranking
|
||||||
|
@ranking ||= ranking_data.map do |talk_id, votes, place|
|
||||||
|
RankingEntry.new talk_id, votes, place
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](talk_id)
|
||||||
|
ranking[talk_id]
|
||||||
|
end
|
||||||
|
|
||||||
|
def talk_ids
|
||||||
|
@talk_ids ||= []
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def ranking_data
|
||||||
|
vote_data.sort_by { |_, votes| -votes }
|
||||||
|
.group_by { |_, votes| votes }
|
||||||
|
.each_with_index.map do |votes, placement|
|
||||||
|
votes.last.map {|talk_vote_data| talk_vote_data << placement + 1 }
|
||||||
|
end.flatten(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def vote_data
|
||||||
|
blank_vote_data.merge(database_vote_data)
|
||||||
|
end
|
||||||
|
|
||||||
|
def blank_vote_data
|
||||||
|
talk_ids.map do |talk_id|
|
||||||
|
[talk_id, 0]
|
||||||
|
end.to_h
|
||||||
|
end
|
||||||
|
|
||||||
|
def database_vote_data
|
||||||
|
SelectedTalk.group(:talk_id).where(talk_id: @talk_ids).count
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,15 @@
|
||||||
|
class Summary
|
||||||
|
include ActiveModel::Model
|
||||||
|
|
||||||
|
attr_accessor :talk_ids
|
||||||
|
|
||||||
|
def number_of_ballots
|
||||||
|
@number_of_ballots ||= TalkPreference.joins(:selected_talks)
|
||||||
|
.where(selected_talks: {talk_id: @talk_ids})
|
||||||
|
.uniq.count
|
||||||
|
end
|
||||||
|
|
||||||
|
def ranking
|
||||||
|
@ranking ||= Ranking.new(talk_ids: talk_ids).ranking
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1 @@
|
||||||
|
json.extract! @summary, :number_of_ballots, :ranking
|
|
@ -1,5 +1,6 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
resources :talk_preferences, only: [:index, :show, :create, :update]
|
resources :talk_preferences, only: [:index, :show, :create, :update]
|
||||||
root to: 'home#index'
|
root to: 'home#index'
|
||||||
|
resource :summary, only: :show
|
||||||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue