From 04b040d3fea574f81606fd16f28f7f8b98a92e06 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 11 Nov 2019 10:21:07 +0800 Subject: [PATCH 1/2] laboratory: shixun filter menu api --- app/controllers/concerns/laboratory_helper.rb | 2 +- app/controllers/shixuns_controller.rb | 14 +++++- app/views/shixuns/menus.json.jbuilder | 46 +++++++++++++------ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/controllers/concerns/laboratory_helper.rb b/app/controllers/concerns/laboratory_helper.rb index 870a1d90e..6a32437bc 100644 --- a/app/controllers/concerns/laboratory_helper.rb +++ b/app/controllers/concerns/laboratory_helper.rb @@ -9,7 +9,7 @@ module LaboratoryHelper end def current_laboratory - @_current_laboratory ||= (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1)) + @_current_laboratory ||= (Laboratory.find_by_subdomain('hut' || request.subdomain) || Laboratory.find(1)) end def default_laboratory diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index cfe6141ee..c1676fbe9 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -154,7 +154,19 @@ class ShixunsController < ApplicationController ## 获取顶部菜单 def menus - @repertoires = Repertoire.includes(sub_repertoires: [:tag_repertoires]).order("updated_at asc") + where_sql = ShixunTagRepertoire.where("shixun_tag_repertoires.tag_repertoire_id = tag_repertoires.id") + + # 云上实验室过滤 + unless current_laboratory.main_site? + where_sql = where_sql.joins('JOIN laboratory_shixuns ls ON ls.shixun_id = shixun_tag_repertoires.shixun_id') + end + where_sql = where_sql.select('1').to_sql + tags = TagRepertoire.where("EXISTS(#{where_sql})").distinct.includes(sub_repertoire: :repertoire) + + @tags_map = tags.group_by(&:sub_repertoire) + @sub_reps_map = @tags_map.keys.group_by(&:repertoire) + + # @repertoires = Repertoire.includes(sub_repertoires: [:tag_repertoires]).order("updated_at asc") # respond_with @repertoires render_json diff --git a/app/views/shixuns/menus.json.jbuilder b/app/views/shixuns/menus.json.jbuilder index 379f13e52..c5021e88c 100644 --- a/app/views/shixuns/menus.json.jbuilder +++ b/app/views/shixuns/menus.json.jbuilder @@ -13,20 +13,36 @@ # ] # }] -json.array! @repertoires do |rep| - json.id rep.id - json.name rep.name - json.sub_repertoires do - json.array! rep.sub_repertoires do |sub| - json.id sub.id - json.name sub.name - json.tags do - json.array! sub.tag_repertoires do |tag| - json.id tag.id - json.name tag.name - end - end - end - end +# json.array! @repertoires do |rep| +# json.id rep.id +# json.name rep.name +# json.sub_repertoires do +# json.array! rep.sub_repertoires do |sub| +# json.id sub.id +# json.name sub.name +# json.tags do +# json.array! sub.tag_repertoires do |tag| +# json.id tag.id +# json.name tag.name +# end +# end +# end +# end +# end + +json.array! @sub_reps_map.keys do |rep| + json.extract! rep, :id, :name + + json.sub_repertoires do + json.array! @sub_reps_map[rep] do |sub_rep| + json.extract! sub_rep, :id, :name + + json.tags do + json.array! @tags_map[sub_rep] do |tag| + json.extract! tag, :id, :name + end + end + end + end end From 5a140414eec1835cbdba90f06fdb2bfd9c6fc402 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 11 Nov 2019 10:23:13 +0800 Subject: [PATCH 2/2] fix --- app/controllers/concerns/laboratory_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/concerns/laboratory_helper.rb b/app/controllers/concerns/laboratory_helper.rb index 6a32437bc..870a1d90e 100644 --- a/app/controllers/concerns/laboratory_helper.rb +++ b/app/controllers/concerns/laboratory_helper.rb @@ -9,7 +9,7 @@ module LaboratoryHelper end def current_laboratory - @_current_laboratory ||= (Laboratory.find_by_subdomain('hut' || request.subdomain) || Laboratory.find(1)) + @_current_laboratory ||= (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1)) end def default_laboratory