Add default scope for non archived repositories

This commit is contained in:
Urban Rotnik 2020-06-16 08:47:48 +02:00
parent 0a7c9aeb1a
commit 83bd8c3ad4
7 changed files with 73 additions and 6 deletions

View file

@ -345,7 +345,7 @@ class RepositoriesController < ApplicationController
def load_repository def load_repository
repository_id = params[:id] || params[:repository_id] repository_id = params[:id] || params[:repository_id]
@repository = Repository.accessible_by_teams(current_team).find_by(id: repository_id) @repository = Repository.accessible_by_teams(current_team).with_archived.find_by(id: repository_id)
render_404 unless @repository render_404 unless @repository
end end
@ -354,7 +354,7 @@ class RepositoriesController < ApplicationController
@repositories = if params[:archived] @repositories = if params[:archived]
@repositories.archived @repositories.archived
else else
@repositories.active @repositories
end end
end end

View file

@ -139,7 +139,7 @@ class RepositoryRowsController < ApplicationController
end end
def available_rows def available_rows
if @repository.repository_rows.empty? if @repository.repository_rows.active.empty?
no_items_string = no_items_string =
"#{t('projects.reports.new.save_PDF_to_inventory_modal.no_items')} " \ "#{t('projects.reports.new.save_PDF_to_inventory_modal.no_items')} " \
"#{link_to(t('projects.reports.new.save_PDF_to_inventory_modal.here'), "#{link_to(t('projects.reports.new.save_PDF_to_inventory_modal.here'),
@ -204,6 +204,7 @@ class RepositoryRowsController < ApplicationController
def load_repository def load_repository
@repository = Repository.accessible_by_teams(current_team) @repository = Repository.accessible_by_teams(current_team)
.with_archived
.eager_load(:repository_columns) .eager_load(:repository_columns)
.find_by(id: params[:repository_id]) .find_by(id: params[:repository_id])
render_404 unless @repository render_404 unless @repository
@ -258,6 +259,7 @@ class RepositoryRowsController < ApplicationController
def load_available_rows(query) def load_available_rows(query)
@repository.repository_rows @repository.repository_rows
.active
.includes(:repository_cells) .includes(:repository_cells)
.name_like(search_params[:q]) .name_like(search_params[:q])
.limit(Constants::SEARCH_LIMIT) .limit(Constants::SEARCH_LIMIT)

View file

@ -48,7 +48,8 @@ class Activity < ApplicationRecord
} }
scope :repositories_joins, lambda { scope :repositories_joins, lambda {
joins("LEFT JOIN repositories ON subject_type = 'RepositoryBase' AND subject_id = repositories.id") joins("LEFT JOIN repositories ON subject_type = 'RepositoryBase' AND subject_id = repositories.id " \
"AND repositories.archived != TRUE")
} }
scope :reports_joins, lambda { scope :reports_joins, lambda {

View file

@ -34,6 +34,9 @@ class Repository < RepositoryBase
.distinct .distinct
} }
default_scope { where.not(archived: true) }
scope :with_archived, -> { unscope(where: :archived) }
scope :archived, -> { with_archived.where(archived: true) }
scope :used_on_task_but_unshared, lambda { |task, team| scope :used_on_task_but_unshared, lambda { |task, team|
where(id: task.repository_rows where(id: task.repository_rows
.select(:repository_id)) .select(:repository_id))
@ -43,13 +46,13 @@ class Repository < RepositoryBase
def self.within_global_limits? def self.within_global_limits?
return true unless Rails.configuration.x.global_repositories_limit.positive? return true unless Rails.configuration.x.global_repositories_limit.positive?
count < Rails.configuration.x.global_repositories_limit with_archived.count < Rails.configuration.x.global_repositories_limit
end end
def self.within_team_limits?(team) def self.within_team_limits?(team)
return true unless Rails.configuration.x.team_repositories_limit.positive? return true unless Rails.configuration.x.team_repositories_limit.positive?
team.repositories.count < Rails.configuration.x.team_repositories_limit team.repositories.with_archived.count < Rails.configuration.x.team_repositories_limit
end end
def self.search( def self.search(

View file

@ -100,6 +100,7 @@ class SmartAnnotation
def repository_rows(repository) def repository_rows(repository)
res = RepositoryRow res = RepositoryRow
.active
.where(repository: repository) .where(repository: repository)
.where_attributes_like('name', @query, at_search: true) .where_attributes_like('name', @query, at_search: true)
.limit(Constants::ATWHO_SEARCH_LIMIT) .limit(Constants::ATWHO_SEARCH_LIMIT)

View file

@ -11,5 +11,10 @@ FactoryBot.define do
trait :read_shared do trait :read_shared do
permission_level { :shared_read } permission_level { :shared_read }
end end
trait :archived do
archived { true }
archived_on { Time.zone.now }
archived_by { created_by }
end
end end
end end

View file

@ -46,6 +46,23 @@ describe Repository, type: :model do
end end
end end
describe 'Scopes' do
describe 'default_scope' do
before do
create :repository
create :repository, :archived
end
it 'returns only active rows' do
expect(Repository.count).to be_eql 1
end
it 'returns all rows' do
expect(Repository.with_archived.count).to be_eql 2
end
end
end
describe '.copy' do describe '.copy' do
let(:created_by) { create :user } let(:created_by) { create :user }
let(:repository) { create :repository } let(:repository) { create :repository }
@ -62,4 +79,42 @@ describe Repository, type: :model do
.to(change { Activity.count }) .to(change { Activity.count })
end end
end end
describe '.within_global_limits?' do
context 'when have an archived repository' do
before do
Rails.configuration.x.global_repositories_limit = 2
create :repository
create :repository, :archived
end
after do
Rails.configuration.x.global_repositories_limit = 0
end
it 'includes archived repositories in condition and returns false' do
expect(described_class.within_global_limits?).to be_falsey
end
end
end
describe '.within_team_limits?' do
context 'when have an archived repository' do
before do
Rails.configuration.x.team_repositories_limit = 2
create :repository, team: team
create :repository, :archived, team: team
end
after do
Rails.configuration.x.team_repositories_limit = 0
end
let(:team) { create :team }
it 'includes archived repositories in condition and returns false' do
expect(described_class.within_team_limits?(team)).to be_falsey
end
end
end
end end