mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-31 12:09:17 +08:00
Update scopes for repository items
This commit is contained in:
parent
6de322498b
commit
91af7135c1
2 changed files with 58 additions and 2 deletions
|
@ -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))
|
||||
|
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in a new issue