From 883c1088cd1209ee1f1c613a02d152488308d8a8 Mon Sep 17 00:00:00 2001 From: Andy Leverenz Date: Tue, 17 Sep 2024 15:58:23 -0500 Subject: [PATCH] Improve custom_path option --- lib/railsui_icon.rb | 21 --------------------- lib/railsui_icon/icon.rb | 28 ++++++++++++++++++---------- lib/railsui_icon/version.rb | 2 +- 3 files changed, 19 insertions(+), 32 deletions(-) delete mode 100644 lib/railsui_icon.rb diff --git a/lib/railsui_icon.rb b/lib/railsui_icon.rb deleted file mode 100644 index e70afcb..0000000 --- a/lib/railsui_icon.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "railsui_icon/version" -require "railsui_icon/engine" -require "railsui_icon/railtie" -require "railsui_icon/helpers" -require "railsui_icon/icon" -require "railsui_icon/configuration" - -module RailsuiIcon - class << self - attr_accessor :configuration - - def configure - self.configuration ||= Configuration.new - yield(configuration) - end - - def root - File.dirname(__dir__) - end - end -end diff --git a/lib/railsui_icon/icon.rb b/lib/railsui_icon/icon.rb index f16dda4..5d63dd6 100644 --- a/lib/railsui_icon/icon.rb +++ b/lib/railsui_icon/icon.rb @@ -43,21 +43,18 @@ def render_standard_icon def render_custom_path(custom_path) return warning if custom_path.blank? - file_name = File.basename(URI.parse(custom_path).path) - asset_path = nil + # Strip leading slashes from custom_path + sanitized_path = custom_path.sub(%r{^/}, '') - if defined?(Railsui::Engine) && Railsui::Engine.root.present? - engine_asset_path = Railsui::Engine.root.join("app/assets/images", file_name) - asset_path = engine_asset_path if File.exist?(engine_asset_path) - end + # Extract file name from sanitized_path + file_name = File.basename(URI.parse(sanitized_path).path) - unless asset_path - app_asset_path = Rails.root.join("app/assets/images", file_name) - asset_path = app_asset_path.to_s if File.exist?(app_asset_path) - end + # Attempt to find the asset in subdirectories of app/assets/images + asset_path = find_asset_path(sanitized_path) raise ArgumentError, "Asset path cannot be found" if asset_path.nil? + # Read the SVG content svg_content = File.read(asset_path) doc = Nokogiri::HTML::DocumentFragment.parse(svg_content) svg = doc.at_css('svg') @@ -73,6 +70,17 @@ def render_custom_path(custom_path) warning end + private + + def find_asset_path(file_name) + # Check in the main app/assets/images directory + path = Rails.root.join('app/assets/images', file_name) + return path.to_s if File.exist?(path) + + # Check in any subdirectories within app/assets/images + Dir[Rails.root.join('app/assets/images/**/*', file_name)].first + end + def file_path File.join(RailsuiIcon.root, "lib/railsui_icon/icons/#{variant}/#{name}.svg") end diff --git a/lib/railsui_icon/version.rb b/lib/railsui_icon/version.rb index 841da3d..f06f8ab 100644 --- a/lib/railsui_icon/version.rb +++ b/lib/railsui_icon/version.rb @@ -1,3 +1,3 @@ module RailsuiIcon - VERSION = "1.0.9" + VERSION = "1.1.0" end