FIX query projects list

This commit is contained in:
Jasder 2020-05-09 15:51:40 +08:00
parent 31afff4907
commit 1e0811006b
6 changed files with 48 additions and 43 deletions

View File

@ -668,7 +668,13 @@ class ApplicationController < ActionController::Base
end end
end end
def kaminari_paginate(relation)
limit = params[:limit] || params[:per_page]
limit = (limit.to_i.zero? || limit.to_i > 15) ? 15 : limit.to_i
page = params[:page].to_i.zero? ? 1 : params[:page].to_i
relation.page(page).per(limit)
end
def strf_time(time) def strf_time(time)
time.blank? ? '' : time.strftime("%Y-%m-%d %H:%M:%S") time.blank? ? '' : time.strftime("%Y-%m-%d %H:%M:%S")

View File

@ -8,8 +8,8 @@ class ProjectsController < ApplicationController
def index def index
scope = Projects::ListQuery.call(params) scope = Projects::ListQuery.call(params)
@total_count = scope.size @projects = kaminari_paginate(scope)
@projects = paginate(scope) @total_count = @projects.total_count
end end
def create def create

View File

@ -2,9 +2,6 @@ module Matchable
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
scope :like, lambda { |keywords|
joins(:repository).where(%w[ projects.name projects.identifier repositories.identifier ].map { |f| "LOWER(#{f}) LIKE :q" }.join(' OR '), q: "%#{keywords.split(" ").join('|')}%") unless keywords.blank?
}
scope :with_project_category, ->(category_id) { where(project_category_id: category_id) unless category_id.blank? } scope :with_project_category, ->(category_id) { where(project_category_id: category_id) unless category_id.blank? }
scope :with_project_language, ->(language_id) { where(project_language_id: language_id) unless language_id.blank? } scope :with_project_language, ->(language_id) { where(project_language_id: language_id) unless language_id.blank? }
scope :with_project_type, ->(project_type) { where(project_type: project_type) if Project.project_types.include?(project_type) } scope :with_project_type, ->(project_type) { where(project_type: project_type) if Project.project_types.include?(project_type) }

View File

@ -5,9 +5,6 @@ module Projectable
has_many :projects, -> { order(position: :asc) } has_many :projects, -> { order(position: :asc) }
scope :without_content, -> { select(column_names - ['content'])} scope :without_content, -> { select(column_names - ['content'])}
scope :search, lambda { |keywords|
where("name LIKE ?", "%#{keywords.split(" ").join('|')}%") unless keywords.blank?
}
end end
module ClassMethods module ClassMethods

View File

@ -10,8 +10,10 @@ class Projects::ListQuery < ApplicationQuery
end end
def call def call
projects = Project.visible q = Project.visible.ransack(name_or_identifier_cont: params[:search])
scope = projects.includes(:project_category, :project_language).like(params[:search])
scope = q.result(distinct: true)
.includes(:project_category, :project_language, :repository, owner: :user_extension)
.with_project_type(params[:project_type]) .with_project_type(params[:project_type])
.with_project_category(params[:category_id]) .with_project_category(params[:category_id])
.with_project_language(params[:language_id]) .with_project_language(params[:language_id])
@ -19,13 +21,7 @@ class Projects::ListQuery < ApplicationQuery
sort = params[:sort_by] || "updated_on" sort = params[:sort_by] || "updated_on"
sort_direction = params[:sort_direction] || "desc" sort_direction = params[:sort_direction] || "desc"
scope = scope.no_anomory_projects.distinct.includes(:project_category, :project_language, :repository, owner: :user_extension).reorder("projects.#{sort} #{sort_direction}") scope = scope.no_anomory_projects.reorder("projects.#{sort} #{sort_direction}")
scope scope
#cope_ids = scope.select(:id,:user_id).no_anomory_projects.distinct.pluck(:id)
#sort = params[:sort_by] || "updated_on"
#sort_direction = params[:sort_direction] || "desc"
#projects.where(id: scope_ids).includes(:project_category, :project_language, :repository, owner: :user_extension).reorder("projects.#{sort} #{sort_direction}")
end end
end end

View File

@ -1,9 +1,20 @@
json.total_count @total_count json.total_count @total_count
json.projects do json.projects @projects do |project|
json.array! @projects.to_a do |project|
user = project.owner user = project.owner
if user.present? next if user.blank?
json.partial! 'project', project: project
json.id project.id
json.identifier project.identifier
json.name project.name
json.description Nokogiri::HTML(project.description).text
json.visits project.visits
json.praises_count project.praises_count
json.forked_count project.forked_count
json.is_public project.is_public
json.mirror_url project.repository&.mirror_url
json.last_update_time render_unix_time(project.updated_on)
json.time_ago time_from_now(project.updated_on)
json.forked_from_project_id project.forked_from_project_id
json.author do json.author do
json.name user.try(:show_real_name) json.name user.try(:show_real_name)
json.login user.login json.login user.login
@ -25,6 +36,4 @@ json.projects do
json.name project.project_language.name json.name project.project_language.name
end end
end end
end
end
end end