Project

General

Profile

Defect #44412 ยป 0001-Memoize-theme-icon-names-instead-of-fetching-them-fr.patch

Go MAEDA, 2026-09-03 10:31

View differences:

lib/redmine/themes.rb
return [] unless asset
ActionController::Base.cache_store.fetch("theme-icons/#{id}/#{sprite}/#{asset.digest}") do
asset.content.scan(/id=['"]icon--([^'"]+)['"]/).flatten
end
# This is called once per icon rendered, so it must not read and
# scan the sprite every time. Memoize the names per asset digest,
# so that changes to the sprite file are still picked up.
@icons ||= {}
digest, names = @icons[sprite]
return names if digest == asset.digest
names = asset.content.scan(/id=['"]icon--([^'"]+)['"]/).flatten
@icons[sprite] = [asset.digest, names]
names
end
def asset_paths
test/unit/lib/redmine/themes_test.rb
assert_equal [], theme.icons('icons')
end
def test_icons_should_be_cached
def test_icons_should_pick_up_changes_to_the_sprite
theme = Redmine::Themes::Theme.new('/tmp/test')
theme.stubs(:id).returns('test')
theme.stubs(:image_path).with('icons.svg').returns('themes/test/icons.svg')
asset = mock('asset')
asset.stubs(:content).returns('<symbol id="icon--edit"></symbol>')
asset.stubs(:digest).returns('123456')
asset.stubs(:content).returns('<symbol id="icon--edit"></symbol>')
Rails.application.assets.load_path.stubs(:find).with('themes/test/icons.svg').returns(asset)
# Use a memory store for this test since the test environment uses null_store
memory_store = ActiveSupport::Cache.lookup_store(:memory_store)
ActionController::Base.stubs(:cache_store).returns(memory_store)
# First call - cache miss
assert_equal ['edit'], theme.icons('icons')
# Second call - verify it's in the cache
cache_key = "theme-icons/test/icons/123456"
assert_equal ['edit'], memory_store.read(cache_key)
# If digest changes, it should miss cache
# A new digest must invalidate the memoized names
asset.stubs(:digest).returns('789')
asset.stubs(:content).returns('<symbol id="icon--new"></symbol>')
assert_equal ['new'], theme.icons('icons')
    (1-1/1)