Skip to content

Instantly share code, notes, and snippets.

@jqr
Created March 4, 2024 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jqr/800ad8aadb6d9c9447e2cc9d195918f7 to your computer and use it in GitHub Desktop.
Save jqr/800ad8aadb6d9c9447e2cc9d195918f7 to your computer and use it in GitHub Desktop.
CTE Limited Badged Counters
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