Created
March 4, 2024 15:13
-
-
Save jqr/800ad8aadb6d9c9447e2cc9d195918f7 to your computer and use it in GitHub Desktop.
CTE Limited Badged Counters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GoodJob::Job.retried.badged_count | |
module CteLimited | |
extend ActiveSupport::Concern | |
included do | |
scope :cte_limited, -> (limit = 10_000) { with(table_name => limit(limit)) } | |
scope :badged_count, -> (limit = 999) { (limit ? cte_limited(limit + 1) : all).count.badgify(limit:) } | |
end | |
end | |
# Make this number suitable for use as a badge. Automatically hides if zero, | |
# delimits normally, and shows a + if a limit is reached. | |
def badgify(limit: 999) | |
if self == 0 | |
"" | |
elsif limit && self > limit | |
StringUtil.int(limit) + "+" | |
else | |
StringUtil.int(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment