Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ GEM
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
minitest (5.20.0)
nokogiri (1.16.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
Expand Down Expand Up @@ -129,6 +131,7 @@ GEM
zeitwerk (2.6.17)

PLATFORMS
arm64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
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
4 changes: 4 additions & 0 deletions lib/rails_heroicon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
module RailsHeroicon
ICON_PATH = File.join(File.dirname(__FILE__), "../compressed/icons.json")
ICONS = JSON.parse(File.read(ICON_PATH)).freeze

class << self
attr_accessor :cache
end
end
Empty file.
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
9 changes: 8 additions & 1 deletion lib/rails_heroicon/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
require_relative "helper"

module RailsHeroicon
class Railtie < Rails::Railtie
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
store = config.rails_heroicon_cache_store
::RailsHeroicon.cache = ActiveSupport::Cache.lookup_store(*store)
end
end
end
1 change: 1 addition & 0 deletions test/rails_heroicon/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module RailsHeroicon
class HelperTest < Minitest::Test
include ::RailsHeroicon::Helper
include RunInitializer

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
30 changes: 30 additions & 0 deletions test/rails_heroicon/railtie_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "test_helper"

module RailsHeroicon
class RailtieTest < Minitest::Test
include RunInitializer

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
35 changes: 35 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,38 @@
require "rails_heroicon"

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

module RunInitializer
def before_setup
save_cache_store
end

def setup
run_initializer
end

def teardown
reset_cache_store
run_initializer
end

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