diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 5b55c1536..7ba3e1e47 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -13,10 +13,10 @@ class SearchController < ApplicationController case params[:group] when 'projects' - @project_search_count = fetch_cached_count Project + @project_search_count = fetch_cached_count(Project) search_projects if params[:preview] == 'true' - results = @project_results.limit(4) + results = @project_results&.limit(4) || [] else results = @project_results.page(params[:page]).per(Constants::SEARCH_LIMIT) end @@ -28,6 +28,22 @@ class SearchController < ApplicationController next_page: (results.next_page if results.respond_to?(:next_page)), } return + when 'protocols' + @protocol_search_count = fetch_cached_count(Protocol) + search_protocols + results = if params[:preview] == 'true' + @protocol_results&.limit(4) || [] + else + @protocol_results.page(params[:page]).per(Constants::SEARCH_LIMIT) + end + + render json: results, + each_serializer: GlobalSearch::ProtocolSerializer, + meta: { + total: @search_count, + next_page: (results.next_page if results.respond_to?(:next_page)), + } + return end #@search_id = params[:search_id] ? params[:search_id] : generate_search_id diff --git a/app/javascript/vue/global_search/groups/protocols.vue b/app/javascript/vue/global_search/groups/protocols.vue index 8055dc1de..6b6d8dc43 100644 --- a/app/javascript/vue/global_search/groups/protocols.vue +++ b/app/javascript/vue/global_search/groups/protocols.vue @@ -3,12 +3,56 @@

{{ i18n.t('search.index.protocol_templates') }} + [{{ total }}]

+
+
+ +
+
+ +
+
diff --git a/app/serializers/global_search/protocol_serializer.rb b/app/serializers/global_search/protocol_serializer.rb new file mode 100644 index 000000000..feac6d3fd --- /dev/null +++ b/app/serializers/global_search/protocol_serializer.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module GlobalSearch + class ProtocolSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + + attributes :id, :name, :code, :created_at, :updated_at, :created_by, :team, :archived, :url + + def team + { + name: object.team.name, + url: protocols_path(team: object.team) + } + end + + def created_by + { + name: object.created_by.name, + avatar_url: avatar_path(object.created_by, :icon_small) + } + end + + def created_at + I18n.l(object.created_at, format: :full_date) + end + + def updated_at + I18n.l(object.updated_at, format: :full_date) + end + + def url + protocol_path(object) + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 3aa90769e..faf6df1c0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -457,6 +457,8 @@ en: clear_filters: "Clear filters" id: "ID" created_at: "Created on" + created_by: "Created by" + updated_at: "Updated on" team: "Team" folder: "Folder" comments: