Add aibility to delete stock column [SCI-6723] (#4017)

Co-authored-by: Anton <anton@scinote.net>
This commit is contained in:
aignatov-bio 2022-04-13 14:20:23 +02:00 committed by GitHub
parent 36639727a1
commit 5ac9f51220
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 17 deletions

View file

@ -34,8 +34,6 @@ class RepositoryCell < ApplicationRecord
has_many :hidden_repository_cell_reminders, dependent: :destroy has_many :hidden_repository_cell_reminders, dependent: :destroy
before_destroy :prevent_stock_cell_destroy
validates :repository_column, validates :repository_column,
inclusion: { in: (lambda do |repository_cell| inclusion: { in: (lambda do |repository_cell|
repository_cell.repository_row&.repository&.repository_columns || [] repository_cell.repository_row&.repository&.repository_columns || []
@ -92,8 +90,4 @@ class RepositoryCell < ApplicationRecord
errors.add(:value_type, 'must match column data type') errors.add(:value_type, 'must match column data type')
end end
end end
def prevent_stock_cell_destroy
raise NotImplementedError if value.class.name == 'RepositoryStockValue'
end
end end

View file

@ -39,8 +39,8 @@ class RepositoryColumn < ApplicationRecord
after_create :update_repository_table_states_with_new_column after_create :update_repository_table_states_with_new_column
after_update :clear_hidden_repository_cell_reminders after_update :clear_hidden_repository_cell_reminders
before_destroy :prevent_stock_column_destroy
around_destroy :update_repository_table_states_with_removed_column around_destroy :update_repository_table_states_with_removed_column
before_destroy :nulify_stock_consumption
scope :list_type, -> { where(data_type: 'RepositoryListValue') } scope :list_type, -> { where(data_type: 'RepositoryListValue') }
scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') } scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') }
@ -116,11 +116,6 @@ class RepositoryColumn < ApplicationRecord
Constants::REPOSITORY_LIST_ITEMS_DELIMITERS_MAP[metadata['delimiter']&.to_sym] || "\n" Constants::REPOSITORY_LIST_ITEMS_DELIMITERS_MAP[metadata['delimiter']&.to_sym] || "\n"
end end
def deletable?
data_type != 'RepositoryStockValue'
end
def items def items
items_method_name = "#{data_type.chomp('Value').underscore}_items" items_method_name = "#{data_type.chomp('Value').underscore}_items"
items_method_name = 'repository_stock_unit_items' if data_type == 'RepositoryStockValue' items_method_name = 'repository_stock_unit_items' if data_type == 'RepositoryStockValue'
@ -172,7 +167,11 @@ class RepositoryColumn < ApplicationRecord
.delete_all .delete_all
end end
def prevent_stock_column_destroy def nulify_stock_consumption
raise NotImplementedError unless deletable? if data_type == 'RepositoryStockValue'
MyModuleRepositoryRow.where(repository_row_id: repository.repository_rows.select(:id))
.where.not(stock_consumption: nil)
.update_all(stock_consumption: nil)
end
end end
end end

View file

@ -48,9 +48,7 @@
id="<%= column.id %>" id="<%= column.id %>"
data-type="<%= column.data_type %>" data-type="<%= column.data_type %>"
data-edit-column-url="<%= edit_repository_repository_column_path(repository, column) %>" data-edit-column-url="<%= edit_repository_repository_column_path(repository, column) %>"
<% if column.deletable? %> data-destroy-column-url="<%= repository_columns_destroy_html_path(repository, column) %>"
data-destroy-column-url="<%= repository_columns_destroy_html_path(repository, column) %>"
<% end %>
data-editable-row="<%= can_manage_repository_column?(column) %>" data-editable-row="<%= can_manage_repository_column?(column) %>"
<% column.metadata.each do |k, v| %> <% column.metadata.each do |k, v| %>
<%= "data-metadata-#{k}=#{v}" %> <%= "data-metadata-#{k}=#{v}" %>