TagSetPresenter: refactor humanized_essential_tag_string.

Move Post#humanized_essential_tag_string to TagSetPresenter#humanized_essential_tag_string.

This allows humanized_essential_tag_string to reuse the same set of tags
already fetched by the tag set presenter for the sidebar.

This avoids fetching the tag categories from memcache again (via
Post#typed_tags) when we're already fetched the tags once before.

This also means it's no longer necessary to cache humanized_essential_tag_string
itself in memcache, since it can be generated as quickly as the sidebar taglist.
This commit is contained in:
evazion
2018-09-30 20:39:12 -05:00
parent 739bb1270c
commit 88a177e1d5
5 changed files with 34 additions and 35 deletions

View File

@@ -144,7 +144,7 @@ class PostPresenter < Presenter
end
def humanized_essential_tag_string
@post.humanized_essential_tag_string
@humanized_essential_tag_string ||= tag_set_presenter.humanized_essential_tag_string(default: "##{@post.id}")
end
def filename_for_download

View File

@@ -61,6 +61,36 @@ class TagSetPresenter < Presenter
end.join(" \n")
end
def humanized_essential_tag_string(category_list: TagCategory.humanized_list, default: "")
strings = category_list.map do |category|
mapping = TagCategory.humanized_mapping[category]
max_tags = mapping["slice"]
regexmap = mapping["regexmap"]
formatstr = mapping["formatstr"]
excluded_tags = mapping["exclusion"]
type_tags = tags_for_category(category).map(&:name) - excluded_tags
next if type_tags.empty?
if max_tags > 0 && type_tags.length > max_tags
type_tags = type_tags.take(max_tags) + ["others"]
end
if regexmap != //
type_tags = type_tags.map { |tag| tag.match(regexmap)[1] }
end
if category == "copyright" && tags_for_category("character").blank?
type_tags.to_sentence
else
formatstr % type_tags.to_sentence
end
end
strings = strings.compact.join(" ").tr("_", " ")
strings.blank? ? default : strings
end
private
def tags
@@ -128,5 +158,5 @@ class TagSetPresenter < Presenter
html
end
memoize :tags, :tags_by_category, :ordered_tags
memoize :tags, :tags_by_category, :ordered_tags, :humanized_essential_tag_string
end