diff --git a/app/controllers/stores_controller.rb b/app/controllers/stores_controller.rb index 251f37cdc..dde7d9e2d 100644 --- a/app/controllers/stores_controller.rb +++ b/app/controllers/stores_controller.rb @@ -25,13 +25,15 @@ class StoresController < ApplicationController LIMIT = 12 unless const_defined?(:LIMIT) def index - @projects_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 0, 1). - reorder("#{Attachment.table_name}.downloads DESC"). - limit(LIMIT) + @projects_attach = project_classification(0).take(LIMIT) + @courses_attach = project_classification(1).take(LIMIT) + # @projects_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 0, 1). + # reorder("#{Attachment.table_name}.downloads DESC"). + # limit(LIMIT) - @courses_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 1, 1). - reorder("#{Attachment.table_name}.downloads DESC"). - limit(LIMIT) + # @courses_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 1, 1). + # reorder("#{Attachment.table_name}.downloads DESC"). + # limit(LIMIT) @homeworks_attach = Attachment.where("container_type = 'HomeworkAttach'"). reorder("downloads DESC"). limit(LIMIT) @@ -49,30 +51,45 @@ class StoresController < ApplicationController private def project_classification project_type=0 - pro_attach = Attachment.joins("LEFT JOIN projects ON attachments.container_id = projects.id"). - where("projects.project_type=#{project_type}"). - reorder("downloads DESC"). - limit(LIMIT) + # pro_attach = Attachment.joins("LEFT JOIN projects ON attachments.container_id = projects.id"). + # where("attachments.container_type='Project' AND projects.is_public=1 AND projects.project_type=#{project_type}"). + # reorder("downloads DESC"). + # limit(LIMIT) + pro_attach = join_tools_project Project, project_type doc_attach = join_tools_project Document, project_type issue_attach = join_tools_project Issue, project_type - mess_attach = [] - news_attach = join_tools_project News"news", project_type - vers_attach = join_tools_project Version"versions", project_type - wiki_attach = [] + mess_attach = join_tools_project Message, project_type + news_attach = join_tools_project News, project_type + vers_attach = join_tools_project Version, project_type + wiki_attach = join_tools_project WikiPage, project_type - tmp = pro_attach+doc_attach+issue_attach+mess_attach+news_attach+vers_attach+wiki_attach + tmp = pro_attach + tmp = pro_attach+doc_attach+issue_attach+mess_attach+news_attach+vers_attach+wiki_attach tmp.sort { |a, b| b.downloads <=> a.downloads } end def join_tools_project tableName, project_type=0 Attachment.joins(str_join_table(tableName)). - where("projects.project_type=#{project_type}"). + where("attachments.container_type='#{tableName.to_s}' AND projects.is_public=1 AND projects.project_type=#{project_type}"). reorder('downloads DESC'). limit(LIMIT) end def str_join_table tableClass - str = "LEFT JOIN #{tableClass.table_name} ON attachments.container_id = #{tableClass.table_name}.id - LEFT JOIN projects ON #{tableClass.table_name}.project_id = projects.id" - str + case tableClass.to_s + when 'Project' + "LEFT JOIN projects ON attachments.container_id = projects.id" + when 'Document', 'Issue', 'Version', 'News' # 连接子表即有 project_id 字段,即两层连接 + "LEFT JOIN #{tableClass.table_name} ON attachments.container_id = #{tableClass.table_name}.id + LEFT JOIN projects ON #{tableClass.table_name}.project_id = projects.id" + when 'Message' # 三层连接 + "LEFT JOIN #{tableClass.table_name} ON attachments.container_id = #{tableClass.table_name}.id + LEFT JOIN boards ON boards.id = #{tableClass.table_name}.board_id + LEFT JOIN projects ON boards.project_id = projects.id" + when 'WikiPage'# 三层连接 + "LEFT JOIN #{tableClass.table_name} ON attachments.container_id = #{tableClass.table_name}.id + LEFT JOIN wikis ON wikis.id = #{tableClass.table_name}.wiki_id + LEFT JOIN projects ON wikis.project_id = projects.id" + else + end end end diff --git a/app/views/layouts/base_stores.html.erb b/app/views/layouts/base_stores.html.erb index 9d125b251..00760263b 100644 --- a/app/views/layouts/base_stores.html.erb +++ b/app/views/layouts/base_stores.html.erb @@ -2,6 +2,7 @@
+