Implement conflicts table generation

This commit is contained in:
Petko Bordjukov 2016-10-12 20:07:57 +03:00
parent 31c7a43a18
commit a39af296ba
5 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,11 @@
class ConflictsController < ApplicationController
def show
@conflicts_table = ConflictsTable.new conflicts_table_params
end
private
def conflicts_table_params
params.require(:conflicts_table).permit(:talk_id, other_talks_ids: [])
end
end

12
app/models/conflicts.rb Normal file
View File

@ -0,0 +1,12 @@
class Conflicts
include ActiveModel::Model
attr_accessor :left, :right
def conflicts
TalkPreference.joins('INNER JOIN "selected_talks" AS left
ON "left"."talk_preference_id" = "talk_preferences"."unique_id"
INNER JOIN "selected_talks" AS right
ON "right"."talk_preference_id" = "talk_preferences"."unique_id"').where(left: {talk_id: @left}, right: {talk_id: @right}).count
end
end

View File

@ -0,0 +1,18 @@
class ConflictsTable
include ActiveModel::Model
attr_accessor :talk_id, :other_talks_ids
def other_talks_ids
@other_talks_ids ||= []
end
def conflicts
other_talks_ids.map do |right|
{
talk_id: right,
number_of_conflicts: Conflicts.new(left: talk_id, right: right).conflicts
}
end
end
end

View File

@ -0,0 +1 @@
json.extract! @conflicts_table, :talk_id, :conflicts

View File

@ -2,5 +2,6 @@ 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 resource :summary, only: :show
resource :conflicts, 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