From 9f3b77ed15a27530a394a4f08d7cdc19b5967207 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 26 Jan 2022 10:12:46 +0100 Subject: [PATCH] Finalyze repository filter elements management backend [SCI-6396] --- .../repository_table_filters_controller.rb | 4 +--- app/models/repository_checklist_item.rb | 9 +++++++++ app/models/repository_list_item.rb | 9 +++++++++ app/models/repository_status_item.rb | 11 +++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/repository_table_filters_controller.rb b/app/controllers/repository_table_filters_controller.rb index 75d9c302e..394a15c17 100644 --- a/app/controllers/repository_table_filters_controller.rb +++ b/app/controllers/repository_table_filters_controller.rb @@ -10,9 +10,7 @@ class RepositoryTableFiltersController < ApplicationController before_action :check_manage_permissions, only: %i(update destroy) def index - - - render json: @repository.repository_table_filters, each_serializer: RepositoryFilterSerializer + render json: @repository.repository_table_filters.order(:name), each_serializer: RepositoryFilterSerializer end def show diff --git a/app/models/repository_checklist_item.rb b/app/models/repository_checklist_item.rb index 457d518c4..7809af432 100644 --- a/app/models/repository_checklist_item.rb +++ b/app/models/repository_checklist_item.rb @@ -14,6 +14,8 @@ class RepositoryChecklistItem < ApplicationRecord uniqueness: { scope: :repository_column_id }, length: { maximum: Constants::NAME_MAX_LENGTH } + before_destroy :update_table_fiter_elements + private def validate_per_column_limit @@ -22,4 +24,11 @@ class RepositoryChecklistItem < ApplicationRecord errors.add(:base, :per_column_limit) end end + + def update_table_fiter_elements + repository_column.repository_table_filter_elements.find_each do |filter_element| + filter_element.parameters['item_ids']&.delete(id) + filter_element.parameters['item_ids'].blank? ? filter_element.destroy! : filter_element.save! + end + end end diff --git a/app/models/repository_list_item.rb b/app/models/repository_list_item.rb index 9d90c8ed0..7de9486d7 100644 --- a/app/models/repository_list_item.rb +++ b/app/models/repository_list_item.rb @@ -11,6 +11,8 @@ class RepositoryListItem < ApplicationRecord uniqueness: { scope: :repository_column_id }, length: { maximum: Constants::TEXT_MAX_LENGTH } + before_destroy :update_table_fiter_elements + private def validate_per_column_limit @@ -19,4 +21,11 @@ class RepositoryListItem < ApplicationRecord errors.add(:base, :per_column_limit) end end + + def update_table_fiter_elements + repository_column.repository_table_filter_elements.find_each do |filter_element| + filter_element.parameters['item_ids']&.delete(id) + filter_element.parameters['item_ids'].blank? ? filter_element.destroy! : filter_element.save! + end + end end diff --git a/app/models/repository_status_item.rb b/app/models/repository_status_item.rb index 546f42841..46d04371e 100644 --- a/app/models/repository_status_item.rb +++ b/app/models/repository_status_item.rb @@ -11,7 +11,18 @@ class RepositoryStatusItem < ApplicationRecord inverse_of: :modified_repository_status_types has_many :repository_status_values, inverse_of: :repository_status_item, dependent: :destroy + before_destroy :update_table_fiter_elements + def data "#{icon} #{status}" end + + private + + def update_table_fiter_elements + repository_column.repository_table_filter_elements.find_each do |filter_element| + filter_element.parameters['status_ids']&.delete(id) + filter_element.parameters['status_ids'].blank? ? filter_element.destroy! : filter_element.save! + end + end end