Merge pull request #2661 from urbanrotnik/ur-sci-4740-default-scope

Update default scope [SCI-4740] [SCI-4687][SCI-4746]
This commit is contained in:
Urban Rotnik 2020-06-18 14:07:10 +02:00 committed by GitHub
commit b498aa54d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 90 additions and 21 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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(

View file

@ -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 }

View file

@ -46,10 +46,14 @@ module SmartAnnotations
def generate_rep_item_snippet(name, object)
if object
repository_name = fetch_repository_name(object)
return "<span class='sa-type'>" \
"#{trim_repository_name(repository_name)}</span> " \
"<a href='#{ROUTES.repository_repository_row_path(object.repository, object)}' " \
"class='record-info-link'>#{object.name}</a>"
link = if object.archived?
"#{repository_name} #{I18n.t('atwho.res.archived')}"
else
"<a href='#{ROUTES.repository_repository_row_path(object.repository, object)}' " \
"class='record-info-link'>#{object.name}</a>"
end
return "<span class='sa-type'>#{trim_repository_name(repository_name)}</span> " + link
end
"<span class='sa-type'>Inv</span> " \
"#{name} #{I18n.t('atwho.res.deleted')}"

View file

@ -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)

View file

@ -69,7 +69,7 @@
<%= form.check_box :module_activity, label: t("projects.reports.elements.modals.module_contents_inner.activity") %>
</li>
<% # 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| %>
<li>
<%= form.check_box "module_repository_#{repository.id}", label: repository.name.capitalize, data: { id: repository.id } %>
</li>

View file

@ -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

View file

@ -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