From 4ef30844f64bd9e3f7b52ae8d2894af68b54c0b8 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 18 Feb 2021 19:16:41 -0600 Subject: [PATCH] views: factor out humanized_number helper. * Render counts in the user profile tooltip the same way as tag counts. * Render tag counts in the tags index page the same way as tag counts in the tags list. --- app/components/tag_list_component.rb | 11 +---------- .../tag_list_component.html+categorized.erb | 2 +- .../tag_list_component.html+search.erb | 2 +- app/helpers/application_helper.rb | 8 +++++++- app/views/tags/index.html.erb | 2 +- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/components/tag_list_component.rb b/app/components/tag_list_component.rb index 2b22640ba..00d235f7e 100644 --- a/app/components/tag_list_component.rb +++ b/app/components/tag_list_component.rb @@ -2,6 +2,7 @@ class TagListComponent < ApplicationComponent attr_reader :tags, :current_query, :show_extra_links + delegate :humanized_number, to: :helpers def initialize(tags: [], current_query: nil, show_extra_links: false) @tags = tags @@ -38,14 +39,4 @@ class TagListComponent < ApplicationComponent def is_underused_tag?(tag) tag.post_count <= 1 && tag.general? && tag.name !~ /_\((cosplay|style)\)\z/ end - - def humanized_post_count(tag) - if tag.post_count >= 10_000 - "#{tag.post_count / 1_000}k" - elsif tag.post_count >= 1_000 - "%.1fk" % (tag.post_count / 1_000.0) - else - tag.post_count.to_s - end - end end diff --git a/app/components/tag_list_component/tag_list_component.html+categorized.erb b/app/components/tag_list_component/tag_list_component.html+categorized.erb index 1f76e52c7..a47aad530 100644 --- a/app/components/tag_list_component/tag_list_component.html+categorized.erb +++ b/app/components/tag_list_component/tag_list_component.html+categorized.erb @@ -22,7 +22,7 @@ <%= link_to t.pretty_name, posts_path(tags: t.name), class: "search-tag" %> - <%= tag.span humanized_post_count(t), class: ["post-count", ("low-post-count" if is_underused_tag?(t))], title: t.post_count %> + <%= tag.span humanized_number(t.post_count), class: ["post-count", ("low-post-count" if is_underused_tag?(t))], title: t.post_count %> <% end %> diff --git a/app/components/tag_list_component/tag_list_component.html+search.erb b/app/components/tag_list_component/tag_list_component.html+search.erb index 9d8779b1e..72888f6e5 100644 --- a/app/components/tag_list_component/tag_list_component.html+search.erb +++ b/app/components/tag_list_component/tag_list_component.html+search.erb @@ -16,7 +16,7 @@ <% end %> <%= link_to t.pretty_name, posts_path(tags: t.name), class: "search-tag" %> - <%= tag.span humanized_post_count(t), class: "post-count", title: t.post_count %> + <%= tag.span humanized_number(t.post_count), class: "post-count", title: t.post_count %> <% end %> diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 527e3be18..1057a076f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -122,7 +122,13 @@ module ApplicationHelper end def humanized_number(number) - number_to_human number, units: { thousand: "k", million: "m" }, format: "%n%u" + if number >= 10_000 + "#{number / 1_000}k" + elsif number >= 1_000 + "%.1fk" % (number / 1_000.0) + else + number.to_s + end end def time_ago_in_words_tagged(time, compact: false) diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 12efdf39c..4a99de931 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -14,7 +14,7 @@ ← <%= link_to tag_alias.antecedent_name, tag_alias, class: "fineprint" %> <% end %> - <%= tag.span t.post_count, class: "post-count" %> + <%= tag.span humanized_number(t.post_count), class: "post-count", title: t.post_count %> <% end %> <% t.column column: "control" do |tag| %> <% if policy(tag).update? %>