diff options
| author | Paco Guzman <pacoguzmanp@gmail.com> | 2016-09-21 14:46:10 +0200 |
|---|---|---|
| committer | Paco Guzman <pacoguzmanp@gmail.com> | 2016-10-06 16:51:55 +0200 |
| commit | b4819a5d750ee458a424cf7965926c5758bf784a (patch) | |
| tree | 427a3df9f9ac740622b2000ca7e36c6f07792b21 /lib/banzai/object_renderer.rb | |
| parent | 0bbeff3d5e6c1b5ea3b364f052ed6f777c3aa645 (diff) | |
| download | gitlab-ce-18663-commits-reference-mentionables.tar.gz | |
GitPushService group but author cross_reference creation18663-commits-reference-mentionables
The reference extractor phase happens once per type not once
pero pushed commit, so we could be avoiding a lot of DB queries
Diffstat (limited to 'lib/banzai/object_renderer.rb')
| -rw-r--r-- | lib/banzai/object_renderer.rb | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/lib/banzai/object_renderer.rb b/lib/banzai/object_renderer.rb index 9aef807c152..ffd11e2425b 100644 --- a/lib/banzai/object_renderer.rb +++ b/lib/banzai/object_renderer.rb @@ -51,9 +51,31 @@ module Banzai redactor.redact(documents) end + # Renders the attributes of a set of objects. + # + # Returns an Array of `Nokogiri::HTML::Document`. + def render_attributes(objects, attribute) + strings_and_contexts = populate_contexts(objects, attribute) + + Banzai.cache_collection_render(strings_and_contexts).each_with_index.map do |html, index| + Banzai::Pipeline[:relative_link].to_document(html, strings_and_contexts[index][:context]) + end + end + + def populate_contexts(objects, attribute) + objects.map do |object| + context = context_for(object, attribute) + + string = object.__send__(attribute) + + { text: string, context: context } + end + end + # Returns a Banzai context for the given object and attribute. def context_for(object, attribute) - context = base_context.merge(cache_key: [object, attribute]) + context = base_context.merge(base_context_klass_attr(object.class, attribute)) + context[:cache_key] = [object, attribute] if object.respond_to?(:author) context[:author] = object.author @@ -62,20 +84,14 @@ module Banzai context end - # Renders the attributes of a set of objects. - # - # Returns an Array of `Nokogiri::HTML::Document`. - def render_attributes(objects, attribute) - strings_and_contexts = objects.map do |object| - context = context_for(object, attribute) - - string = object.__send__(attribute) + def base_context_klass_attr(klass, attribute) + @base_context_klass_attr ||= Hash.new { |h, k| h[k] = {} } + @base_context_klass_attr[klass][attribute] ||= begin + return {} unless klass.respond_to?(:mentionable_attrs) - { text: string, context: context } - end + _attr, context_klass_options = klass.mentionable_attrs.detect { |attr, _options| attr.to_sym == attribute } - Banzai.cache_collection_render(strings_and_contexts).each_with_index.map do |html, index| - Banzai::Pipeline[:relative_link].to_document(html, strings_and_contexts[index][:context]) + context_klass_options || {} end end |
