Commit 735c7425 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '31914-support-multiple-query-on-todos-finder-for-project-ids-pd' into 'master'

Support query for multiple not deleted projects by ids

See merge request gitlab-org/gitlab!19618
parents 54c64b52 60f6264a
......@@ -117,12 +117,6 @@ class TodosFinder
params[:group_id].present?
end
def project
strong_memoize(:project) do
Project.find_without_deleted(params[:project_id]) if project?
end
end
def group
strong_memoize(:group) do
Group.find(params[:group_id])
......@@ -181,7 +175,7 @@ class TodosFinder
def by_project(items)
if project?
items.for_project(project)
items.for_undeleted_projects.for_project(params[:project_id])
else
items
end
......
......@@ -464,13 +464,6 @@ class Project < ApplicationRecord
# Used by Projects::CleanupService to hold a map of rewritten object IDs
mount_uploader :bfg_object_map, AttachmentUploader
# Returns a project, if it is not about to be removed.
#
# id - The ID of the project to retrieve.
def self.find_without_deleted(id)
without_deleted.find_by_id(id)
end
def self.eager_load_namespace_and_owner
includes(namespace: :owner)
end
......
......@@ -55,7 +55,8 @@ class Todo < ApplicationRecord
scope :done, -> { with_state(:done) }
scope :for_action, -> (action) { where(action: action) }
scope :for_author, -> (author) { where(author: author) }
scope :for_project, -> (project) { where(project: project) }
scope :for_project, -> (projects) { where(project: projects) }
scope :for_undeleted_projects, -> { joins(:project).merge(Project.without_deleted) }
scope :for_group, -> (group) { where(group: group) }
scope :for_type, -> (type) { where(target_type: type) }
scope :for_target, -> (id) { where(target_id: id) }
......
......@@ -163,6 +163,28 @@ describe TodosFinder do
expect(todos).to match_array([todo1, todo2])
end
end
context 'by project' do
let_it_be(:project1) { create(:project) }
let_it_be(:project2) { create(:project) }
let_it_be(:project3) { create(:project) }
let!(:todo1) { create(:todo, user: user, project: project1, state: :pending) }
let!(:todo2) { create(:todo, user: user, project: project2, state: :pending) }
let!(:todo3) { create(:todo, user: user, project: project3, state: :pending) }
it 'returns the expected todos for one project' do
todos = finder.new(user, { project_id: project2.id }).execute
expect(todos).to match_array([todo2])
end
it 'returns the expected todos for many projects' do
todos = finder.new(user, { project_id: [project2.id, project1.id] }).execute
expect(todos).to match_array([todo2, todo1])
end
end
end
context 'external authorization' do
......
......@@ -4899,20 +4899,6 @@ describe Project do
end
end
describe '.find_without_deleted' do
it 'returns nil if the project is about to be removed' do
project = create(:project, pending_delete: true)
expect(described_class.find_without_deleted(project.id)).to be_nil
end
it 'returns a project when it is not about to be removed' do
project = create(:project)
expect(described_class.find_without_deleted(project.id)).to eq(project)
end
end
describe '.for_group' do
it 'returns the projects for a given group' do
group = create(:group)
......
......@@ -221,6 +221,40 @@ describe Todo do
expect(described_class.for_project(project1)).to eq([todo])
end
it 'returns the todos for many projects' do
project1 = create(:project)
project2 = create(:project)
project3 = create(:project)
todo1 = create(:todo, project: project1)
todo2 = create(:todo, project: project2)
create(:todo, project: project3)
expect(described_class.for_project([project2, project1])).to contain_exactly(todo2, todo1)
end
end
describe '.for_undeleted_projects' do
let(:project1) { create(:project) }
let(:project2) { create(:project) }
let(:project3) { create(:project) }
let!(:todo1) { create(:todo, project: project1) }
let!(:todo2) { create(:todo, project: project2) }
let!(:todo3) { create(:todo, project: project3) }
it 'returns the todos for a given project' do
expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo2, todo3)
end
context 'when todo belongs to deleted project' do
let(:project2) { create(:project, pending_delete: true) }
it 'excludes todos of deleted projects' do
expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo3)
end
end
end
describe '.for_group' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment