diff --git a/app/controllers/my_module_repositories_controller.rb b/app/controllers/my_module_repositories_controller.rb index b41711d39..9a4bd5455 100644 --- a/app/controllers/my_module_repositories_controller.rb +++ b/app/controllers/my_module_repositories_controller.rb @@ -115,7 +115,7 @@ class MyModuleRepositoriesController < ApplicationController end def load_repository - @repository = Repository.find_by(id: params[:id]) + @repository = Repository.with_archived.find_by(id: params[:id]) render_404 unless @repository end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index f9704fd2e..277e06d9f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -345,17 +345,13 @@ 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 def load_repositories @repositories = Repository.accessible_by_teams(current_team).order('repositories.created_at ASC') - @repositories = if params[:archived] - @repositories.archived - else - @repositories.active - end + @repositories = @repositories.archived if params[:archived] end def set_inline_name_editing 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/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index b0c1dea55..910341f0c 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -123,7 +123,7 @@ module ReportsHelper end def assign_repository_or_snapshot(my_module, element_id, snapshot, repository) - original_repository = Repository.find_by(id: element_id) if element_id + original_repository = Repository.with_archived.find_by(id: element_id) if element_id repository ||= snapshot repository || my_module.active_snapshot_or_live(original_repository) || original_repository end 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/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index d7383a557..6197c1d6f 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -5,10 +5,12 @@ class RepositorySnapshot < RepositoryBase after_save :refresh_report_references, if: :saved_change_to_selected before_destroy :refresh_report_references_for_destroy, prepend: true - belongs_to :original_repository, foreign_key: :parent_id, - class_name: 'Repository', - inverse_of: :repository_snapshots, - optional: true + belongs_to :original_repository, -> { unscope(where: :archived) }, + foreign_key: :parent_id, + class_name: 'Repository', + inverse_of: :repository_snapshots, + optional: true + belongs_to :my_module, optional: true validates :name, presence: true, length: { maximum: Constants::NAME_MAX_LENGTH } diff --git a/app/services/smart_annotations/html_preview.rb b/app/services/smart_annotations/html_preview.rb index c9a390d82..101402e0c 100644 --- a/app/services/smart_annotations/html_preview.rb +++ b/app/services/smart_annotations/html_preview.rb @@ -46,10 +46,14 @@ module SmartAnnotations def generate_rep_item_snippet(name, object) if object repository_name = fetch_repository_name(object) - return "" \ - "#{trim_repository_name(repository_name)} " \ - "#{object.name}" + link = if object.archived? + "#{repository_name} #{I18n.t('atwho.res.archived')}" + else + "#{object.name}" + end + + return "#{trim_repository_name(repository_name)} " + link end "Inv " \ "#{name} #{I18n.t('atwho.res.deleted')}" 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/app/views/reports/new/modal/_module_contents_inner.html.erb b/app/views/reports/new/modal/_module_contents_inner.html.erb index d131d7783..9e5e836b8 100644 --- a/app/views/reports/new/modal/_module_contents_inner.html.erb +++ b/app/views/reports/new/modal/_module_contents_inner.html.erb @@ -69,7 +69,7 @@ <%= form.check_box :module_activity, label: t("projects.reports.elements.modals.module_contents_inner.activity") %> <% # List all repositories, no matter whether rows are assigned or not %> - <% Repository.accessible_by_teams(@project.team).order(created_at: :asc).select(:id, :name).find_each do |repository| %> + <% Repository.accessible_by_teams(@project.team).with_archived.order(created_at: :asc).select(:id, :name).find_each do |repository| %>
  • <%= form.check_box "module_repository_#{repository.id}", label: repository.name.capitalize, data: { id: repository.id } %>
  • 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