Implement global search results component for inventory items [SCI-10476]

This commit is contained in:
sboursen-scinote 2024-04-03 00:55:09 +02:00
parent 5b7ca1e49d
commit 84dd5eaa70
5 changed files with 138 additions and 1 deletions

View file

@ -73,6 +73,22 @@ class SearchController < ApplicationController
next_page: (results.next_page if results.respond_to?(:next_page)),
}
return
when 'repository_rows'
@repository_row_search_count = fetch_cached_count(RepositoryRow)
search_repository_rows
results = if params[:preview] == 'true'
@repository_row_results.take(4)
else
@repository_row_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
render json: results,
each_serializer: GlobalSearch::RepositoryRowSerializer,
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
@ -372,6 +388,12 @@ class SearchController < ApplicationController
@search_count = current_repository_search_count
end
def search_repository_rows
@repository_row_results = []
@repository_row_results = search_by_name(RepositoryRow) if @repository_row_search_count.positive?
@search_count = @repository_row_search_count
end
def search_assets
@asset_results = []
@asset_results = search_by_name(Asset) if @asset_search_count.positive?

View file

@ -3,12 +3,58 @@
<h2 class="flex items-center gap-2 mt-0 mb-4">
<i class="sn-icon sn-icon-inventory"></i>
{{ i18n.t('search.index.inventory_items') }}
[{{ total }}]
</h2>
<div>
<div class="grid grid-cols-[auto_110px_auto_auto_auto_auto] items-center">
<template v-for="row in preparedResults" :key="row.id">
<a :href="row.attributes.url" class="h-full py-2 px-4 overflow-hidden font-bold border-0 border-b border-solid border-sn-light-grey">
<StringWithEllipsis class="w-full" :text="row.attributes.name"></StringWithEllipsis>
</a>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.id') }}:</b>
<span class="shrink-0">{{ row.attributes.code }}</span>
</div>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.created_at') }}:</b>
<span class="shrink-0">{{ row.attributes.created_at }}</span>
</div>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.created_by') }}:</b>
<img :src="row.attributes.created_by.avatar_url" class="w-5 h-5 border border-sn-super-light-grey rounded-full mx-1" />
<span class="shrink-0">{{ row.attributes.created_by.name }}</span>
</div>
<div class="h-full py-2 px-4 grid grid-cols-[auto_1fr] items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.team') }}:</b>
<a :href="row.attributes.team.url" class="shrink-0 overflow-hidden">
<StringWithEllipsis class="w-full" :text="row.attributes.team.name"></StringWithEllipsis>
</a>
</div>
<div class="h-full py-2 px-4 grid grid-cols-[auto_1fr] items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.repository') }}:</b>
<a :href="row.attributes.repository.url" class="shrink-0 overflow-hidden">
<StringWithEllipsis class="w-full" :text="row.attributes.repository.name"></StringWithEllipsis>
</a>
</div>
</template>
</div>
<div v-if="!selected && total > 4" class="mt-4">
<button class="btn btn-light" @click="$emit('selectGroup', 'RepositoryRowsComponent')">View all</button>
</div>
</div>
</div>
</template>
<script>
import searchMixin from './search_mixin';
export default {
name: 'RepositoryRowsComponent'
name: 'RepositoryRowsComponent',
mixins: [searchMixin],
data() {
return {
group: 'repository_rows'
};
}
};
</script>

View file

@ -120,6 +120,35 @@ class RepositoryRow < ApplicationRecord
where(repository: Repository.viewable_by_user(user, teams))
end
def self.search(user,
include_archived,
query = nil,
page = 1,
_current_team = nil,
options = {})
searchable_row_fields = [RepositoryRow::PREFIXED_ID_SQL, 'repository_rows.name', 'users.full_name']
repositories = Repository.search(user).pluck(:id)
new_query =
RepositoryRow
.joins(:repository, :created_by)
.where(repository_id: repositories)
.distinct
.where_attributes_like(
searchable_row_fields, query, options
)
new_query = new_query.active unless include_archived
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
else
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
end
end
def self.filter_by_teams(teams = [])
return self if teams.blank?

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
module GlobalSearch
class RepositoryRowSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :name, :code, :created_at, :created_by, :team, :repository, :archived, :url
def team
{
name: object.team.name,
url: repository_path(object.repository)
}
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 repository
{
name: object.repository.name,
url: repository_path(object.repository)
}
end
def url
# switch to repository_repository_rows_path when inventory items page is implemented
repository_path(object.repository)
end
end
end

View file

@ -453,6 +453,7 @@ en:
protocol_templates: "Protocol templates"
label_templates: "Label templates"
reports: "Reports"
repository: "Inventory"
more_search_options: "More search options"
clear_filters: "Clear filters"
id: "ID"