#18 Add sql index for unpublished exercises

And remove spec for remove feature
This commit is contained in:
Maximilian Pass
2020-12-15 12:17:11 +01:00
committed by Felix Auringer
parent 2d2869765f
commit 028fc2989a
13 changed files with 148 additions and 1 deletions

View File

@ -271,3 +271,7 @@ input#subscribe {
.do-not-answer {
background-color: #ea2f1085;
}
#q_submission_study_group_id_in_chosen {
margin-right: 20px;
}

View File

@ -1,6 +1,7 @@
class RequestForCommentsController < ApplicationController
include SubmissionScoring
before_action :set_request_for_comment, only: [:show, :edit, :update, :destroy, :mark_as_solved, :set_thank_you_note]
before_action :set_study_group_grouping, only: %i[index get_my_comment_requests get_rfcs_with_my_comments]
before_action :require_user!
@ -18,8 +19,12 @@ class RequestForCommentsController < ApplicationController
.ransack(params[:q])
@request_for_comments = @search.result
.where("question NOT LIKE '%#loesung%'")
.joins(:exercise)
.where(exercises: {unpublished: false})
.includes(submission: [:study_group])
.order('created_at DESC')
.paginate(page: params[:page], total_entries: @search.result.length)
authorize!
end
@ -125,4 +130,12 @@ class RequestForCommentsController < ApplicationController
params.require(:request_for_comment).permit(:exercise_id, :file_id, :question, :requested_at, :solved, :submission_id).merge(user_id: current_user.id, user_type: current_user.class.name)
end
# The index page requires the grouping of the study groups
# The study groups are grouped by the current study group and other study groups of the user
def set_study_group_grouping
current_study_group = StudyGroup.find_by(id: session[:study_group_id])
my_study_groups = current_user.study_groups.reject { |group| group == current_study_group }
@study_groups_grouping = [[t('request_for_comments.index.study_groups.current'), Array(current_study_group)],
[t('request_for_comments.index.study_groups.my'), my_study_groups]]
end
end

View File

@ -8,6 +8,10 @@ h1 = RequestForComment.model_name.human(count: 2)
.form-group
= f.label(:title_cont, t('request_for_comments.solved'), class: 'sr-only')
= f.select(:solved_not_eq, [[t('request_for_comments.show_all'), 2], [t('request_for_comments.show_unsolved'), 1], [t('request_for_comments.show_solved'), 0]])
.form-group
= f.label(:submission_study_group_id_eq, t('request_for_comments.index.study_groups.placeholder'), class: 'sr-only')
= f.grouped_collection_select(:submission_study_group_id_in, @study_groups_grouping, :second, :first, :id, :name, {},
{ class: 'form-control', multiple: true, "data-placeholder": t('request_for_comments.index.study_groups.placeholder') })
.table-responsive
table.table.sortable.mt-4

View File

@ -722,6 +722,10 @@ de:
all: "Alle Kommentaranfragen"
get_rfcs_with_my_comments: Kommentaranfragen die ich kommentiert habe
get_my_rfc_activity: "Meine Kommentaraktivität"
study_groups:
placeholder: "Lerngruppe"
current: "Aktuelle Lerngruppe"
my: "Meine Lerngruppen"
no_question: "Der Autor hat keine Frage zu dieser Anfrage gestellt."
mark_as_solved: "Diese Frage als beantwortet markieren"
show_all: "Alle Anfragen anzeigen"

View File

@ -724,6 +724,10 @@ en:
get_my_comment_requests: My Requests for Comments
get_rfcs_with_my_comments: Requests for Comments I have commented on
get_my_rfc_activity: "My Comment Activity"
study_groups:
placeholder: "Study group"
current: "Current Study Group"
my: "My Study Groups"
no_question: "The author did not enter a question for this request."
mark_as_solved: "Mark this question as answered"
show_all: "All requests"

View File

@ -0,0 +1,6 @@
class AddIndexToRfc < ActiveRecord::Migration[5.2]
def change
add_index(:request_for_comments, %i[user_id user_type created_at],
order: { user_id: :asc, user_type: :asc, created_at: :desc }, name: :index_rfc_on_user_and_created_at)
end
end

View File

@ -0,0 +1,5 @@
class AddIndexToUnpublishedExercises < ActiveRecord::Migration[5.2]
def change
add_index(:exercises, :id, where: 'NOT unpublished', name: :index_unpublished_exercises)
end
end

View File

