parent
4543fb83d8
commit
191f5ec677
@ -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.
|
@ -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/
|
@ -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
|
@ -0,0 +1,2 @@
|
||||
module SubjectListsHelper
|
||||
end
|
@ -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
|
@ -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
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe SubjectListsController, type: :controller do
|
||||
|
||||
end
|
@ -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
|
Loading…
Reference in new issue