Commit 1e0673f9 authored by Andreas Brandl's avatar Andreas Brandl

Move prepare! into concern

parent 378e2345
...@@ -211,14 +211,6 @@ module API ...@@ -211,14 +211,6 @@ module API
expose :namespace, using: 'API::Entities::NamespaceBasic' expose :namespace, using: 'API::Entities::NamespaceBasic'
expose :custom_attributes, using: 'API::Entities::CustomAttribute', if: :with_custom_attributes expose :custom_attributes, using: 'API::Entities::CustomAttribute', if: :with_custom_attributes
# This adds preloading to the query and executes batch counting
# Side-effect: The query will be executed during batch counting
def self.prepare!(projects_relation)
preload_relation(projects_relation).tap do |projects|
execute_batch_counting(projects)
end
end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def self.preload_relation(projects_relation, options = {}) def self.preload_relation(projects_relation, options = {})
# Preloading tags, should be done with using only `:tags`, # Preloading tags, should be done with using only `:tags`,
......
...@@ -5,8 +5,12 @@ module API ...@@ -5,8 +5,12 @@ module API
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
def forks_counting_projects(projects) # This adds preloading to the query and executes batch counting
projects # Side-effect: The query will be executed during batch counting
def prepare!(projects_relation)
preload_relation(projects_relation).tap do |projects|
execute_batch_counting(projects)
end
end end
def execute_batch_counting(projects) def execute_batch_counting(projects)
...@@ -14,6 +18,10 @@ module API ...@@ -14,6 +18,10 @@ module API
::Projects::BatchOpenIssuesCountService.new(projects).refresh_cache ::Projects::BatchOpenIssuesCountService.new(projects).refresh_cache
end end
def forks_counting_projects(projects)
projects
end
end end
end end
end end
...@@ -3,13 +3,32 @@ ...@@ -3,13 +3,32 @@
require 'spec_helper' require 'spec_helper'
describe API::ProjectsBatchCounting do describe API::ProjectsBatchCounting do
describe '.execute_batch_counting' do subject do
subject do Class.new do
Class.new do include ::API::ProjectsBatchCounting
include ::API::ProjectsBatchCounting end
end end
describe '.prepare!' do
let(:projects) { double }
let(:preloaded_projects) { double }
it 'preloads the relation' do
expect(subject).to receive(:preload_relation).with(projects).and_return(preloaded_projects)
allow(subject).to receive(:execute_batch_counting).with(preloaded_projects)
expect(subject.prepare!(projects)).to eq(preloaded_projects)
end
it 'executes batch counting' do
allow(subject).to receive(:preload_relation).with(projects).and_return(preloaded_projects)
expect(subject).to receive(:execute_batch_counting).with(preloaded_projects)
subject.prepare!(projects)
end end
end
describe '.execute_batch_counting' do
let(:projects) { create_list(:project, 2) } let(:projects) { create_list(:project, 2) }
let(:count_service) { double } let(:count_service) { double }
......
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