Merge pull request #2322 from okriuchykhin/ok_SCI_4251

Fix global search for repositories and checklists update [SCI-4251]
This commit is contained in:
Alex Kriuchykhin 2020-01-16 16:37:12 +01:00 committed by GitHub
commit e3e36d40a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 34 deletions

View file

@ -138,9 +138,7 @@ class SearchController < ApplicationController
repository_results[:repository] = repository
repository_results[:count] = 0
search_results.each do |result|
if repository.id == result.id
repository_results[:count] += result.counter
end
repository_results[:count] += result.counter if repository.id == result.id
end
team_results[:repositories][repository.name] = repository_results
team_results[:count] += repository_results[:count]

View file

@ -57,33 +57,30 @@ class Repository < ApplicationRecord
repository = nil,
options = {}
)
repositories = repository ||
Repository.accessible_by_teams(user.teams.pluck(:id))
repositories = repository&.id || Repository.accessible_by_teams(user.teams.pluck(:id)).pluck(:id)
includes_json = { repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES }
searchable_attributes = ['repository_rows.name', 'users.full_name'] +
Extends::REPOSITORY_EXTRA_SEARCH_ATTR
readable_rows = RepositoryRow.joins(:repository).where(repository_id: repositories)
all_rows = RepositoryRow.where(repository: repositories)
matched_by_user = readable_rows.joins(:created_by).where_attributes_like('users.full_name', query, options)
new_query = RepositoryRow
.distinct
.from(all_rows, 'repository_rows')
.left_outer_joins(:created_by)
.left_outer_joins(includes_json)
.where_attributes_like(searchable_attributes, query, options)
repository_row_matches =
readable_rows.where_attributes_like(['repository_rows.name', 'repository_rows.id'], query, options)
repository_rows = readable_rows.where(id: repository_row_matches)
repository_rows = repository_rows.or(readable_rows.where(id: matched_by_user))
Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |field, include_hash|
custom_cell_matches = readable_rows.joins(repository_cells: include_hash)
.where_attributes_like(field, query, options)
repository_rows = repository_rows.or(readable_rows.where(id: custom_cell_matches))
end
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
.joins(:repository)
.select(
'repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter'
)
repository_rows.select('repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter')
.group('repositories.id')
else
new_query
.limit(Constants::SEARCH_LIMIT)
repository_rows.limit(Constants::SEARCH_LIMIT)
.offset((page - 1) * Constants::SEARCH_LIMIT)
end
end

View file

@ -33,11 +33,12 @@ class RepositoryChecklistValue < ApplicationRecord
end
def update_data!(new_data, user)
repository_checklist_items_values.destroy_all
repository_cell.repository_column
.repository_checklist_items.where(id: JSON.parse(new_data)).find_each do |item|
repository_checklist_items_values.create!(repository_checklist_item: item)
end
item_ids = JSON.parse(new_data)
return destroy! if item_ids.blank?
self.repository_checklist_items = repository_cell.repository_column
.repository_checklist_items
.where(id: item_ids)
self.last_modified_by = user
save!
end
@ -46,7 +47,8 @@ class RepositoryChecklistValue < ApplicationRecord
value = new(attributes)
value.repository_checklist_items = value.repository_cell
.repository_column
.repository_checklist_items.where(id: JSON.parse(payload))
.repository_checklist_items
.where(id: JSON.parse(payload))
value
end
end

View file

@ -1,9 +1,9 @@
# frozen_string_literal: true
class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0]
def change
def up
create_table :repository_status_items do |t|
t.string :status, null: false, index: true
t.string :status, null: false
t.string :icon, null: false
t.references :repository, null: false, foreign_key: true
t.references :repository_column, null: false, foreign_key: true
@ -12,5 +12,11 @@ class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0]
t.timestamps
end
add_gin_index_without_tags :repository_status_items, :status
end
def down
drop_table :repository_status_items
end
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class CreateRepositoryChecklists < ActiveRecord::Migration[6.0]
def change
def up
create_table :repository_checklist_values do |t|
t.references :created_by, index: true, foreign_key: { to_table: :users }, null: true
t.references :last_modified_by, index: true, foreign_key: { to_table: :users }, null: true
@ -10,7 +10,7 @@ class CreateRepositoryChecklists < ActiveRecord::Migration[6.0]
end
create_table :repository_checklist_items do |t|
t.string :data, null: false, index: true
t.string :data, null: false
t.references :repository, null: false, foreign_key: true
t.references :repository_column, null: false, foreign_key: true
t.references :created_by, index: true, foreign_key: { to_table: :users }, null: true
@ -19,10 +19,18 @@ class CreateRepositoryChecklists < ActiveRecord::Migration[6.0]
t.timestamps
end
add_gin_index_without_tags :repository_checklist_items, :data
create_table :repository_checklist_items_values do |t|
t.belongs_to :repository_checklist_value, index: { name: 'index_on_repository_checklist_value_id' }
t.belongs_to :repository_checklist_item, index: { name: 'index_on_repository_checklist_item_id' }
t.timestamps
end
end
def down
drop_table :repository_checklist_items_values
drop_table :repository_checklist_items
drop_table :repository_checklist_values
end
end