2020-04-09 18:33:04 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryBase < ApplicationRecord
|
|
|
|
include Discard::Model
|
|
|
|
|
|
|
|
self.table_name = 'repositories'
|
|
|
|
|
|
|
|
attribute :discarded_by_id, :integer
|
|
|
|
|
|
|
|
belongs_to :team
|
|
|
|
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
|
|
|
|
has_many :repository_columns, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
|
2022-01-25 19:12:55 +08:00
|
|
|
has_one :repository_stock_column,
|
|
|
|
-> { where(data_type: 'RepositoryStockValue') },
|
|
|
|
class_name: 'RepositoryColumn',
|
|
|
|
foreign_key: :repository_id,
|
|
|
|
inverse_of: :repository,
|
|
|
|
dependent: :destroy
|
2020-04-09 18:33:04 +08:00
|
|
|
has_many :repository_rows, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
|
|
|
|
has_many :repository_table_states, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
|
2020-05-22 01:25:28 +08:00
|
|
|
has_many :report_elements, inverse_of: :repository, dependent: :destroy, foreign_key: :repository_id
|
2020-04-09 18:33:04 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :team, presence: true
|
|
|
|
validates :created_by, presence: true
|
|
|
|
|
|
|
|
# Not discarded
|
|
|
|
default_scope -> { kept }
|
|
|
|
|
2022-02-11 17:46:19 +08:00
|
|
|
def self.stock_management_enabled?
|
2022-04-01 19:54:08 +08:00
|
|
|
ApplicationSettings.instance.values['stock_management_enabled']
|
2022-02-11 17:46:19 +08:00
|
|
|
end
|
|
|
|
|
2022-04-15 16:55:14 +08:00
|
|
|
def self.reminders_enabled?
|
|
|
|
RepositoryBase.stock_management_enabled?
|
|
|
|
end
|
|
|
|
|
2022-02-11 17:46:19 +08:00
|
|
|
def has_stock_management?
|
|
|
|
self.class.stock_management_enabled? && repository_columns.stock_type.exists?
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def cell_preload_includes
|
|
|
|
cell_includes = []
|
|
|
|
repository_columns.pluck(:data_type).each do |data_type|
|
2021-07-07 23:43:51 +08:00
|
|
|
value_class = data_type.constantize
|
|
|
|
next unless value_class.const_defined?('EXTRA_PRELOAD_INCLUDE')
|
|
|
|
|
|
|
|
cell_includes << value_class::EXTRA_PRELOAD_INCLUDE
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
|
|
|
cell_includes
|
|
|
|
end
|
|
|
|
|
2021-08-13 20:07:01 +08:00
|
|
|
def default_table_state
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_table_columns
|
|
|
|
default_table_state['columns'].to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_columns_count
|
|
|
|
default_table_state['columns'].length
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_table_order_as_js_array
|
|
|
|
default_table_state['order'].to_json
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def destroy_discarded(discarded_by_id = nil)
|
|
|
|
self.discarded_by_id = discarded_by_id
|
|
|
|
destroy
|
|
|
|
end
|
|
|
|
handle_asynchronously :destroy_discarded, queue: :clear_discarded_repository, priority: 20
|
|
|
|
end
|