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.
This commit is contained in:
evazion
2021-02-18 19:16:41 -06:00
parent bcca0ca53a
commit 4ef30844f6
5 changed files with 11 additions and 14 deletions

View File

@@ -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

View File

@@ -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 %>
</li>
<% end %>
</ul>

View File

@@ -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 %>
</li>
<% end %>
</ul>

View File

@@ -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)

View File

@@ -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? %>