mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-17 06:35:14 +08:00
f892467d67
* Fix relationship css issues on Item card [SCI-9911] * Fix failing specs [SCI-9911]
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
# 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 }
|
|
validate :prevent_self_connections, :prevent_reciprocal_connections
|
|
|
|
def parent?(repository_row)
|
|
parent_id == repository_row.id
|
|
end
|
|
|
|
def child?(repository_row)
|
|
child_id == repository_row.id
|
|
end
|
|
|
|
private
|
|
|
|
def prevent_self_connections
|
|
errors.add(:base, 'A repository_row cannot have a connection with itself') if parent_id == child_id
|
|
end
|
|
|
|
def prevent_reciprocal_connections
|
|
if parent_id && child_id && RepositoryRowConnection.exists?(parent_id: child_id, child_id: parent_id)
|
|
errors.add(:base, 'Reciprocal connections are not allowed')
|
|
end
|
|
end
|
|
|
|
def relationship_type(repository_row)
|
|
return :parent if parent?(repository_row)
|
|
|
|
return :child if child?(repository_row)
|
|
|
|
nil
|
|
end
|
|
end
|