diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index 49032962b..fbc550d70 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -30,8 +30,14 @@ class RepositoryRow < ApplicationRecord length: { maximum: Constants::NAME_MAX_LENGTH } validates :created_by, presence: true - scope :active, -> { where(archived: false) } - scope :archived, -> { where(archived: true) } + scope :active, lambda { + joins(:repository) + .where('repositories.archived = FALSE AND repository_rows.archived = FALSE') + } + scope :archived, lambda { + joins(:repository) + .where('repositories.archived = TRUE OR repository_rows.archived = TRUE') + } def self.viewable_by_user(user, teams) where(repository: Repository.viewable_by_user(user, teams)) diff --git a/spec/models/repository_row_spec.rb b/spec/models/repository_row_spec.rb index a1e145eb0..15c1a931e 100644 --- a/spec/models/repository_row_spec.rb +++ b/spec/models/repository_row_spec.rb @@ -43,6 +43,56 @@ describe RepositoryRow, type: :model do end end + describe 'Scopes' do + describe '.archived' do + before do + create :repository_row, repository: repository + create :repository_row, repository: repository + create :repository_row, :archived, repository: repository + end + + context 'when repository is active' do + let(:repository) { create :repository } + + it 'includes only archived rows within active repository' do + expect(repository.repository_rows.archived.count).to be_eql(1) + end + end + + context 'when repository is archived' do + let(:repository) { create :repository, :archived } + + it 'includes all rows within archived repository' do + expect(repository.repository_rows.archived.count).to be_eql(3) + end + end + end + + describe '.active' do + before do + create :repository_row, repository: repository + create :repository_row, repository: repository + create :repository_row, :archived, repository: repository + end + + context 'when repository is active' do + let(:repository) { create :repository } + + it 'includes only active rows from active repository scope' do + expect(repository.repository_rows.active.count).to be_eql(2) + end + end + + context 'when repository is archived' do + let(:repository) { create :repository, :archived } + + it 'includes 0 rows from archived repository' do + expect(repository.repository_rows.active.count).to be_eql(0) + end + end + end + end + describe '.archived' do context 'when archived' do let(:repository_row) { build :repository_row, :archived }