-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Rails out of the box
- Loading branch information
Showing
9 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_view" | ||
|
||
module PhosphorIcons | ||
module Helper | ||
include ActionView::Helpers::TagHelper | ||
|
||
mattr_accessor :phosphor_icons_helper_cache, default: {} | ||
|
||
# rubocop:disable Style/IdenticalConditionalBranches | ||
def phosphor_icon(symbol, options = {}) | ||
return "" if symbol.nil? | ||
|
||
cache_key = [symbol, options] | ||
|
||
if (tag = phosphor_icons_helper_cache[cache_key]) | ||
tag | ||
else | ||
icon = PhosphorIcons::Icon.new(symbol, options) | ||
|
||
tag = content_tag(:svg, icon.path.html_safe, icon.options).freeze | ||
phosphor_icons_helper_cache[cache_key] = tag | ||
tag | ||
end | ||
end | ||
# rubocop:enable Style/IdenticalConditionalBranches | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails" | ||
|
||
module PhosphorIcons | ||
class Railtie < Rails::Railtie | ||
initializer "phosphor_icons_helper.helper" do | ||
ActionView::Base.send(:include, PhosphorIcons::Helper) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestPhosphorHelper < Minitest::Test | ||
include PhosphorIcons::Helper | ||
|
||
def test_renders_nothing_when_no_symbol_is_passed_in | ||
assert_equal("", phosphor_icon(nil)) | ||
end | ||
|
||
def test_renders_the_svg | ||
assert_match(%r{<svg.*phosphor-icon.*>.*</svg>}, phosphor_icon("alarm")) | ||
end | ||
|
||
def test_has_a_path | ||
assert_match(/<path/, phosphor_icon("alarm")) | ||
end | ||
|
||
def test_caches_svgs | ||
PhosphorIcons::Helper.phosphor_icons_helper_cache = {} | ||
|
||
mock = Minitest::Mock.new | ||
# rubocop:disable Style/ClassVars | ||
def mock.path | ||
@@call_count ||= 0 | ||
@@call_count += 1 | ||
|
||
raise "PhosphorIcons library called twice" if @@call_count > 1 | ||
|
||
"foo" | ||
end | ||
# rubocop:enable Style/ClassVars | ||
|
||
def mock.options; end | ||
|
||
PhosphorIcons::Icon.stub(:new, mock) do | ||
phosphor_icon("alarm") | ||
phosphor_icon("alarm") | ||
end | ||
|
||
PhosphorIcons::Helper.phosphor_icons_helper_cache = {} | ||
end | ||
end |