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
116 116
        return [] unless asset
117
        ActionController::Base.cache_store.fetch("theme-icons/#{id}/#{sprite}/#{asset.digest}") do
118
          asset.content.scan(/id=['"]icon--([^'"]+)['"]/).flatten
119
        end
117
        # This is called once per icon rendered, so it must not read and
118
        # scan the sprite every time. Memoize the names per asset digest,
119
        # so that changes to the sprite file are still picked up.
120
        @icons ||= {}
121
        digest, names = @icons[sprite]
122
        return names if digest == asset.digest
123

  
124
        names = asset.content.scan(/id=['"]icon--([^'"]+)['"]/).flatten
125
        @icons[sprite] = [asset.digest, names]
126
        names
120 127
      end
121 128
      def asset_paths
test/unit/lib/redmine/themes_test.rb
82 82
    assert_equal [], theme.icons('icons')
83 83
  end
84
  def test_icons_should_be_cached
84
  def test_icons_should_pick_up_changes_to_the_sprite
85 85
    theme = Redmine::Themes::Theme.new('/tmp/test')
86
    theme.stubs(:id).returns('test')
87 86
    theme.stubs(:image_path).with('icons.svg').returns('themes/test/icons.svg')
88 87
    asset = mock('asset')
89
    asset.stubs(:content).returns('<symbol id="icon--edit"></symbol>')
90 88
    asset.stubs(:digest).returns('123456')
89
    asset.stubs(:content).returns('<symbol id="icon--edit"></symbol>')
91 90
    Rails.application.assets.load_path.stubs(:find).with('themes/test/icons.svg').returns(asset)
92
    # Use a memory store for this test since the test environment uses null_store
93
    memory_store = ActiveSupport::Cache.lookup_store(:memory_store)
94
    ActionController::Base.stubs(:cache_store).returns(memory_store)
95

  
96
    # First call - cache miss
97 91
    assert_equal ['edit'], theme.icons('icons')
98
    # Second call - verify it's in the cache
99
    cache_key = "theme-icons/test/icons/123456"
100
    assert_equal ['edit'], memory_store.read(cache_key)
101

  
102
    # If digest changes, it should miss cache
92
    # A new digest must invalidate the memoized names
103 93
    asset.stubs(:digest).returns('789')
104 94
    asset.stubs(:content).returns('<symbol id="icon--new"></symbol>')
105 95
    assert_equal ['new'], theme.icons('icons')
    (1-1/1)