TagSetPresenter: refactor tag string for post edit form.

Move PostPresenter#categorized_tag_groups to TagSetPresenter#split_tag_list_text.

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

This avoids a memcache call to get the tag categories when rendering the
tag string for the post edit form.
This commit is contained in:
evazion
2018-09-30 17:46:33 -05:00
parent b1f2096d72
commit 739bb1270c
3 changed files with 24 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
class PostPresenter < Presenter
attr_reader :pool, :next_post_in_pool
delegate :tag_list_html, :split_tag_list_html, :inline_tag_list_html, to: :tag_set_presenter
delegate :tag_list_html, :split_tag_list_html, :split_tag_list_text, :inline_tag_list_html, to: :tag_set_presenter
def self.preview(post, options = {})
if post.nil?
@@ -151,22 +151,6 @@ class PostPresenter < Presenter
"#{humanized_essential_tag_string} - #{@post.md5}.#{@post.file_ext}"
end
def categorized_tag_groups
string = []
TagCategory.categorized_list.each do |category|
if @post.typed_tags(category).any?
string << @post.typed_tags(category).join(" ")
end
end
string
end
def categorized_tag_string
categorized_tag_groups.join(" \n")
end
def safe_mode_message(template)
html = ["This image is unavailable on safe mode (#{Danbooru.config.app_name}). Go to "]
html << template.link_to("Danbooru", "https://danbooru.donmai.us") # XXX don't hardcode.

View File

@@ -8,6 +8,9 @@ class TagSetPresenter < Presenter
extend Memoist
attr_reader :tag_names
# @param [Array<String>] a list of tags to present. Tags will be presented in
# the order given. The list should not contain duplicates. The list may
# contain tags that do not exist in the tags table, such as metatags.
def initialize(tag_names)
@tag_names = tag_names
end
@@ -30,7 +33,7 @@ class TagSetPresenter < Presenter
html = ""
category_list.each do |category|
typetags = ordered_tags.select { |tag| tag.category == Tag.categories.value_for(category) }
typetags = tags_for_category(category)
if typetags.any?
html << TagCategory.header_mapping[category] if headers
@@ -51,12 +54,27 @@ class TagSetPresenter < Presenter
%{<span class="inline-tag-list">#{html}</span>}.html_safe
end
# the list of tags inside the tag box in the post edit form.
def split_tag_list_text(category_list: TagCategory.categorized_list)
category_list.map do |category|
tags_for_category(category).map(&:name).join(" ")
end.join(" \n")
end
private
def tags
Tag.where(name: tag_names).select(:name, :post_count, :category)
end
memoize :tags
def tags_by_category
ordered_tags.group_by(&:category)
end
def tags_for_category(category_name)
category = TagCategory.mapping[category_name.downcase]
tags_by_category[category] || []
end
def ordered_tags
names_to_tags = tags.map { |tag| [tag.name, tag] }.to_h
@@ -65,7 +83,6 @@ class TagSetPresenter < Presenter
names_to_tags[name] || Tag.new(name: name).freeze
end
end
memoize :ordered_tags
def build_list_item(tag, name_only: false, humanize_tags: true, show_extra_links: false, current_query: "")
name = tag.name
@@ -110,4 +127,6 @@ class TagSetPresenter < Presenter
html << "</li>"
html
end
memoize :tags, :tags_by_category, :ordered_tags
end

View File

@@ -78,7 +78,7 @@
<div class="input">
<div>
<%= f.label :tag_string, "Tags" %>
<%= f.text_area :tag_string, :size => "50x5", :value => post.presenter.categorized_tag_string + " ", :data => { :autocomplete => "tag-edit" } %>
<%= f.text_area :tag_string, :size => "50x5", :value => post.presenter.split_tag_list_text + " ", :data => { :autocomplete => "tag-edit" } %>
<span id="open-edit-dialog" class="ui-icon ui-icon-arrow-1-ne" title="detach" style="display: none;" data-shortcut="shift+e"/>
</div>