Try to fix problems with removed request_store gem

This commit is contained in:
Alexander Meindl 2023-11-21 17:52:18 +01:00
parent c802d02fad
commit 5603d2f29d
5 changed files with 85 additions and 15 deletions

View File

@ -1,9 +1,17 @@
# frozen_string_literal: true
class AdditionalsJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
if Redmine::VERSION.to_s >= '5.1'
class AdditionalsJob < ApplicationJob
end
else
class AdditionalsJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
include Additionals::JobWrapper
around_enqueue :keep_current_user
end
end

View File

@ -118,20 +118,19 @@ class Dashboard < ActiveRecord::Base
if user.admin?
scope.where.not(visibility: VISIBILITY_PRIVATE).or(scope.where(author_id: user.id))
elsif user.memberships.includes([:memberships]).any?
scope.where("#{table_name}.visibility = ?" \
" OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" \
scope.where "#{table_name}.visibility = :public" \
" OR (#{table_name}.visibility = :roles AND #{table_name}.id IN (" \
"SELECT DISTINCT d.id FROM #{table_name} d" \
" INNER JOIN #{DashboardRole.table_name} dr ON dr.dashboard_id = d.id" \
" INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = dr.role_id" \
" INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" \
" INNER JOIN #{Project.table_name} p ON p.id = m.project_id AND p.status <> ?" \
" INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = :user_id" \
" INNER JOIN #{Project.table_name} p ON p.id = m.project_id AND p.status IN(:statuses)" \
' WHERE d.project_id IS NULL OR d.project_id = m.project_id))' \
" OR #{table_name}.author_id = ?",
VISIBILITY_PUBLIC,
VISIBILITY_ROLES,
user.id,
Project::STATUS_ARCHIVED,
user.id)
" OR #{table_name}.author_id = :user_id",
public: VISIBILITY_PUBLIC,
roles: VISIBILITY_ROLES,
user_id: user.id,
statuses: Project.usable_status_ids
elsif user.logged?
scope.where(visibility: VISIBILITY_PUBLIC).or(scope.where(author_id: user.id))
else

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Additionals
module JobWrapper
def keep_current_user
current_user = User.current
yield
User.current = current_user
end
end
end

View File

@ -5,6 +5,9 @@ module Additionals
module ProjectPatch
extend ActiveSupport::Concern
USABLE_STATUSES = { Project::STATUS_ACTIVE => :active,
Project::STATUS_CLOSED => :closed }.freeze
included do
prepend InstanceOverwriteMethods
include InstanceMethods
@ -14,6 +17,30 @@ module Additionals
safe_attributes 'enable_new_ticket_message', 'new_ticket_message'
end
class_methods do
def usable_status_ids
USABLE_STATUSES.keys
end
def sql_for_usable_status(table = nil)
table ||= Project.table_name
"#{table}.status IN(#{usable_status_ids.join ', '})"
end
def available_status_ids
available_statuses.keys
end
def available_statuses
statuses = USABLE_STATUSES.dup
statuses[Project::STATUS_ARCHIVED] = :archived
statuses[Project::STATUS_SCHEDULED_FOR_DELETION] = :scheduled_for_deletion if Redmine::VERSION.to_s >= '5.1'
statuses
end
end
module InstanceOverwriteMethods
def assignable_users(tracker = nil)
super

View File

@ -150,4 +150,29 @@ class ProjectTest < Additionals::TestCase
assert_save role
assert project.consider_hidden_roles?
end
def test_usable_status_ids
ids = Project.usable_status_ids
assert_sorted_equal ids, [Project::STATUS_ACTIVE, Project::STATUS_CLOSED]
end
def test_sql_for_usable_status
assert_equal "projects.status IN(#{Project::STATUS_ACTIVE}, #{Project::STATUS_CLOSED})",
Project.sql_for_usable_status
assert_equal "projects.status IN(#{Project::STATUS_ACTIVE}, #{Project::STATUS_CLOSED})",
Project.sql_for_usable_status(:projects)
assert_equal "subprojects.status IN(#{Project::STATUS_ACTIVE}, #{Project::STATUS_CLOSED})",
Project.sql_for_usable_status('subprojects')
end
def test_available_status_ids
ids = Project.available_status_ids
if Redmine::VERSION.to_s < '5.1'
assert_equal 3, ids.count
else
assert_operator ids.count, :>, 3
end
end
end