fix: project list query

This commit is contained in:
yystopf 2021-09-07 17:19:38 +08:00
parent 74a9743bcc
commit 84aed0391a
3 changed files with 17 additions and 4 deletions

View File

@ -28,7 +28,7 @@ class ProjectsController < ApplicationController
end
def index
scope = Projects::ListQuery.call(params)
scope = current_user.logged? ? Projects::ListQuery.call(params, current_user.id) : Projects::ListQuery.call(params)
# @projects = kaminari_paginate(scope)
@projects = paginate scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units)

View File

@ -137,6 +137,18 @@ class Project < ApplicationRecord
delegate :content, to: :project_detail, allow_nil: true
delegate :name, to: :license, prefix: true, allow_nil: true
def self.all_visible(user_id=nil)
user_projects_sql = Project.joins(:owner).where(users: {type: 'User'}).to_sql
org_public_projects_sql = Project.joins(:owner).merge(Organization.joins(:organization_extension).where(organization_extensions: {visibility: 'common'})).to_sql
if user_id.present?
org_limit_projects_sql = Project.joins(:owner).merge(Organization.joins(:organization_extension).where(organization_extensions: {visibility: 'limited'})).to_sql
org_privacy_projects_sql = Project.joins(:owner).merge(Organization.joins(:organization_extension, :organization_users).where(organization_extensions: {visibility: 'privacy'}, organization_users: {user_id: user_id})).to_sql
return Project.from("( #{ user_projects_sql } UNION #{ org_public_projects_sql } UNION #{ org_limit_projects_sql } UNION #{org_privacy_projects_sql} ) AS projects").visible
else
return Project.from("( #{ user_projects_sql } UNION #{ org_public_projects_sql } ) AS projects").visible
end
end
def reset_cache_data
if changes[:user_id].present?
first_owner = Owner.find_by_id(changes[:user_id].first)

View File

@ -1,16 +1,17 @@
class Projects::ListQuery < ApplicationQuery
include CustomSortable
attr_reader :params
attr_reader :params, :current_user_id
sort_columns :updated_on, :created_on, :forked_count, :praises_count, default_by: :updated_on, default_direction: :desc
def initialize(params)
def initialize(params, current_user_id=nil)
@params = params
@current_user_id = current_user_id
end
def call
q = Project.visible.by_name_or_identifier(params[:search])
q = Project.all_visible(current_user_id).by_name_or_identifier(params[:search])
scope = q
.with_project_type(params[:project_type])