Remove zero-padding from account slug

This shortens URLs in the self-hosted case, where external account ID is
typically a small number.
This commit is contained in:
Kevin McConnell
2026-01-28 11:48:38 +00:00
parent c059e0b216
commit e105ca6fb5
2 changed files with 11 additions and 3 deletions
@@ -1,6 +1,5 @@
module AccountSlug
PATTERN = /(\d{7,})/
FORMAT = "%07d"
PATTERN = /(\d+)/
PATH_INFO_MATCH = /\A(\/#{AccountSlug::PATTERN})/
class Extractor
@@ -40,7 +39,7 @@ module AccountSlug
end
def self.decode(slug) slug.to_i end
def self.encode(id) FORMAT % id end
def self.encode(id) id.to_s end
end
Rails.application.config.middleware.insert_after Rack::TempfileReaper, AccountSlug::Extractor
@@ -44,6 +44,15 @@ class AccountSlugExtractorTest < ActiveSupport::TestCase
assert_nil captured.fetch(:current_account)
end
test "encodes account IDs without zero-padding" do
assert_equal "1", AccountSlug.encode(1)
end
test "decodes both padded and non-padded slugs" do
assert_equal 123, AccountSlug.decode("123")
assert_equal 123, AccountSlug.decode("0000123")
end
private
def call_with_env(path, extra_env = {})
captured = {}