2022-01-13 00:13:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryLedgerRecord < ApplicationRecord
|
2023-08-22 16:47:58 +08:00
|
|
|
auto_strip_attributes :comment
|
|
|
|
|
2023-09-07 19:12:27 +08:00
|
|
|
belongs_to :repository_stock_value
|
2022-01-20 19:44:57 +08:00
|
|
|
belongs_to :reference, polymorphic: true
|
|
|
|
belongs_to :user
|
2023-09-08 17:35:16 +08:00
|
|
|
belongs_to :repository,
|
|
|
|
(lambda do |repository_ledger_record|
|
|
|
|
repository_ledger_record.reference_type == 'RepositoryBase' ? self : none
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :reference_id, inverse_of: :repository_ledger_records
|
|
|
|
belongs_to :my_module_repository_row,
|
|
|
|
(lambda do |repository_ledger_record|
|
|
|
|
repository_ledger_record.reference_type == 'MyModuleRepositoryRow' ? self : none
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :reference_id, inverse_of: :repository_ledger_records
|
|
|
|
has_one :repository_row, through: :repository_stock_value
|
2023-11-20 20:24:31 +08:00
|
|
|
|
|
|
|
validate :my_module_references_present?, if: -> { reference.is_a?(MyModuleRepositoryRow) }
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def my_module_references_present?
|
|
|
|
return if my_module_references.present? &&
|
|
|
|
my_module_references['my_module_id'].is_a?(Integer) &&
|
|
|
|
my_module_references['experiment_id'].is_a?(Integer) &&
|
|
|
|
my_module_references['project_id'].is_a?(Integer) &&
|
|
|
|
my_module_references['team_id'].is_a?(Integer)
|
|
|
|
|
|
|
|
errors.add(:base, I18n.t('repository_ledger_records.errors.my_module_references_missing'))
|
|
|
|
end
|
2022-01-13 00:13:50 +08:00
|
|
|
end
|