Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for use in Rails engines #50

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed docs/rails_designer_icon.jpg
Binary file not shown.
15 changes: 2 additions & 13 deletions lib/rails_icons/icon.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "rails_icons/icon/file_path"
require "rails_icons/icon/attributes"

class RailsIcons::Icon
Expand Down Expand Up @@ -35,19 +36,7 @@ def error_message
"Icon not found: `#{attributes.join(" / ")}`"
end

def file_path
return RailsIcons::Engine.root.join("app", "assets", "svg", "rails_icons", "icons", "animated", "#{@name}.svg") if @library.animated?
return Rails.root.join(custom_library.dig(:path), "#{@name}.svg") if custom_library?

parts = [
RailsIcons.configuration.destination_path,
@library,
@variant,
"#{@name}.svg"
].compact_blank!

Rails.root.join(*parts)
end
def file_path = RailsIcons::Icon::FilePath.new(name: @name, library: @library, variant: @variant).call

def attach_attributes(to:)
RailsIcons::Icon::Attributes
Expand Down
57 changes: 57 additions & 0 deletions lib/rails_icons/icon/file_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module RailsIcons
class Icon
class FilePath
def initialize(name:, library:, variant:)
@name = name
@library = library
@variant = variant
end

def call
return animated_icons_path if @library.animated?
return custom_library_path if @library.custom?

icon_path = icons_path_in_app || icons_path_in_engines

raise RailsIcons::NotFound if icon_path.nil?

icon_path
end

private

def animated_icons_path = RailsIcons::Engine.root.join("app", "assets", "svg", "rails_icons", "icons", "animated", "#{@name}.svg")

def custom_library_path = Rails.root.join(@library.custom_path, "#{@name}.svg")

def icons_path_in_app
path = app_path

path if File.exist?(path)
end

def icons_path_in_engines
path = nil

Rails::Engine.subclasses.find do |engine|
path = engine.root.join(*parts)

path if File.exist?(path)
end

path
end

def app_path = Rails.root.join(*parts)

def parts
[
RailsIcons.configuration.destination_path,
@library,
@variant,
"#{@name}.svg"
].compact_blank!
end
end
end
end
Loading