diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index f90d44789..e38e24690 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -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. diff --git a/app/presenters/tag_set_presenter.rb b/app/presenters/tag_set_presenter.rb index e8b499010..aaa83655c 100644 --- a/app/presenters/tag_set_presenter.rb +++ b/app/presenters/tag_set_presenter.rb @@ -8,6 +8,9 @@ class TagSetPresenter < Presenter extend Memoist attr_reader :tag_names + # @param [Array] 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 %{#{html}}.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 << "" html end + + memoize :tags, :tags_by_category, :ordered_tags end diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 2b5ddc82a..c838e2ecd 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -78,7 +78,7 @@
<%= 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" } %>