From 191f5ec677c6d861c94533d1c069a601eb21553f Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 26 Sep 2019 14:37:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E7=94=A8=E5=AE=9E=E8=B7=B5=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=E6=94=B9=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/subject_lists.js | 2 + app/assets/stylesheets/subject_lists.scss | 3 ++ app/controllers/shixun_lists_controller.rb | 2 +- app/controllers/subject_lists_controller.rb | 10 +++++ app/helpers/subject_lists_helper.rb | 2 + app/models/searchable/subject.rb | 7 +++- app/services/subject_search_service.rb | 42 +++++++++++++++++++ app/views/subject_lists/index.json.jbuilder | 22 ++++++++++ config/routes.rb | 1 + .../subject_lists_controller_spec.rb | 5 +++ spec/helpers/subject_lists_helper_spec.rb | 15 +++++++ 11 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/subject_lists.js create mode 100644 app/assets/stylesheets/subject_lists.scss create mode 100644 app/controllers/subject_lists_controller.rb create mode 100644 app/helpers/subject_lists_helper.rb create mode 100644 app/services/subject_search_service.rb create mode 100644 app/views/subject_lists/index.json.jbuilder create mode 100644 spec/controllers/subject_lists_controller_spec.rb create mode 100644 spec/helpers/subject_lists_helper_spec.rb diff --git a/app/assets/javascripts/subject_lists.js b/app/assets/javascripts/subject_lists.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/subject_lists.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/subject_lists.scss b/app/assets/stylesheets/subject_lists.scss new file mode 100644 index 000000000..bf0f29103 --- /dev/null +++ b/app/assets/stylesheets/subject_lists.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the subject_lists controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/shixun_lists_controller.rb b/app/controllers/shixun_lists_controller.rb index e92e857ad..b57f0af21 100644 --- a/app/controllers/shixun_lists_controller.rb +++ b/app/controllers/shixun_lists_controller.rb @@ -5,6 +5,6 @@ class ShixunListsController < ApplicationController private def search_params - params.permit(:keyword, :type, :page, :limit, :order, :type, :status, :diff) + params.permit(:keyword, :type, :page, :limit, :order, :status, :diff) end end \ No newline at end of file diff --git a/app/controllers/subject_lists_controller.rb b/app/controllers/subject_lists_controller.rb new file mode 100644 index 000000000..c8a523d79 --- /dev/null +++ b/app/controllers/subject_lists_controller.rb @@ -0,0 +1,10 @@ +class SubjectListsController < ApplicationController + def index + @results = SubjectSearchService.call(search_params) + end + + private + def search_params + params.permit(:keyword, :type, :page, :limit, :order, :sort) + end +end diff --git a/app/helpers/subject_lists_helper.rb b/app/helpers/subject_lists_helper.rb new file mode 100644 index 000000000..92df21ed8 --- /dev/null +++ b/app/helpers/subject_lists_helper.rb @@ -0,0 +1,2 @@ +module SubjectListsHelper +end diff --git a/app/models/searchable/subject.rb b/app/models/searchable/subject.rb index a0036c200..64845a566 100644 --- a/app/models/searchable/subject.rb +++ b/app/models/searchable/subject.rb @@ -18,7 +18,9 @@ module Searchable::Subject def search_data { name: name, - description: Util.extract_content(description)[0..Searchable::MAXIMUM_LENGTH] + description: Util.extract_content(description)[0..Searchable::MAXIMUM_LENGTH], + shixuns_count: shixuns_count, + myshixuns_count: member_count, }.merge!(searchable_user_data) .merge!(searchable_stages_data) end @@ -45,7 +47,8 @@ module Searchable::Subject visits_count: visits, stage_count: stages_count, stage_shixuns_count: stage_shixuns_count, - shixuns_count: shixuns_count + shixuns_count: shixuns_count, + myshixuns_count: member_count } end diff --git a/app/services/subject_search_service.rb b/app/services/subject_search_service.rb new file mode 100644 index 000000000..2366cecc2 --- /dev/null +++ b/app/services/subject_search_service.rb @@ -0,0 +1,42 @@ +class SubjectSearchService < ApplicationService + include ElasticsearchAble + + attr_reader :params + + def initialize(params) + @params = params + end + + def call + # 全部实训/我的实训 + type = params[:type] || "all" + + if type == "mine" + @subjects = User.current.shixuns.visible.unhidden + else + @subjects = Subject.visible.unhidden + end + + Subject.search(keyword, search_options) + end + + private + + def search_options + model_options = { + includes: [ user: { user_extension: :school } ] + } + model_options.merge!(where: { id: @subjects.pluck(:id) }) + model_options.merge!(order: {sort_type => sort_str}) + model_options.merge!(default_options) + model_options + end + + def sort_str + params[:order] || "desc" + end + + def sort_type + params[:sort] || "myshixuns_count" + end +end \ No newline at end of file diff --git a/app/views/subject_lists/index.json.jbuilder b/app/views/subject_lists/index.json.jbuilder new file mode 100644 index 000000000..6992deb1f --- /dev/null +++ b/app/views/subject_lists/index.json.jbuilder @@ -0,0 +1,22 @@ +json.subjects_count @results.total_count + +json.subject_list do + json.array! @results.with_highlights(multiple: true) do |obj, highlights| + json.merge! obj.to_searchable_json + + # 去除开头标点符号 + reg = /^[,。?:;‘’!“”—……、]/ + # 附件的替换 + atta_reg = /!\[.*]\(\/api\/attachments\/\d+\)/ + + highlights[:description]&.first&.sub!(reg, '') + highlights[:description]&.map{|des| des.gsub!(atta_reg, '')} + highlights[:content]&.first&.sub!(reg, '') + highlights[:content]&.map{|des| des.gsub!(atta_reg, '')} + + json.title highlights.delete(:name)&.join('...') || obj.searchable_title + json.description highlights[:description]&.join('...') || Util.extract_content(obj.description)[0..300]&.sub!(atta_reg, '') + + json.content highlights + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f111d4894..bafd7dd0e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -174,6 +174,7 @@ Rails.application.routes.draw do end end + resources :subject_lists resources :shixun_lists resources :shixuns, param: :identifier do diff --git a/spec/controllers/subject_lists_controller_spec.rb b/spec/controllers/subject_lists_controller_spec.rb new file mode 100644 index 000000000..b6ce01674 --- /dev/null +++ b/spec/controllers/subject_lists_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SubjectListsController, type: :controller do + +end diff --git a/spec/helpers/subject_lists_helper_spec.rb b/spec/helpers/subject_lists_helper_spec.rb new file mode 100644 index 000000000..b48480161 --- /dev/null +++ b/spec/helpers/subject_lists_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the SubjectListsHelper. For example: +# +# describe SubjectListsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe SubjectListsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end