Code cleanup

This commit is contained in:
Douwe Maan 2015-10-13 23:03:53 +02:00
parent 93fcddd7a7
commit cd2583a3be
3 changed files with 17 additions and 23 deletions

View file

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

View file

@ -12,7 +12,6 @@ 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
@ -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

View file

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