From 83bd8c3ad4f246b3c92f72da88309c5114bba8ba Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 16 Jun 2020 08:47:48 +0200 Subject: [PATCH] Add default scope for non archived repositories --- app/controllers/repositories_controller.rb | 4 +- app/controllers/repository_rows_controller.rb | 4 +- app/models/activity.rb | 3 +- app/models/repository.rb | 7 ++- app/utilities/smart_annotation.rb | 1 + spec/factories/repositories.rb | 5 ++ spec/models/repository_spec.rb | 55 +++++++++++++++++++ 7 files changed, 73 insertions(+), 6 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index f9704fd2e..b4278bea2 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -345,7 +345,7 @@ class RepositoriesController < ApplicationController def load_repository 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 end @@ -354,7 +354,7 @@ class RepositoriesController < ApplicationController @repositories = if params[:archived] @repositories.archived else - @repositories.active + @repositories end end diff --git a/app/controllers/repository_rows_controller.rb b/app/controllers/repository_rows_controller.rb index 72869b489..493f8d062 100644 --- a/app/controllers/repository_rows_controller.rb +++ b/app/controllers/repository_rows_controller.rb @@ -139,7 +139,7 @@ class RepositoryRowsController < ApplicationController end def available_rows - if @repository.repository_rows.empty? + if @repository.repository_rows.active.empty? no_items_string = "#{t('projects.reports.new.save_PDF_to_inventory_modal.no_items')} " \ "#{link_to(t('projects.reports.new.save_PDF_to_inventory_modal.here'), @@ -204,6 +204,7 @@ class RepositoryRowsController < ApplicationController def load_repository @repository = Repository.accessible_by_teams(current_team) + .with_archived .eager_load(:repository_columns) .find_by(id: params[:repository_id]) render_404 unless @repository @@ -258,6 +259,7 @@ class RepositoryRowsController < ApplicationController def load_available_rows(query) @repository.repository_rows + .active .includes(:repository_cells) .name_like(search_params[:q]) .limit(Constants::SEARCH_LIMIT) diff --git a/app/models/activity.rb b/app/models/activity.rb index 09538c687..fa7951f62 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -48,7 +48,8 @@ class Activity < ApplicationRecord } 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 { diff --git a/app/models/repository.rb b/app/models/repository.rb index baf66c593..030f57b82 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -34,6 +34,9 @@ class Repository < RepositoryBase .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| where(id: task.repository_rows .select(:repository_id)) @@ -43,13 +46,13 @@ class Repository < RepositoryBase def self.within_global_limits? 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 def self.within_team_limits?(team) 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 def self.search( diff --git a/app/utilities/smart_annotation.rb b/app/utilities/smart_annotation.rb index 6c35577ca..56e6d2aaa 100644 --- a/app/utilities/smart_annotation.rb +++ b/app/utilities/smart_annotation.rb @@ -100,6 +100,7 @@ class SmartAnnotation def repository_rows(repository) res = RepositoryRow + .active .where(repository: repository) .where_attributes_like('name', @query, at_search: true) .limit(Constants::ATWHO_SEARCH_LIMIT) diff --git a/spec/factories/repositories.rb b/spec/factories/repositories.rb index 441a91d0d..d9c31dfc4 100644 --- a/spec/factories/repositories.rb +++ b/spec/factories/repositories.rb @@ -11,5 +11,10 @@ FactoryBot.define do trait :read_shared do permission_level { :shared_read } end + trait :archived do + archived { true } + archived_on { Time.zone.now } + archived_by { created_by } + end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 8f912b0bb..a47349c14 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -46,6 +46,23 @@ describe Repository, type: :model do 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 let(:created_by) { create :user } let(:repository) { create :repository } @@ -62,4 +79,42 @@ describe Repository, type: :model do .to(change { Activity.count }) 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