Skip to content

Commit

Permalink
Improve custom_path option
Browse files Browse the repository at this point in the history
  • Loading branch information
justalever committed Sep 17, 2024
1 parent 63ecb40 commit 883c108
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
21 changes: 0 additions & 21 deletions lib/railsui_icon.rb

This file was deleted.

28 changes: 18 additions & 10 deletions lib/railsui_icon/icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/railsui_icon/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsuiIcon
VERSION = "1.0.9"
VERSION = "1.1.0"
end

0 comments on commit 883c108

Please sign in to comment.