2023-09-25 17:24:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryRowConnection < ApplicationRecord
|
|
|
|
belongs_to :parent,
|
|
|
|
class_name: 'RepositoryRow',
|
|
|
|
inverse_of: :child_connections,
|
|
|
|
counter_cache: :child_connections_count
|
|
|
|
belongs_to :child,
|
|
|
|
class_name: 'RepositoryRow',
|
|
|
|
inverse_of: :parent_connections,
|
|
|
|
counter_cache: :parent_connections_count
|
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :last_modified_by, class_name: 'User'
|
|
|
|
|
|
|
|
validates :parent_id, uniqueness: { scope: :child_id }
|
2023-12-20 21:18:49 +08:00
|
|
|
validate :prevent_self_connections, :prevent_reciprocal_connections
|
|
|
|
|
2024-01-03 18:32:46 +08:00
|
|
|
def parent?(repository_row)
|
|
|
|
parent_id == repository_row.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def child?(repository_row)
|
|
|
|
child_id == repository_row.id
|
|
|
|
end
|
|
|
|
|
2023-12-20 21:18:49 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def prevent_self_connections
|
2024-01-15 18:08:08 +08:00
|
|
|
if parent_id == child_id
|
|
|
|
errors.add(:base, I18n.t('activerecord.errors.models.repository_row_connection.self_connection'))
|
|
|
|
end
|
2023-12-20 21:18:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def prevent_reciprocal_connections
|
|
|
|
if parent_id && child_id && RepositoryRowConnection.exists?(parent_id: child_id, child_id: parent_id)
|
2024-01-15 18:08:08 +08:00
|
|
|
errors.add(:base, I18n.t('activerecord.errors.models.repository_row_connection.reciprocal_connection'))
|
2023-12-20 21:18:49 +08:00
|
|
|
end
|
|
|
|
end
|
2023-12-21 19:33:06 +08:00
|
|
|
|
|
|
|
def relationship_type(repository_row)
|
|
|
|
return :parent if parent?(repository_row)
|
|
|
|
|
|
|
|
return :child if child?(repository_row)
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2023-09-25 17:24:50 +08:00
|
|
|
end
|