From 6cc102cfd1e7665ba327d2a5b550ed7c18335266 Mon Sep 17 00:00:00 2001 From: Alex Kriuchykhin Date: Fri, 11 Feb 2022 13:50:42 +0100 Subject: [PATCH] Fix advanced filters for list and status data types [SCI-6524][SCI-6521] (#3844) --- .../filters/repositoryStatusValue.vue | 2 +- app/models/repository_list_value.rb | 20 +++++++++++++++---- app/models/repository_status_item.rb | 4 ++-- app/models/repository_status_value.rb | 20 +++++++++++++++---- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/app/javascript/vue/repository_filter/filters/repositoryStatusValue.vue b/app/javascript/vue/repository_filter/filters/repositoryStatusValue.vue index 2c3d8e985..27439818f 100644 --- a/app/javascript/vue/repository_filter/filters/repositoryStatusValue.vue +++ b/app/javascript/vue/repository_filter/filters/repositoryStatusValue.vue @@ -47,7 +47,7 @@ }, watch: { value() { - this.parameters = { status_ids: this.value }; + this.parameters = { item_ids: this.value }; this.updateFilter(); } }, diff --git a/app/models/repository_list_value.rb b/app/models/repository_list_value.rb index 8493cd5fc..ab6348788 100644 --- a/app/models/repository_list_value.rb +++ b/app/models/repository_list_value.rb @@ -28,12 +28,24 @@ class RepositoryListValue < ApplicationRecord end def self.add_filter_condition(repository_rows, join_alias, filter_element) - repository_rows + items_join_alias = "#{join_alias}_status_items" + repository_rows = + repository_rows .joins( - "INNER JOIN \"repository_list_items\"" \ - " ON \"repository_list_items\".\"id\" = \"#{join_alias}\".\"repository_list_item_id\"" + "LEFT OUTER JOIN \"repository_list_items\" AS \"#{items_join_alias}\" " \ + "ON \"#{join_alias}\".\"repository_list_item_id\" = \"#{items_join_alias}\".\"id\"" ) - .where(repository_list_items: { id: filter_element.parameters['item_ids'] }) + case filter_element.operator + when 'any_of' + repository_rows + .where("#{items_join_alias}.id = ANY(ARRAY[?]::bigint[])", filter_element.parameters['item_ids']) + when 'none_of' + repository_rows + .where("NOT #{items_join_alias}.id IN(?) OR #{items_join_alias}.id IS NULL", + filter_element.parameters['item_ids']) + else + raise ArgumentError, 'Wrong operator for RepositoryListValue!' + end end def data diff --git a/app/models/repository_status_item.rb b/app/models/repository_status_item.rb index acea4e714..003825d1f 100644 --- a/app/models/repository_status_item.rb +++ b/app/models/repository_status_item.rb @@ -21,8 +21,8 @@ class RepositoryStatusItem < ApplicationRecord def update_table_fiter_elements repository_column.repository_table_filter_elements.find_each do |filter_element| - filter_element.parameters['status_ids']&.delete(id.to_s) - filter_element.parameters['status_ids'].blank? ? filter_element.destroy! : filter_element.save! + filter_element.parameters['item_ids']&.delete(id.to_s) + filter_element.parameters['item_ids'].blank? ? filter_element.destroy! : filter_element.save! end end end diff --git a/app/models/repository_status_value.rb b/app/models/repository_status_value.rb index 66f2af681..af92da8a9 100644 --- a/app/models/repository_status_value.rb +++ b/app/models/repository_status_value.rb @@ -20,12 +20,24 @@ class RepositoryStatusValue < ApplicationRecord end def self.add_filter_condition(repository_rows, join_alias, filter_element) - repository_rows + items_join_alias = "#{join_alias}_status_items" + repository_rows = + repository_rows .joins( - "INNER JOIN \"repository_status_items\"" \ - " ON \"repository_status_items\".\"id\" = \"#{join_alias}\".\"repository_status_item_id\"" + "LEFT OUTER JOIN \"repository_status_items\" AS \"#{items_join_alias}\" " \ + "ON \"#{join_alias}\".\"repository_status_item_id\" = \"#{items_join_alias}\".\"id\"" ) - .where(repository_status_items: { id: filter_element.parameters['status_ids'] }) + case filter_element.operator + when 'any_of' + repository_rows + .where("#{items_join_alias}.id = ANY(ARRAY[?]::bigint[])", filter_element.parameters['item_ids']) + when 'none_of' + repository_rows + .where("NOT #{items_join_alias}.id IN(?) OR #{items_join_alias}.id IS NULL", + filter_element.parameters['item_ids']) + else + raise ArgumentError, 'Wrong operator for RepositoryStatusValue!' + end end def data_changed?(new_data)