From 286283d3446398821ab752771c52acd7f0b1bfe3 Mon Sep 17 00:00:00 2001 From: maful Date: Thu, 22 Jun 2023 21:47:26 +0700 Subject: [PATCH] feat: add support for Rails out of the box --- .rubocop.yml | 4 ++++ Gemfile | 2 ++ README.md | 8 ++++++- Rakefile | 3 +-- lib/phosphor_icons.rb | 2 ++ lib/phosphor_icons/helper.rb | 29 +++++++++++++++++++++++ lib/phosphor_icons/railtie.rb | 11 +++++++++ phosphor_icons.gemspec | 4 +++- test/test_phosphor_helper.rb | 44 +++++++++++++++++++++++++++++++++++ 9 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 lib/phosphor_icons/helper.rb create mode 100644 lib/phosphor_icons/railtie.rb create mode 100644 test/test_phosphor_helper.rb diff --git a/.rubocop.yml b/.rubocop.yml index 88053e93..20d2ef55 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,4 +3,8 @@ inherit_gem: AllCops: NewCops: enable + SuggestExtensions: false TargetRubyVersion: 2.7 + +Gemspec/OrderedDependencies: + Enabled: false diff --git a/Gemfile b/Gemfile index 2f1cbcd0..a7ccebe4 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,8 @@ source "https://rubygems.org" gemspec +gem "rails" + group :development, :test do gem "minitest", "~> 5.18" gem "rake", "~> 13.0" diff --git a/README.md b/README.md index 79d24b6d..3a165a35 100644 --- a/README.md +++ b/README.md @@ -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/). @@ -30,6 +30,12 @@ icon.to_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. diff --git a/Rakefile b/Rakefile index 2049f956..37445b18 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/phosphor_icons.rb b/lib/phosphor_icons.rb index 9367ad80..2d1e5610 100644 --- a/lib/phosphor_icons.rb +++ b/lib/phosphor_icons.rb @@ -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 diff --git a/lib/phosphor_icons/helper.rb b/lib/phosphor_icons/helper.rb new file mode 100644 index 00000000..7c8bcb0a --- /dev/null +++ b/lib/phosphor_icons/helper.rb @@ -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 diff --git a/lib/phosphor_icons/railtie.rb b/lib/phosphor_icons/railtie.rb new file mode 100644 index 00000000..88d66928 --- /dev/null +++ b/lib/phosphor_icons/railtie.rb @@ -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 diff --git a/phosphor_icons.gemspec b/phosphor_icons.gemspec index 6e0759e2..691462b6 100644 --- a/phosphor_icons.gemspec +++ b/phosphor_icons.gemspec @@ -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 diff --git a/test/test_phosphor_helper.rb b/test/test_phosphor_helper.rb new file mode 100644 index 00000000..81fd1685 --- /dev/null +++ b/test/test_phosphor_helper.rb @@ -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{.*}, phosphor_icon("alarm")) + end + + def test_has_a_path + assert_match(/ 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