mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 23:22:53 +00:00
Adds types
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
# typed: true
|
||||
|
||||
class Collection < ApplicationRecord
|
||||
extend T::Sig
|
||||
# Associations
|
||||
belongs_to :user
|
||||
belongs_to :parent_collection, class_name: "Collection", optional: true
|
||||
@@ -15,30 +18,36 @@ class Collection < ApplicationRecord
|
||||
scope :root_collections, -> { where(parent_collection_id: nil) }
|
||||
|
||||
# Instance methods
|
||||
sig { returns(Integer) }
|
||||
def game_count
|
||||
games.count
|
||||
end
|
||||
|
||||
sig { returns(Integer) }
|
||||
def total_game_count
|
||||
game_count + subcollections.sum(&:total_game_count)
|
||||
end
|
||||
|
||||
sig { returns(T::Boolean) }
|
||||
def root?
|
||||
parent_collection_id.nil?
|
||||
end
|
||||
|
||||
sig { returns(T::Boolean) }
|
||||
def subcollection?
|
||||
parent_collection_id.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
sig { void }
|
||||
def cannot_be_own_parent
|
||||
if parent_collection_id.present? && parent_collection_id == id
|
||||
errors.add(:parent_collection_id, "cannot be itself")
|
||||
end
|
||||
end
|
||||
|
||||
sig { void }
|
||||
def subcollection_depth_limit
|
||||
if parent_collection.present? && parent_collection.parent_collection.present?
|
||||
errors.add(:parent_collection_id, "cannot nest more than one level deep")
|
||||
|
||||
Reference in New Issue
Block a user