Commit cd2583a3 authored by Douwe Maan's avatar Douwe Maan

Code cleanup

parent 93fcddd7
...@@ -14,10 +14,18 @@ module Gitlab ...@@ -14,10 +14,18 @@ module Gitlab
class ReferenceFilter < HTML::Pipeline::Filter class ReferenceFilter < HTML::Pipeline::Filter
LazyReference = Struct.new(:klass, :ids) do LazyReference = Struct.new(:klass, :ids) do
def self.load(refs) def self.load(refs)
refs.group_by(&:klass).flat_map do |klass, refs| lazy_references, values = refs.partition { |ref| ref.is_a?(self) }
lazy_values = lazy_references.group_by(&:klass).flat_map do |klass, refs|
ids = refs.flat_map(&:ids) ids = refs.flat_map(&:ids)
klass.where(id: ids) klass.where(id: ids)
end end
values + lazy_values
end
def load
self.klass.where(id: self.ids)
end end
end end
......
...@@ -12,8 +12,7 @@ module Gitlab ...@@ -12,8 +12,7 @@ module Gitlab
def initialize(*) def initialize(*)
super super
result[:lazy_references] ||= Hash.new { |hash, type| hash[type] = [] } result[:references] ||= Hash.new { |hash, type| hash[type] = [] }
result[:references] ||= Hash.new { |hash, type| hash[type] = [] }
end end
def call def call
...@@ -41,23 +40,16 @@ module Gitlab ...@@ -41,23 +40,16 @@ module Gitlab
references.each do |type, values| references.each do |type, values|
Array.wrap(values).each do |value| Array.wrap(values).each do |value|
refs = result[:references][type] << value
if value.is_a?(ReferenceFilter::LazyReference)
result[:lazy_references]
else
result[:references]
end
refs[type] << value
end end
end end
end end
# Will load all references of one type using one query. # Will load all references of one type using one query.
def load_lazy_references def load_lazy_references
result[:lazy_references].each do |type, refs| refs = result[:references]
values = ReferenceFilter::LazyReference.load(refs) refs.each do |type, values|
result[:references][type].concat(values) refs[type] = ReferenceFilter::LazyReference.load(values)
end end
end end
......
...@@ -54,19 +54,13 @@ module Gitlab ...@@ -54,19 +54,13 @@ module Gitlab
pipeline = HTML::Pipeline.new([filter, Gitlab::Markdown::ReferenceGathererFilter], context) pipeline = HTML::Pipeline.new([filter, Gitlab::Markdown::ReferenceGathererFilter], context)
values = [] values = @texts.flat_map do |text|
lazy_references = []
@texts.each do |text|
result = pipeline.call(text) result = pipeline.call(text)
values.concat(result[:references][filter_type]) result[:references][filter_type]
lazy_references.concat(result[:lazy_references][filter_type])
end end
lazy_values = Gitlab::Markdown::ReferenceFilter::LazyReference.load(lazy_references) Gitlab::Markdown::ReferenceFilter::LazyReference.load(values)
values.concat(lazy_values)
values
end end
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