bigdir/app/controllers/stores_controller.rb

113 lines
5.0 KiB
Ruby
Raw Normal View History

2014-01-15 17:28:53 +08:00
# encoding: utf-8
# Trustie - education management software
# Copyright (C) 2013-2014
class StoresController < ApplicationController
layout 'base_stores'
2014-01-14 15:01:11 +08:00
def search
2014-01-14 17:21:27 +08:00
name = params[:name] ||= ''
2014-01-17 15:37:57 +08:00
(redirect_to stores_path, :notice => l(:label_sumbit_empty);return) if name.blank?
2014-01-20 08:59:17 +08:00
result = find_public_attache name
@searched_attach = paginateHelper result
@result_all_count = result.count;
end
def find_public_attache keywords
# StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map
# 此时内容不多速度还可但文件增长每条判断多则进行3-4次表连接。
# 现在还木有思路 药丸
resultSet = Attachment.where("attachments.container_type IS NOT NULL AND filename LIKE :like ", like: "%#{keywords}%").
2014-01-15 10:40:37 +08:00
reorder("created_on DESC")
2014-01-17 10:18:20 +08:00
result = resultSet.to_a.dup
2014-01-17 16:13:05 +08:00
resultSet.to_a.map { |res|
2014-01-17 10:19:18 +08:00
if(res.container.nil? ||
(res.container.class.to_s=="Project" && res.container.is_public == false) ||
(res.container.has_attribute?(:project) && res.container.project.is_public == false) ||
(res.container.class.to_s=="HomeworkAttach" && res.container.bid.reward_type == 3) ||
false
)
result.delete(res)
end
}
2014-01-20 08:59:17 +08:00
result
2014-01-14 15:01:11 +08:00
end
LIMIT = 12 unless const_defined?(:LIMIT)
def index
2014-01-16 09:47:50 +08:00
@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)
2014-01-13 16:34:24 +08:00
2014-01-16 09:47:50 +08:00
# @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.
# joins("LEFT JOIN homework_attaches ON homework_attaches.id=attachments.container_id
# LEFT JOIN bids ON homework_attaches.bid_id=bids.id").
# where("container_type = 'HomeworkAttach' AND bids.reward_type <> 3").
# reorder("downloads DESC").
# limit(LIMIT)
@homeworks_attach = join_tools_project Message, 0
2014-01-13 16:24:13 +08:00
@memos_attach = Attachment.where("container_type = 'Memo'").
2014-01-15 20:06:49 +08:00
reorder("downloads DESC").
2014-01-13 16:24:13 +08:00
limit(LIMIT)
2014-01-13 20:26:40 +08:00
@attach_array = Array.new
@attach_array.push(@projects_attach, @courses_attach, @homeworks_attach, @memos_attach)
2014-01-14 15:41:56 +08:00
@str_arr = [ l(:label_project_deposit),
l(:label_course_practice),
l(:label_borad_project), #l(:label_contest_innovate),
2014-01-14 15:41:56 +08:00
l(:label_forum) ]
end
2014-01-11 09:08:15 +08:00
2014-01-13 16:34:24 +08:00
private
2014-01-13 20:26:40 +08:00
2014-01-13 16:34:24 +08:00
def project_classification project_type=0
2014-01-16 09:47:50 +08:00
# 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
2014-01-14 20:48:43 +08:00
doc_attach = join_tools_project Document, project_type
issue_attach = join_tools_project Issue, project_type
mess_attach = []#join_tools_project Message, project_type
2014-01-16 09:47:50 +08:00
news_attach = join_tools_project News, project_type
vers_attach = join_tools_project Version, project_type
wiki_attach = join_tools_project WikiPage, project_type
2014-01-13 20:26:40 +08:00
2014-01-16 09:47:50 +08:00
tmp = pro_attach
tmp = pro_attach+doc_attach+issue_attach+mess_attach+news_attach+vers_attach+wiki_attach
2014-01-13 21:02:45 +08:00
tmp.sort { |a, b| b.downloads <=> a.downloads }
2014-01-13 20:26:40 +08:00
end
def join_tools_project tableName, project_type=0
2014-01-13 21:02:45 +08:00
Attachment.joins(str_join_table(tableName)).
2014-01-16 09:47:50 +08:00
where("attachments.container_type='#{tableName.to_s}' AND projects.is_public=1 AND projects.project_type=#{project_type}").
2014-01-13 21:02:45 +08:00
reorder('downloads DESC').
limit(LIMIT)
2014-01-13 16:34:24 +08:00
end
2014-01-14 20:48:43 +08:00
def str_join_table tableClass
2014-01-16 09:47:50 +08:00
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
2014-01-13 16:34:24 +08:00
end
end