@ -0,0 +1,6 @@
class AddIndexToExerciseTitle < ActiveRecord::Migration[5.2]
def change
enable_extension 'pg_trgm'
add_index :exercises, :title, using: :gin, opclass: :gin_trgm_ops
end
end

View File

@ -10,9 +10,10 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_10_26_184633) do
ActiveRecord::Schema.define(version: 2020_12_10_113500) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
create_table "anomaly_notifications", id: :serial, force: :cascade do |t|
@ -168,6 +169,8 @@ ActiveRecord::Schema.define(version: 2020_10_26_184633) do
t.datetime "submission_deadline"
t.datetime "late_submission_deadline"
t.index ["id"], name: "index_exercises_on_id"
t.index ["id"], name: "index_unpublished_exercises", where: "(NOT unpublished)"
t.index ["title"], name: "index_exercises_on_title", opclass: :gin_trgm_ops, using: :gin
end
create_table "exercises_proxy_exercises", id: false, force: :cascade do |t|
@ -331,6 +334,7 @@ ActiveRecord::Schema.define(version: 2020_10_26_184633) do
t.integer "times_featured", default: 0
t.index ["exercise_id"], name: "index_request_for_comments_on_exercise_id"
t.index ["submission_id"], name: "index_request_for_comments_on_submission_id"
t.index ["user_id", "user_type", "created_at"], name: "index_rfc_on_user_and_created_at", order: { created_at: :desc }
end
create_table "searches", id: :serial, force: :cascade do |t|

View File

@ -0,0 +1,46 @@
require 'rails_helper'
describe RequestForCommentsController do
let(:user) { FactoryBot.create(:admin) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'GET #index' do
it 'renders the index template' do
get :index
expect(response).to have_http_status :ok
expect(response).to render_template :index
end
it 'shows only rfc`s belonging to selected study group' do
my_study_group = FactoryBot.create(:study_group)
rfc_within_my_study_group = FactoryBot.create(:rfc, user: user)
user.update(study_groups: [my_study_group])
rfc_within_my_study_group.submission.update(study_group: my_study_group)
another_study_group = FactoryBot.create(:study_group)
rfc_other_study_group = FactoryBot.create(:rfc)
rfc_other_study_group.user.update(study_groups: [another_study_group])
rfc_other_study_group.submission.update(study_group: another_study_group)
get :index, params: {"q[submission_study_group_id_in][]": my_study_group.id}
expect(assigns(:request_for_comments)).to eq([rfc_within_my_study_group])
end
end
describe 'GET #get_my_comment_requests' do
before { get :get_my_comment_requests }
expect_status(200)
expect_template(:index)
end
describe 'GET #get_rfcs_with_my_comments' do
before { get :get_rfcs_with_my_comments }
expect_status(200)
expect_template(:index)
end
end

View File

@ -0,0 +1,11 @@
FactoryBot.define do
factory :rfc, class: RequestForComment do
association :user, factory: :external_user
association :submission
association :exercise, factory: :dummy
association :file
sequence :question do |n|
"test question #{n}"
end
end
end

View File

@ -0,0 +1,8 @@
FactoryBot.define do
factory :study_group, class: StudyGroup do
association :consumer
sequence :name do |n|
"TestGroup#{n}"
end
end
end

View File

@ -0,0 +1,32 @@
require 'rails_helper'
describe 'Request_for_Comments' do
let(:exercise) { FactoryBot.create(:audio_video, description: Forgery(:lorem_ipsum).sentence) }
let(:user) { FactoryBot.create(:teacher) }
before do
visit(sign_in_path)
fill_in('email', with: user.email)
fill_in('password', with: FactoryBot.attributes_for(:teacher)[:password])
click_button(I18n.t('sessions.new.link'))
end
it 'does not contain rfcs for unpublished exercises' do
unpublished_rfc = FactoryBot.create(:rfc)
unpublished_rfc.exercise.update(title: 'Unpublished Exercise')
unpublished_rfc.exercise.update(unpublished: true)
rfc = FactoryBot.create(:rfc)
rfc.exercise.update(title: 'Normal Exercise')
rfc.exercise.update(unpublished: false)
visit(request_for_comments_path)
expect(page).to have_content(rfc.exercise.title)
expect(page).not_to have_content(unpublished_rfc.exercise.title)
end
it 'contains a filter for study group in the view' do
visit(request_for_comments_path)
expect(page.find('#q_submission_study_group_id_in')).not_to be_nil
end
end