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
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)
time.blank? ? '' : time.strftime("%Y-%m-%d %H:%M:%S")

View File

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

View File

@ -2,9 +2,6 @@ module Matchable
extend ActiveSupport::Concern
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_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) }

View File

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

View File

@ -10,8 +10,10 @@ class Projects::ListQuery < ApplicationQuery
end
def call
projects = Project.visible
scope = projects.includes(:project_category, :project_language).like(params[:search])
q = Project.visible.ransack(name_or_identifier_cont: params[:search])
scope = q.result(distinct: true)
.includes(:project_category, :project_language, :repository, owner: :user_extension)
.with_project_type(params[:project_type])
.with_project_category(params[:category_id])
.with_project_language(params[:language_id])
@ -19,13 +21,7 @@ class Projects::ListQuery < ApplicationQuery
sort = params[:sort_by] || "updated_on"
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
#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

View File

@ -1,30 +1,39 @@
json.total_count @total_count
json.projects do
json.array! @projects.to_a do |project|
user = project.owner
if user.present?
json.partial! 'project', project: project
json.author do
json.name user.try(:show_real_name)
json.login user.login
json.image_url url_to_avatar(project.owner)
end
json.category do
if project.project_category.blank?
json.nil!
else
json.id project.project_category.id
json.name project.project_category.name
end
end
json.language do
if project.project_language.blank?
json.nil!
else
json.id project.project_language.id
json.name project.project_language.name
end
end
json.projects @projects do |project|
user = project.owner
next if user.blank?
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.name user.try(:show_real_name)
json.login user.login
json.image_url url_to_avatar(project.owner)
end
json.category do
if project.project_category.blank?
json.nil!
else
json.id project.project_category.id
json.name project.project_category.name
end
end
json.language do
if project.project_language.blank?
json.nil!
else
json.id project.project_language.id
json.name project.project_language.name
end
end
end