Commit 378e2345 authored by Andreas Brandl's avatar Andreas Brandl

Signal-boost side effect for prepare method

The method does not only prepare the relation (as the `prepare_relation`
suggests), but also triggers batch counting which in turn executes the
query as a side-effect.

This is being signal-boosted with renaming the method to `prepare!`.

We use it in places where we actually want both (preloading/batch
counting) to happen. In other places (projects API), we actually don't
want that and have the ability to split this into multiple steps now.
parent 50876d4c
......@@ -176,7 +176,6 @@ module API
end
class BasicProjectDetails < ProjectIdentity
include ::API::ProjectsRelationBuilder
include ::API::ProjectsBatchCounting
expose :default_branch, if: -> (project, options) { Ability.allowed?(options[:current_user], :download_code, project) }
......@@ -212,6 +211,14 @@ module API
expose :namespace, using: 'API::Entities::NamespaceBasic'
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
def self.preload_relation(projects_relation, options = {})
# Preloading tags, should be done with using only `:tags`,
......@@ -419,7 +426,7 @@ module API
options: { only_owned: true }
).execute
Entities::Project.prepare_relation(projects)
Entities::Project.prepare!(projects)
end
expose :shared_projects, using: Entities::Project do |group, options|
......@@ -429,7 +436,7 @@ module API
options: { only_shared: true }
).execute
Entities::Project.prepare_relation(projects)
Entities::Project.prepare!(projects)
end
end
......
......@@ -231,7 +231,7 @@ module API
projects, options = with_custom_attributes(projects, options)
present options[:with].prepare_relation(projects), options
present options[:with].prepare!(projects), options
end
desc 'Get a list of subgroups in this group.' do
......
......@@ -96,7 +96,7 @@ module API
options[:with] = Entities::BasicProjectDetails if params[:simple]
projects = options[:with].prepare_relation(projects, options)
projects = options[:with].preload_relation(projects, options)
[projects, options]
end
......
# frozen_string_literal: true
module API
module ProjectsRelationBuilder
extend ActiveSupport::Concern
class_methods do
# Prepare the given projects relation, e.g. perform preloading.
def prepare_relation(projects_relation, options = {})
preload_relation(projects_relation, options)
end
def preload_relation(projects_relation, options = {})
projects_relation
end
end
end
end
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