2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryColumn < ApplicationRecord
|
2020-04-09 18:33:04 +08:00
|
|
|
belongs_to :repository, class_name: 'RepositoryBase'
|
2022-01-26 00:48:45 +08:00
|
|
|
belongs_to :repository_snapshot, foreign_key: :repository_id, optional: true
|
2019-05-07 19:47:50 +08:00
|
|
|
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
|
2017-05-16 21:27:36 +08:00
|
|
|
has_many :repository_cells, dependent: :destroy
|
|
|
|
has_many :repository_rows, through: :repository_cells
|
2020-02-05 18:58:27 +08:00
|
|
|
has_many :repository_list_items, -> { order('data ASC') }, dependent: :destroy,
|
|
|
|
index_errors: true,
|
|
|
|
inverse_of: :repository_column
|
|
|
|
has_many :repository_status_items, -> { order('status ASC') }, dependent: :destroy,
|
|
|
|
index_errors: true,
|
|
|
|
inverse_of: :repository_column
|
|
|
|
has_many :repository_checklist_items, -> { order('data ASC') }, dependent: :destroy,
|
|
|
|
index_errors: true,
|
|
|
|
inverse_of: :repository_column
|
2022-01-18 20:17:05 +08:00
|
|
|
has_many :repository_stock_unit_items, -> { order('data ASC') }, dependent: :destroy,
|
|
|
|
index_errors: true,
|
|
|
|
inverse_of: :repository_column
|
2021-12-02 21:18:13 +08:00
|
|
|
has_many :repository_table_filter_elements, dependent: :destroy
|
2019-10-09 21:18:51 +08:00
|
|
|
|
2019-10-11 15:41:22 +08:00
|
|
|
accepts_nested_attributes_for :repository_status_items, allow_destroy: true
|
2019-10-16 19:26:53 +08:00
|
|
|
accepts_nested_attributes_for :repository_list_items, allow_destroy: true
|
2019-12-09 20:38:40 +08:00
|
|
|
accepts_nested_attributes_for :repository_checklist_items, allow_destroy: true
|
2022-01-18 20:17:05 +08:00
|
|
|
accepts_nested_attributes_for :repository_stock_unit_items, allow_destroy: true
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
enum data_type: Extends::REPOSITORY_DATA_TYPES
|
|
|
|
|
2023-01-31 00:07:27 +08:00
|
|
|
store_accessor :metadata, %i(reminder_value reminder_unit reminder_message)
|
2022-03-09 21:13:48 +08:00
|
|
|
|
2022-01-26 00:48:45 +08:00
|
|
|
validates :data_type, uniqueness: { if: :repository_stock_value?, scope: :repository_id }
|
|
|
|
validates :data_type, uniqueness: { if: :repository_stock_consumption_value?, scope: :repository_id }
|
2022-01-18 20:17:05 +08:00
|
|
|
|
2017-05-16 21:27:36 +08:00
|
|
|
validates :name,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH },
|
2019-05-07 19:47:50 +08:00
|
|
|
uniqueness: { scope: :repository_id, case_sensitive: true }
|
2019-11-19 19:10:38 +08:00
|
|
|
validates :name, :data_type, :repository, :created_by, presence: true
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
after_create :update_repository_table_states_with_new_column
|
2022-03-23 16:51:42 +08:00
|
|
|
after_update :clear_hidden_repository_cell_reminders
|
2022-04-08 20:09:00 +08:00
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
around_destroy :update_repository_table_states_with_removed_column
|
2022-04-13 20:20:23 +08:00
|
|
|
before_destroy :nulify_stock_consumption
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2018-03-15 22:43:16 +08:00
|
|
|
scope :list_type, -> { where(data_type: 'RepositoryListValue') }
|
2018-05-16 15:31:19 +08:00
|
|
|
scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') }
|
2019-10-21 17:53:18 +08:00
|
|
|
scope :status_type, -> { where(data_type: 'RepositoryStatusValue') }
|
2019-12-09 20:38:40 +08:00
|
|
|
scope :checkbox_type, -> { where(data_type: 'RepositoryChecklistValue') }
|
2022-01-25 19:12:55 +08:00
|
|
|
scope :stock_type, -> { where(data_type: 'RepositoryStockValue') }
|
2022-01-26 00:48:45 +08:00
|
|
|
scope :stock_consumption_type, -> { where(data_type: 'RepositoryStockConsumptionValue') }
|
2018-05-16 15:31:19 +08:00
|
|
|
|
|
|
|
def self.name_like(query)
|
|
|
|
where('repository_columns.name ILIKE ?', "%#{query}%")
|
|
|
|
end
|
2018-03-15 22:43:16 +08:00
|
|
|
|
2019-11-19 19:10:38 +08:00
|
|
|
# Add enum check method with underscores (eg repository_list_value)
|
|
|
|
data_types.each do |k, _|
|
|
|
|
define_method "#{k.underscore}?" do
|
|
|
|
public_send "#{k}?"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
def update_repository_table_states_with_new_column
|
2018-04-18 21:53:48 +08:00
|
|
|
service = RepositoryTableStateColumnUpdateService.new
|
2018-04-20 17:44:43 +08:00
|
|
|
service.update_states_with_new_column(repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_repository_table_states_with_removed_column
|
|
|
|
# Calculate old_column_index - this can only be done before
|
|
|
|
# record is deleted when we still have its index
|
|
|
|
old_column_index = (
|
2021-08-13 20:07:01 +08:00
|
|
|
repository.default_columns_count +
|
2018-04-20 17:44:43 +08:00
|
|
|
repository.repository_columns
|
|
|
|
.order(id: :asc)
|
|
|
|
.pluck(:id)
|
|
|
|
.index(id)
|
2019-11-19 17:33:51 +08:00
|
|
|
)
|
2018-04-20 17:44:43 +08:00
|
|
|
|
|
|
|
# Perform the destroy itself
|
|
|
|
yield
|
|
|
|
|
|
|
|
# Update repository table states
|
|
|
|
service = RepositoryTableStateColumnUpdateService.new
|
|
|
|
service.update_states_with_removed_column(
|
|
|
|
repository, old_column_index
|
|
|
|
)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
2018-03-13 18:36:05 +08:00
|
|
|
|
|
|
|
def importable?
|
2022-05-24 17:49:36 +08:00
|
|
|
if data_type == 'RepositoryStockValue'
|
|
|
|
RepositoryBase.stock_management_enabled?
|
|
|
|
else
|
|
|
|
Extends::REPOSITORY_IMPORTABLE_TYPES.include?(data_type.to_sym)
|
|
|
|
end
|
2018-03-13 18:36:05 +08:00
|
|
|
end
|
2020-04-07 23:59:46 +08:00
|
|
|
|
|
|
|
def deep_dup
|
|
|
|
new_column = super
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
extra_method_name = "#{data_type.underscore}_deep_dup"
|
|
|
|
__send__(extra_method_name, new_column) if respond_to?(extra_method_name, true)
|
2020-04-07 23:59:46 +08:00
|
|
|
|
|
|
|
new_column
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(repository_snapshot)
|
|
|
|
column_snapshot = deep_dup
|
|
|
|
column_snapshot.assign_attributes(
|
|
|
|
repository: repository_snapshot,
|
|
|
|
parent_id: id,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
column_snapshot.save!
|
2022-01-26 00:48:45 +08:00
|
|
|
snapshot_stock_consumption!(repository_snapshot) if repository_stock_value?
|
|
|
|
column_snapshot
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
|
|
|
|
2020-06-01 23:19:33 +08:00
|
|
|
def delimiter_char
|
|
|
|
Constants::REPOSITORY_LIST_ITEMS_DELIMITERS_MAP[metadata['delimiter']&.to_sym] || "\n"
|
|
|
|
end
|
|
|
|
|
2021-11-23 05:48:36 +08:00
|
|
|
def items
|
|
|
|
items_method_name = "#{data_type.chomp('Value').underscore}_items"
|
2022-03-23 17:05:07 +08:00
|
|
|
items_method_name = 'repository_stock_unit_items' if data_type == 'RepositoryStockValue'
|
2021-11-23 05:48:36 +08:00
|
|
|
__send__(items_method_name) if respond_to?(items_method_name, true)
|
|
|
|
end
|
|
|
|
|
2020-04-07 23:59:46 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def repository_list_value_deep_dup(new_column)
|
2020-04-09 18:33:04 +08:00
|
|
|
repository_list_items.each do |item|
|
2020-04-21 20:49:36 +08:00
|
|
|
new_column.repository_list_items << item.deep_dup
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
2020-04-07 23:59:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_checklist_value_deep_dup(new_column)
|
2020-04-09 18:33:04 +08:00
|
|
|
repository_checklist_items.each do |item|
|
2020-04-21 20:49:36 +08:00
|
|
|
new_column.repository_checklist_items << item.deep_dup
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
2020-04-07 23:59:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_status_value_deep_dup(new_column)
|
2020-04-09 18:33:04 +08:00
|
|
|
repository_status_items.each do |item|
|
2020-04-21 20:49:36 +08:00
|
|
|
new_column.repository_status_items << item.deep_dup
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
2020-04-07 23:59:46 +08:00
|
|
|
end
|
2022-01-18 20:17:05 +08:00
|
|
|
|
2022-01-26 00:48:45 +08:00
|
|
|
def repository_stock_value_deep_dup(new_column)
|
2022-01-18 20:17:05 +08:00
|
|
|
repository_stock_unit_items.each do |item|
|
|
|
|
new_column.repository_stock_unit_items << item.deep_dup
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 00:48:45 +08:00
|
|
|
|
|
|
|
def snapshot_stock_consumption!(repository_snapshot)
|
|
|
|
column_snapshot = deep_dup
|
|
|
|
column_snapshot.assign_attributes(
|
|
|
|
name: I18n.t('repositories.table.row_consumption'),
|
|
|
|
repository_snapshot: repository_snapshot,
|
|
|
|
data_type: 'RepositoryStockConsumptionValue',
|
|
|
|
parent_id: nil
|
|
|
|
)
|
|
|
|
column_snapshot.save!
|
|
|
|
end
|
2022-03-23 16:51:42 +08:00
|
|
|
|
|
|
|
def clear_hidden_repository_cell_reminders
|
2023-01-31 00:07:27 +08:00
|
|
|
return unless reminder_value_changed? || reminder_unit_changed?
|
2022-03-23 16:51:42 +08:00
|
|
|
|
|
|
|
HiddenRepositoryCellReminder.joins(repository_cell: :repository_column)
|
|
|
|
.where(repository_columns: { id: id })
|
|
|
|
.delete_all
|
|
|
|
end
|
2022-04-08 20:09:00 +08:00
|
|
|
|
2022-04-13 20:20:23 +08:00
|
|
|
def nulify_stock_consumption
|
|
|
|
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
|
2022-04-08 20:09:00 +08:00
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|