2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryRow < ApplicationRecord
|
2022-06-03 16:10:00 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2017-06-23 22:06:11 +08:00
|
|
|
include SearchableModel
|
2019-08-08 21:51:08 +08:00
|
|
|
include SearchableByNameModel
|
2020-06-10 00:43:22 +08:00
|
|
|
include ArchivableModel
|
2022-03-09 21:13:48 +08:00
|
|
|
include ReminderRepositoryCellJoinable
|
2017-06-23 22:06:11 +08:00
|
|
|
|
2021-07-19 20:23:36 +08:00
|
|
|
ID_PREFIX = 'IT'
|
|
|
|
include PrefixedIdModel
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
belongs_to :repository, class_name: 'RepositoryBase'
|
2023-09-14 20:22:01 +08:00
|
|
|
delegate :team, to: :repository
|
2021-07-19 20:23:36 +08:00
|
|
|
belongs_to :parent, class_name: 'RepositoryRow', optional: true
|
2023-09-25 17:24:50 +08:00
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :last_modified_by, class_name: 'User'
|
2020-06-08 22:22:37 +08:00
|
|
|
belongs_to :archived_by,
|
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :archived_repository_rows,
|
|
|
|
optional: true
|
2020-06-10 00:43:22 +08:00
|
|
|
belongs_to :restored_by,
|
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :restored_repository_rows,
|
|
|
|
optional: true
|
2021-07-07 23:43:51 +08:00
|
|
|
has_many :repository_cells, -> { order(:id) }, inverse_of: :repository_row, dependent: :destroy
|
|
|
|
|
|
|
|
{
|
|
|
|
repository_text: 'RepositoryTextValue',
|
|
|
|
repository_number: 'RepositoryNumberValue',
|
|
|
|
repository_list: 'RepositoryListValue',
|
|
|
|
repository_asset: 'RepositoryAssetValue',
|
|
|
|
repository_status: 'RepositoryStatusValue',
|
|
|
|
repository_checklist: 'RepositoryChecklistValue',
|
2022-03-14 18:56:38 +08:00
|
|
|
repository_stock: 'RepositoryStockValue',
|
2021-12-21 19:38:52 +08:00
|
|
|
repository_date_time: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_time: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_date: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_date_time_range: 'RepositoryDateTimeRangeValueBase',
|
|
|
|
repository_time_range: 'RepositoryDateTimeRangeValueBase',
|
|
|
|
repository_date_range: 'RepositoryDateTimeRangeValueBase'
|
2021-07-07 23:43:51 +08:00
|
|
|
}.each do |relation, class_name|
|
|
|
|
has_many "#{relation}_cells".to_sym, -> { where(value_type: class_name) }, class_name: 'RepositoryCell',
|
|
|
|
inverse_of: :repository_row
|
|
|
|
has_many "#{relation}_values".to_sym, class_name: class_name, through: "#{relation}_cells".to_sym,
|
|
|
|
source: :value, source_type: class_name
|
|
|
|
end
|
|
|
|
|
2022-02-11 17:46:19 +08:00
|
|
|
has_one :repository_stock_cell,
|
|
|
|
lambda {
|
|
|
|
joins(:repository_column)
|
|
|
|
.where(repository_columns: { data_type: 'RepositoryStockValue' })
|
|
|
|
.where(value_type: 'RepositoryStockValue')
|
|
|
|
},
|
|
|
|
class_name: 'RepositoryCell',
|
|
|
|
inverse_of: :repository_row
|
|
|
|
has_one :repository_stock_value, class_name: 'RepositoryStockValue',
|
|
|
|
through: :repository_stock_cell,
|
|
|
|
source: :value,
|
|
|
|
source_type: 'RepositoryStockValue'
|
|
|
|
|
|
|
|
# Only in snapshots
|
|
|
|
has_one :repository_stock_consumption_cell,
|
|
|
|
lambda {
|
|
|
|
joins(:repository_column)
|
|
|
|
.where(repository_columns: { data_type: 'RepositoryStockConsumptionValue' })
|
|
|
|
.where(value_type: 'RepositoryStockValue')
|
|
|
|
},
|
|
|
|
class_name: 'RepositoryCell',
|
|
|
|
inverse_of: :repository_row
|
|
|
|
has_one :repository_stock_consumption_value,
|
|
|
|
class_name: 'RepositoryStockConsumptionValue',
|
|
|
|
through: :repository_stock_consumption_cell,
|
|
|
|
source: :value,
|
|
|
|
source_type: 'RepositoryStockValue'
|
2022-01-13 00:13:50 +08:00
|
|
|
|
2024-02-05 20:36:12 +08:00
|
|
|
has_many :repository_columns, through: :repository_cells, dependent: :destroy
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :my_module_repository_rows,
|
2017-06-06 23:35:29 +08:00
|
|
|
inverse_of: :repository_row, dependent: :destroy
|
2024-02-05 20:36:12 +08:00
|
|
|
has_many :my_modules, through: :my_module_repository_rows, dependent: :destroy
|
2023-09-25 17:24:50 +08:00
|
|
|
has_many :child_connections,
|
|
|
|
class_name: 'RepositoryRowConnection',
|
|
|
|
foreign_key: :parent_id,
|
|
|
|
dependent: :destroy,
|
|
|
|
inverse_of: :parent
|
|
|
|
has_many :child_repository_rows,
|
|
|
|
through: :child_connections,
|
|
|
|
class_name: 'RepositoryRow',
|
2024-02-05 20:36:12 +08:00
|
|
|
source: :child,
|
|
|
|
dependent: :destroy
|
2023-09-25 17:24:50 +08:00
|
|
|
has_many :parent_connections,
|
|
|
|
class_name: 'RepositoryRowConnection',
|
|
|
|
foreign_key: :child_id,
|
|
|
|
dependent: :destroy,
|
|
|
|
inverse_of: :child
|
|
|
|
has_many :parent_repository_rows,
|
|
|
|
through: :parent_connections,
|
|
|
|
class_name: 'RepositoryRow',
|
2024-02-05 20:36:12 +08:00
|
|
|
source: :parent,
|
|
|
|
dependent: :destroy
|
2024-07-10 13:29:18 +08:00
|
|
|
has_many :storage_location_repository_rows, inverse_of: :repository_row, dependent: :destroy
|
|
|
|
has_many :storage_locations, through: :storage_location_repository_rows
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
validates :created_by, presence: true
|
2020-06-05 05:29:37 +08:00
|
|
|
|
2024-06-12 21:01:36 +08:00
|
|
|
attr_accessor :import_status, :import_message
|
2024-06-07 15:55:11 +08:00
|
|
|
|
2020-06-24 17:22:28 +08:00
|
|
|
scope :active, -> { where(archived: false) }
|
|
|
|
scope :archived, -> { where(archived: true) }
|
2018-04-26 20:13:46 +08:00
|
|
|
|
2022-03-11 19:38:45 +08:00
|
|
|
scope :with_active_reminders, lambda { |user|
|
2023-01-24 16:36:13 +08:00
|
|
|
reminder_repository_cells_scope(joins(:repository_cells), user)
|
2022-03-09 21:13:48 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 20:23:36 +08:00
|
|
|
def code
|
|
|
|
"#{ID_PREFIX}#{parent_id || id}"
|
|
|
|
end
|
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
|
|
|
where(repository: Repository.viewable_by_user(user, teams))
|
|
|
|
end
|
|
|
|
|
2024-04-03 06:55:09 +08:00
|
|
|
def self.search(user,
|
|
|
|
include_archived,
|
|
|
|
query = nil,
|
|
|
|
_current_team = nil,
|
|
|
|
options = {})
|
|
|
|
|
2024-04-18 21:45:34 +08:00
|
|
|
teams = options[:teams] || current_team || user.teams.select(:id)
|
2024-04-03 06:55:09 +08:00
|
|
|
searchable_row_fields = [RepositoryRow::PREFIXED_ID_SQL, 'repository_rows.name', 'users.full_name']
|
2024-04-18 21:45:34 +08:00
|
|
|
|
|
|
|
readable_rows = distinct.joins(:repository, :created_by)
|
|
|
|
.joins("INNER JOIN user_assignments repository_user_assignments " \
|
|
|
|
"ON repository_user_assignments.assignable_type = 'RepositoryBase' " \
|
|
|
|
"AND repository_user_assignments.assignable_id = repositories.id")
|
|
|
|
.where(repository_user_assignments: { user_id: user, team_id: teams })
|
|
|
|
|
|
|
|
readable_rows = readable_rows.active unless include_archived
|
|
|
|
repository_rows = readable_rows.where_attributes_like_boolean(searchable_row_fields, query, options)
|
|
|
|
|
|
|
|
Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |_data_type, config|
|
|
|
|
custom_cell_matches = readable_rows.joins(config[:includes])
|
|
|
|
.where_attributes_like_boolean(config[:field], query, options)
|
|
|
|
repository_rows = repository_rows.or(readable_rows.where(id: custom_cell_matches))
|
2024-04-03 06:55:09 +08:00
|
|
|
end
|
2024-04-18 21:45:34 +08:00
|
|
|
|
|
|
|
repository_rows
|
2024-04-03 06:55:09 +08:00
|
|
|
end
|
|
|
|
|
2022-07-07 18:00:35 +08:00
|
|
|
def self.filter_by_teams(teams = [])
|
|
|
|
return self if teams.blank?
|
|
|
|
|
|
|
|
joins(:repository).where(repository: { team: teams })
|
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def self.name_like(query)
|
|
|
|
where('repository_rows.name ILIKE ?', "%#{query}%")
|
|
|
|
end
|
2019-04-29 20:02:02 +08:00
|
|
|
|
|
|
|
def self.change_owner(team, user, new_owner)
|
|
|
|
joins(:repository)
|
|
|
|
.where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user)
|
|
|
|
.update_all(created_by_id: new_owner.id)
|
|
|
|
end
|
2020-01-25 16:43:05 +08:00
|
|
|
|
|
|
|
def editable?
|
|
|
|
true
|
|
|
|
end
|
2020-04-09 18:33:04 +08:00
|
|
|
|
2020-06-23 23:12:31 +08:00
|
|
|
def row_archived?
|
|
|
|
self[:archived]
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived
|
|
|
|
row_archived? || repository&.archived?
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived?
|
|
|
|
row_archived? ? super : repository.archived?
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_by
|
|
|
|
row_archived? ? super : repository.archived_by
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on
|
|
|
|
row_archived? ? super : repository.archived_on
|
|
|
|
end
|
|
|
|
|
2022-01-20 19:44:57 +08:00
|
|
|
def has_stock?
|
|
|
|
RepositoryStockValue.joins(repository_cell: :repository_row).exists?(repository_rows: { id: id })
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(repository_snapshot)
|
|
|
|
row_snapshot = dup
|
|
|
|
row_snapshot.assign_attributes(
|
|
|
|
repository: repository_snapshot,
|
|
|
|
parent_id: id,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
row_snapshot.save!
|
|
|
|
|
|
|
|
repository_cells.each { |cell| cell.snapshot!(row_snapshot) }
|
2022-01-26 00:48:45 +08:00
|
|
|
row_snapshot
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|
2022-06-03 16:10:00 +08:00
|
|
|
|
|
|
|
def row_consumption(stock_consumption)
|
|
|
|
if repository_stock_cell.present?
|
|
|
|
consumed_stock = number_with_precision(
|
|
|
|
stock_consumption || 0,
|
|
|
|
precision: (repository.repository_stock_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
|
|
|
"#{consumed_stock} #{repository_stock_value&.repository_stock_unit_item&.data}"
|
|
|
|
else
|
|
|
|
'-'
|
|
|
|
end
|
|
|
|
end
|
2023-09-26 14:43:20 +08:00
|
|
|
|
|
|
|
def relationship_count
|
|
|
|
parent_connections.size + child_connections.size
|
|
|
|
end
|
2024-01-11 00:23:15 +08:00
|
|
|
|
|
|
|
def archived_branch?
|
|
|
|
archived?
|
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|