Browse Source

Merge pull request #26 from lexintegritastech/improve-main-ui

Sort `subjects`
pull/27/head
Alexander D. Bondoc 4 years ago committed by GitHub
parent
commit
e30feafb5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/components/doctrine_modal_form_component/doctrine_modal_form_component.html.erb
  2. 21
      app/controllers/api/subjects_controller.rb
  3. 26
      app/controllers/concerns/subject_search.rb
  4. 2
      app/javascript/controllers/document_controller.js
  5. 8
      app/models/cdao/subject.rb
  6. 4
      app/views/api/subjects/index.json.jbuilder

2
app/components/doctrine_modal_form_component/doctrine_modal_form_component.html.erb

@ -11,7 +11,7 @@
<div class="row">
<div class="col-sm-12 p-2">
<strong> <%= label_tag :subjects %> </strong>
<%= select_tag "subject_ids[]", options_from_collection_for_select(subjects, :id, :lineage_name, doctrine.subject_ids), class: "form-select subject-ids-selectize", multiple: true, prompt: "Please select" %>
<%= select_tag "subject_ids[]", options_from_collection_for_select(subjects.sort_by { |sub| sub.lineage_name }, :id, :lineage_name, doctrine.subject_ids), class: "form-select subject-ids-selectize", multiple: true, prompt: "Please select" %>
</div>
</div>

21
app/controllers/api/subjects_controller.rb

@ -0,0 +1,21 @@
module Api
class SubjectsController < BaseController
include SubjectSearch
authorize_resource :doctrine, class: "Cdao::Subject", only: %i[index]
def index
@search = search_subject(search_params)
@subjects = @search.results
respond_with @subjects
end
private
def search_params
params.permit(:name, :parent_id, :state, doctrine_ids: [], exclude_ids: [])
end
end
end

26
app/controllers/concerns/subject_search.rb

@ -0,0 +1,26 @@
module SubjectSearch
def search_subject(search_params)
fulltext_fields = %i[name lineage_name].freeze
search = ::Cdao::Subject.search do
fulltext search_params[:q], fields: fulltext_fields if search_params[:q].present?
fulltext_fields.each do |field|
fulltext search_params[field], fields: %i[field], query_phrase_slop: 0, minimum_match: 1
end
with :parent_id, search_params[:parent_id].to_i if search_params[:parent_id].present?
without :parent_id if search_params[:is_root].present? && search_params[:is_root].to_s.eql?("true")
without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present?
order_by :lineage_name, :asc
order_by :name, :asc
paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20
end
search
end
end

2
app/javascript/controllers/document_controller.js

@ -19,7 +19,7 @@ export default class extends ApplicationController {
modalDocumentSearch () {
var $this = this
$.get("/api/jurisprudences.json", { q: $this.inputTarget.value }, function (data, status) {
$.get("/api/documents.json", { q: $this.inputTarget.value }, function (data, status) {
if (status === "success") {
$this.stimulate("DocumentReflex#render_modal_document_search_table", data)
}

8
app/models/cdao/subject.rb

@ -55,7 +55,15 @@ class Cdao::Subject < Cdao::Base
text :name
text :lineage_name do
lineage_name
end
string :name
string :state
string :lineage_name do
lineage_name
end
end
end

4
app/views/api/subjects/index.json.jbuilder

@ -0,0 +1,4 @@
json.array!(@subjects) do |subject|
json.extract! subject, *%i[id name parent_id state doctrine_ids created_at updated_at]
json.text [subject.lineage_name].join(" - ")
end
Loading…
Cancel
Save