Skip to content

Commit

Permalink
[feature] Allow configuring caching
Browse files Browse the repository at this point in the history
As with Rails, this allows to configure the cache store
through `config.rails_heroicon_cache_store`

By default it is set to :memory_store.

Closes abeidahmed#48
  • Loading branch information
hallelujah committed Apr 17, 2024
1 parent 30ca6e8 commit 3a140a3
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ passed into the `title` option will be rendered inside of a
`<title>` tag within the rendered SVG, which modern browsers will lean on to
display a tooltip on hover.

### Caching

Like with Rails, you can provide `config.rails_heroicon_cache_store` to change the cache store.

By default, caching uses `:memory_store`

## Development

- Clone the repo
Expand Down
12 changes: 5 additions & 7 deletions lib/rails_heroicon/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module RailsHeroicon
module Helper
include ActionView::Helpers::TagHelper

mattr_accessor :icon_cache, default: {}
# To add a heroicon, call <tt><%= heroicon "icon_name" %></tt> on your erb template.
# Head over to https://heroicons.com to view all the icons.
#
Expand Down Expand Up @@ -38,13 +37,12 @@ module Helper
# if <tt>aria-label</tt> is set, then <tt>role=img</tt> is set automatically.
def heroicon(symbol, title: nil, **options)
cache_key = [symbol, title, options]
return icon_cache[cache_key] if icon_cache[cache_key]

icon = RailsHeroicon.new(symbol, **options)
title_tag = content_tag(:title, title) if title
tag = content_tag(:svg, title_tag.to_s.html_safe + icon.svg_path.html_safe, icon.options)
icon_cache[cache_key] = tag
tag
::RailsHeroicon.cache.fetch(cache_key) do
icon = RailsHeroicon.new(symbol, **options)
title_tag = content_tag(:title, title) if title
content_tag(:svg, title_tag.to_s.html_safe + icon.svg_path.html_safe, icon.options)
end
end
end
end
12 changes: 11 additions & 1 deletion lib/rails_heroicon/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
require_relative "helper"

module RailsHeroicon
class Railtie < Rails::Railtie
class << self
attr_accessor :cache
end

class Railtie < ::Rails::Railtie
initializer "rails_heroicon.helper" do
ActionView::Base.send :include, Helper
end

config.rails_heroicon_cache_store = :memory_store

initializer "rails_heroicon.cache" do
::RailsHeroicon.cache = ActiveSupport::Cache.lookup_store(*config.rails_heroicon_cache_store)
end
end
end
5 changes: 5 additions & 0 deletions test/rails_heroicon/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
module RailsHeroicon
class HelperTest < Minitest::Test
include ::RailsHeroicon::Helper
include RunInitializer

def setup
run_initializer
end

def test_attributes
icon = heroicon("user", class: "text-red-600", data: {foo: "bar"})
Expand Down
4 changes: 2 additions & 2 deletions test/rails_heroicon/rails_heroicon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def test_solid_icon_stroke_width

private

def icon(name: "user", **options)
RailsHeroicon.new(name, **options)
def icon(name: "user", **)
RailsHeroicon.new(name, **)
end
end
end
43 changes: 43 additions & 0 deletions test/rails_heroicon/railtie_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require "test_helper"

module RailsHeroicon
class RailtieTest < Minitest::Test
include RunInitializer

def before_setup
save_cache_store
end

def setup
run_initializer
end

def teardown
reset_cache_store
run_initializer
end

class DefaultCacheStoreTest < RailtieTest
def test_default_config
assert_equal :memory_store, ::RailsHeroicon::Railtie.config.rails_heroicon_cache_store
end

def test_default_cache
assert_instance_of ActiveSupport::Cache::MemoryStore, ::RailsHeroicon.cache
end
end

class ChangeCacheStoreTest < RailtieTest
def setup
set_cache_store :null_store
super
end

def test_changed_cache
assert_instance_of ActiveSupport::Cache::NullStore, ::RailsHeroicon.cache
end
end
end
end
22 changes: 22 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,25 @@
require "rails_heroicon"

require "minitest/autorun"
require "rails/railtie"
require "rails_heroicon/railtie"

module RunInitializer
def run_initializer
::RailsHeroicon::Railtie.initializers.find { |i| i.name == "rails_heroicon.cache" }.bind(RailsHeroicon::Railtie).run
end

def set_cache_store(store)
::RailsHeroicon::Railtie.config.rails_heroicon_cache_store = store
end

def save_cache_store
@icon_cache_store = ::RailsHeroicon::Railtie.config.rails_heroicon_cache_store
end

def reset_cache_store
raise "Cannot reset cache store, please set it before with #save_cache_store" unless instance_variable_defined?(:@icon_cache_store)

set_cache_store @icon_cache_store
end
end

0 comments on commit 3a140a3

Please sign in to comment.