Skip to content

Commit

Permalink
refactor: better and more performant svg finder
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Aug 22, 2024
1 parent ba155ce commit 349d5a2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/css/fields/trix.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ trix-toolbar {
.trix-button-group:not(:first-child) {
@apply ml-0;
}
}
}
5 changes: 4 additions & 1 deletion app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def action_data_attributes(action)
end

def action_css_class(action)
"flex items-center px-4 py-3 w-full font-semibold text-sm hover:bg-primary-100 border-b#{is_disabled?(action) ? " text-gray-500" : " text-black"}"
helpers.class_names("flex items-center px-4 py-3 w-full font-semibold text-sm hover:bg-primary-100", {
"text-gray-500": is_disabled?(action),
"text-black": !is_disabled?(action),
})
end
end
11 changes: 8 additions & 3 deletions app/helpers/avo/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module ApplicationHelper
include ::Pagy::Frontend
include Avo::ResourcesHelper

AVO_CACHED_SVGS = {}

def render_license_warning(title: "", message: "", icon: "exclamation")
render partial: "avo/sidebar/license_warning", locals: {
title: title,
Expand Down Expand Up @@ -65,9 +67,12 @@ def svg(file_name, **args)

file_name = "#{file_name}.svg" unless file_name.end_with? ".svg"

with_asset_finder(::Avo::SvgFinder) do
inline_svg file_name, **args
end
inline_svg svg_path(file_name), **args
end

# The path shouldn't change between reboots so we can memoize the paths based on the filename.
def svg_path(file_name)
AVO_CACHED_SVGS[file_name] ||= Avo::SvgFinder.find_asset(file_name).pathname.to_s
end

def input_classes(extra_classes = "", has_error: false)
Expand Down
2 changes: 1 addition & 1 deletion lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def default_logger

file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
file_logger.formatter = proc do |severity, time, progname, msg|
"[Avo] #{time}: #{msg}\n".tap do |i|
"[Avo->] #{time}: #{msg}\n".tap do |i|
puts i
end
end
Expand Down
22 changes: 13 additions & 9 deletions lib/avo/svg_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ def pathname
# Use the found asset
return found_asset if found_asset.present?

paths = [
Rails.root.join("app", "assets", "svgs", @filename).to_s,
Rails.root.join(@filename).to_s,
Avo::Engine.root.join("app", "assets", "svgs", @filename).to_s,
Avo::Engine.root.join("app", "assets", "svgs", "avo", @filename).to_s,
Avo::Engine.root.join("app", "assets", "svgs", "heroicons", "outline", @filename).to_s,
Avo::Engine.root.join(@filename).to_s
]

paths.find do |path|
File.exist? path
end
end

def paths
[
Avo::Engine.root.join(@filename).to_s,
Avo::Engine.root.join("app", "assets", "svgs", "heroicons", "outline", @filename),
Avo::Engine.root.join("app", "assets", "svgs", "avo", @filename),
Avo::Engine.root.join("app", "assets", "svgs", @filename),
Rails.root.join("app", "assets", "svgs", @filename),
Rails.root.join(@filename),
# Add all paths from Rails including engines
*Rails.application.assets.paths.map { |path| Rails.root.join(path, @filename) },
].map(&:to_s).uniq
end

def default_strategy
# If the app uses Propshaft, grab it from there
if defined?(Propshaft)
Expand Down

0 comments on commit 349d5a2

Please sign in to comment.