Skip to content

Commit

Permalink
feat: add support for Rails out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
maful committed Jun 22, 2023
1 parent 871f074 commit 286283d
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ inherit_gem:

AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 2.7

Gemspec/OrderedDependencies:
Enabled: false
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ source "https://rubygems.org"

gemspec

gem "rails"

group :development, :test do
gem "minitest", "~> 5.18"
gem "rake", "~> 13.0"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Ruby Phosphor Icons

A gem to easily make use of [Phosphor Icons](https://phosphoricons.com) in Ruby and Rails apps.
A gem to easily include [Phosphor Icons](https://phosphoricons.com) in your Ruby and Rails apps.

For a full list of available icons see [the assets directory](https://github.com/phosphor-icons/core/tree/c67d7a849f450be1bfe64fd5820471e4019e5ff0/assets) or preview them at [phosphoricons.com](https://phosphoricons.com/).

Expand All @@ -30,6 +30,12 @@ icon.to_svg
# <svg class="phosphor-icon" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" fill="currentColor"><path d="M128,32a96,96,0,1,0,96,96A96.11,96.11,0,0,0,128,32Zm0,176a80,80,0,1,1,80-80A80.09,80.09,0,0,1,128,208ZM61.66,29.66l-32,32A8,8,0,0,1,18.34,50.34l32-32A8,8,0,1,1,61.66,29.66Zm176,32a8,8,0,0,1-11.32,0l-32-32a8,8,0,0,1,11.32-11.32l32,32A8,8,0,0,1,237.66,61.66ZM184,120a8,8,0,0,1,0,16H128a8,8,0,0,1-8-8V72a8,8,0,0,1,16,0v48Z"/></svg>
```

If you are using Ruby on Rails, you can use `phosphor_icon` helper in your views directly

```erb
<%= phosphor_icon "alarm", class: "h-5 w-5 %>
```

## Documentation

The `Icon` class takes two arguments. The first is the symbol of the icon, and the second is a hash of arguments representing html attributes.
Expand Down
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ require "rubocop/rake_task"
Rake.add_rakelib("lib/tasks")

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.libs = ["lib", "test"]
t.test_files = FileList["test/**/test_*.rb"]
end

Expand Down
2 changes: 2 additions & 0 deletions lib/phosphor_icons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "phosphor_icons/version"
require "phosphor_icons/icon"
require "phosphor_icons/helper"
require "phosphor_icons/railtie" if defined?(Rails)
require "json"

module PhosphorIcons
Expand Down
29 changes: 29 additions & 0 deletions lib/phosphor_icons/helper.rb
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
11 changes: 11 additions & 0 deletions lib/phosphor_icons/railtie.rb
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
4 changes: 3 additions & 1 deletion phosphor_icons.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ Gem::Specification.new do |spec|
spec.files = Dir["{lib}/**/*"] + ["LICENSE", "README.md"]
spec.require_paths = ["lib"]

spec.add_runtime_dependency("ox", "~> 2.14")
spec.add_dependency("ox", "~> 2.14")
spec.add_dependency("railties")
spec.add_dependency("actionview")
end
44 changes: 44 additions & 0 deletions test/test_phosphor_helper.rb
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

0 comments on commit 286283d

Please sign in to comment.