diff --git a/app/models/repository_checklist_value.rb b/app/models/repository_checklist_value.rb index 6b9cd113e..8c9e54ad3 100644 --- a/app/models/repository_checklist_value.rb +++ b/app/models/repository_checklist_value.rb @@ -26,16 +26,34 @@ class RepositoryChecklistValue < ApplicationRecord end def self.add_filter_condition(repository_rows, join_alias, filter_element) - repository_rows + items_join_alias = "#{join_alias}_checklist_items" + repository_rows = + repository_rows .joins( - "INNER JOIN \"repository_checklist_items_values\"" \ - " ON \"repository_checklist_items_values\".\"repository_checklist_value_id\" = \"#{join_alias}\".\"id\"" + "LEFT OUTER JOIN \"repository_checklist_items_values\" AS \"#{join_alias}_checklist_items_values\" " \ + "ON \"#{join_alias}_checklist_items_values\".\"repository_checklist_value_id\" = \"#{join_alias}\".\"id\"" ) .joins( - 'INNER JOIN "repository_checklist_items"' \ - ' ON "repository_checklist_items_values"."repository_checklist_item_id" = "repository_checklist_items"."id"' + "LEFT OUTER JOIN \"repository_checklist_items\" AS \"#{items_join_alias}\" " \ + "ON \"#{join_alias}_checklist_items_values\".\"repository_checklist_item_id\" = \"#{items_join_alias}\".\"id\"" ) - .where(repository_checklist_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 'all_of' + repository_rows + .having("ARRAY_AGG(#{items_join_alias}.id ORDER BY #{items_join_alias}.id) @> ARRAY[?]::bigint[]", + filter_element.parameters['item_ids'].sort) + .group(:id) + when 'none_of' + repository_rows + .having("NOT ARRAY_AGG(#{items_join_alias}.id) && ARRAY[?]::bigint[]", filter_element.parameters['item_ids']) + .group(:id) + else + raise ArgumentError, 'Wrong operator for RepositoryChecklistValue!' + end end def data diff --git a/app/services/repository_datatable_service.rb b/app/services/repository_datatable_service.rb index 20eb1ad64..ef6636872 100644 --- a/app/services/repository_datatable_service.rb +++ b/app/services/repository_datatable_service.rb @@ -339,11 +339,11 @@ class RepositoryDatatableService repository_rows = repository_rows .joins( - "INNER JOIN \"repository_cells\" AS \"#{join_cells_alias}\"" \ + "LEFT OUTER JOIN \"repository_cells\" AS \"#{join_cells_alias}\"" \ " ON \"repository_rows\".\"id\" = \"#{join_cells_alias}\".\"repository_row_id\"" \ " AND \"#{join_cells_alias}\".\"repository_column_id\" = '#{filter_element.repository_column.id}'") .joins( - "INNER JOIN \"#{config[:table_name]}\" AS \"#{join_values_alias}\"" \ + "LEFT OUTER JOIN \"#{config[:table_name]}\" AS \"#{join_values_alias}\"" \ " ON \"#{join_values_alias}\".\"id\" = \"#{join_cells_alias}\".\"value_id\"" )