From 166aa3cc6f1b08ea77d2b72451bfc5488b13595d Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sat, 20 Aug 2022 01:28:47 +0200 Subject: [PATCH] Remove Ransack from Exercise edit for tags * Also improve Ransack usage of attributes and associations --- app/controllers/exercises_controller.rb | 3 +-- app/models/application_record.rb | 8 ++++++++ app/models/execution_environment.rb | 4 ++++ app/models/exercise.rb | 8 ++++++++ app/models/exercise_collection.rb | 4 ++++ app/models/proxy_exercise.rb | 4 ++++ app/models/request_for_comment.rb | 8 ++++++++ app/models/submission.rb | 4 ++++ app/models/user.rb | 4 ++++ app/views/exercises/_form.html.slim | 2 +- 10 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 47fb7bed..d8c01ef8 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -461,8 +461,7 @@ class ExercisesController < ApplicationController private :set_file_types def collect_set_and_unset_exercise_tags - @search = policy_scope(Tag).ransack(params[:q]) - @tags = @search.result.order(:name) + @tags = policy_scope(Tag) checked_exercise_tags = @exercise.exercise_tags checked_tags = checked_exercise_tags.collect(&:tag).to_set unchecked_tags = Tag.all.to_set.subtract checked_tags diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 2e96397b..2852279e 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -15,4 +15,12 @@ class ApplicationRecord < ActiveRecord::Base end end end + + def self.ransackable_associations(_auth_object = nil) + [] + end + + def self.ransackable_attributes(_auth_object = nil) + [] + end end diff --git a/app/models/execution_environment.rb b/app/models/execution_environment.rb index a3bee8d6..53ff0427 100644 --- a/app/models/execution_environment.rb +++ b/app/models/execution_environment.rb @@ -61,6 +61,10 @@ class ExecutionEnvironment < ApplicationRecord exposed_ports.join(', ') end + def self.ransackable_attributes(_auth_object = nil) + %w[id] + end + private def set_default_values diff --git a/app/models/exercise.rb b/app/models/exercise.rb index bd27750c..7711dbe7 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -597,4 +597,12 @@ cause: %w[submit assess remoteSubmit remoteAssess]}).distinct WHERE exercise_id = #{id} ) AS t ON t.fv = submissions.id").distinct end + + def self.ransackable_attributes(_auth_object = nil) + %w[title] + end + + def self.ransackable_associations(_auth_object = nil) + %w[execution_environment] + end end diff --git a/app/models/exercise_collection.rb b/app/models/exercise_collection.rb index 8b356e4f..895abdc3 100644 --- a/app/models/exercise_collection.rb +++ b/app/models/exercise_collection.rb @@ -31,4 +31,8 @@ working_time: time_to_f(item.exercise.average_working_time)} def to_s "#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})" end + + def self.ransackable_attributes(_auth_object = nil) + %w[id] + end end diff --git a/app/models/proxy_exercise.rb b/app/models/proxy_exercise.rb index 497a2d70..63f92d7e 100644 --- a/app/models/proxy_exercise.rb +++ b/app/models/proxy_exercise.rb @@ -246,4 +246,8 @@ class ProxyExercise < ApplicationRecord def select_easiest_exercise(exercises) exercises.order(:expected_difficulty).first end + + def self.ransackable_attributes(_auth_object = nil) + %w[title] + end end diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index 7668a8f2..b3b73274 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -77,6 +77,14 @@ class RequestForComment < ApplicationRecord # ugly, but necessary end + def ransackable_associations(_auth_object = nil) + %w[exercise submission] + end + + def ransackable_attributes(_auth_object = nil) + %w[solved] + end + private def row_number_user_sql diff --git a/app/models/submission.rb b/app/models/submission.rb index 6136c09c..d5317c3d 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -191,6 +191,10 @@ class Submission < ApplicationRecord result.merge(output) end + def self.ransackable_attributes(_auth_object = nil) + %w[study_group_id] + end + private def prepared_runner diff --git a/app/models/user.rb b/app/models/user.rb index cd5e5379..fb718c57 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,4 +41,8 @@ class User < ApplicationRecord def to_s displayname end + + def self.ransackable_attributes(_auth_object = nil) + %w[name email external_id consumer_id role] + end end diff --git a/app/views/exercises/_form.html.slim b/app/views/exercises/_form.html.slim index 0ed9e03f..876cf47a 100644 --- a/app/views/exercises/_form.html.slim +++ b/app/views/exercises/_form.html.slim @@ -69,7 +69,7 @@ thead tr th = t('activerecord.attributes.exercise.selection') - th = sort_link(@search, :title, t('activerecord.attributes.tag.name')) + th = t('activerecord.attributes.tag.name') th = t('activerecord.attributes.tag.difficulty') = collection_check_boxes :exercise, :tag_ids, @exercise_tags, :tag_id, :id do |b| tr