simplified some Active Record queries

This commit is contained in:
Hauke Klement
2015-03-11 14:31:19 +01:00
parent a06e20b6c5
commit a8bda864df
6 changed files with 6 additions and 6 deletions

View File

@ -2,7 +2,7 @@ class ErrorsController < ApplicationController
before_action :set_execution_environment before_action :set_execution_environment
def authorize! def authorize!
authorize(@error || Error.where(execution_environment_id: @execution_environment.id)) authorize(@error || @execution_environment.errors)
end end
private :authorize! private :authorize!

View File

@ -71,7 +71,7 @@ class ExercisesController < ApplicationController
private :handle_file_uploads private :handle_file_uploads
def implement def implement
@submission = Submission.where(exercise_id: @exercise.id, user_id: current_user.id).order('created_at DESC').first @submission = current_user.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first
@files = (@submission ? @submission.collect_files : @exercise.files).select(&:visible).sort_by(&:name_with_extension) @files = (@submission ? @submission.collect_files : @exercise.files).select(&:visible).sort_by(&:name_with_extension)
@paths = collect_paths(@files) @paths = collect_paths(@files)
end end

View File

@ -28,7 +28,7 @@ class HintsController < ApplicationController
private :hint_params private :hint_params
def index def index
@hints = Hint.where(execution_environment_id: @execution_environment.id).order(:name) @execution_environment.hints.order(:name)
authorize! authorize!
end end

View File

@ -52,7 +52,7 @@ class Submission < ActiveRecord::Base
end end
def siblings def siblings
Submission.where(exercise_id: exercise_id, user_id: user_id, user_type: user_type) user.submissions.where(exercise_id: exercise_id)
end end
def to_s def to_s

View File

@ -22,7 +22,7 @@ class ExercisePolicy < AdminOrAuthorPolicy
if @user.admin? if @user.admin?
@scope.all @scope.all
elsif @user.internal_user? elsif @user.internal_user?
@scope.where("user_id = #{@user.id} OR public = TRUE OR (team_id IS NOT NULL AND team_id IN (SELECT t.id FROM teams t JOIN internal_users_teams iut ON t.id = iut.team_id WHERE iut.internal_user_id = #{@user.id}))") @scope.where('user_id = ? OR public = TRUE OR (team_id IS NOT NULL AND team_id IN (SELECT t.id FROM teams t JOIN internal_users_teams iut ON t.id = iut.team_id WHERE iut.internal_user_id = ?))', @user.id, @user.id)
else else
@scope.none @scope.none
end end

View File

@ -2,7 +2,7 @@ class Whistleblower
PLACEHOLDER_REGEXP = /\$(\d)/ PLACEHOLDER_REGEXP = /\$(\d)/
def find_hint(stderr) def find_hint(stderr)
Hint.where(execution_environment_id: @execution_environment.id).detect do |hint| @execution_environment.hints.detect do |hint|
@matches = Regexp.new(hint.regular_expression).match(stderr) @matches = Regexp.new(hint.regular_expression).match(stderr)
end end
end end