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

Set asset finder from callable #154

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Conversation

xymbol
Copy link
Contributor

@xymbol xymbol commented Oct 24, 2023

It fixes #151 by allowing the asset finder to be a callable object. This is useful when the asset pipeline is not ready when the initializer is run, for example, when using Propshaft.

I don't know if this is the best way to solve this problem. On the one hand, Propshaft could be done when its initializer is run, but on the other hand, this gem's railtie assumes the asset pipeline is ready.

@@ -51,7 +59,6 @@ def asset_finder=(finder)
# See: https://github.com/jamesmartin/inline_svg/issues/25
InlineSvg::StaticAssetFinder
end
asset_finder
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return values are ignored in setter methods.

if config.asset_finder.nil?
# In default Rails apps, this will be a fully operational
# Sprockets::Environment instance
config.asset_finder = app.instance_variable_get(:@assets)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to call the #assets method here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to deal with older versions of Rails (probably back to 3.x) when these things did not all quack like ducks. May not be necessary with the supported versions of Rails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced it with a method call.

lib/inline_svg.rb Outdated Show resolved Hide resolved
It fixes jamesmartin#151 by allowing the asset finder to be a callable object. This is
useful when the asset pipeline is not ready when the initializer is run, for
example, when using Propshaft.

I don't know if this is the best way to solve this problem. On the one hand,
Propshaft could be done when its initializer is run, but on the other hand,
this gem's railtie assumes the asset pipeline is ready.
Copy link
Owner

@jamesmartin jamesmartin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. ✨

I don't know if this is the best way to solve this problem. On the one hand, Propshaft could be done when its initializer is run, but on the other hand, this gem's railtie assumes the asset pipeline is ready

I'm not sure either. Assuming the asset pipeline is ready has been reliable until now. I don't have a better suggestion to solve this problem right now. 🤷

if config.asset_finder.nil?
# In default Rails apps, this will be a fully operational
# Sprockets::Environment instance
config.asset_finder = app.instance_variable_get(:@assets)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to deal with older versions of Rails (probably back to 3.x) when these things did not all quack like ducks. May not be necessary with the supported versions of Rails.

lib/inline_svg.rb Outdated Show resolved Hide resolved
@@ -81,6 +88,11 @@ def incompatible_transformation?(klass)
!klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform)
end

def set_asset_finder_from_callable
while @asset_finder&.respond_to?(:call)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the significance of the while here? 🤔

Copy link
Contributor Author

@xymbol xymbol Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle a callable returning a callable. I can replace it with a conditional only to call the object once or add a spec to describe the intention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example.

@yshmarov
Copy link

Hi guys!
Just wanted to let you know - right now, the only way it works for me with Propshaft, is if I add this:

# config/initializers/inline_svg.rb
InlineSvg.configure do |config|
  config.asset_finder = InlineSvg::PropshaftAssetFinder
end

@xymbol
Copy link
Contributor Author

xymbol commented Oct 25, 2023

… right now, the only way it works for me with Propshaft, is if I add this…

@yshmarov So, this branch does not work with your app? Did you change the order in your Gemfile, as mentioned in #151?

I have a Propshaft app working with this change. Still, I'm unconvinced by the initializer approach and would rather have a set of strategies to detect the asset pipeline lazily.

I'd be happy to explore that.

@xymbol xymbol marked this pull request as ready for review October 27, 2023 18:28
@xymbol
Copy link
Contributor Author

xymbol commented Oct 27, 2023

@jamesmartin I drafted a more flexible approach in #155, which still needs to be completed, where we don't rely on the initializer and lazily detect which asset finder adapter matches the environment unless the user sets one explicitly.

I can clean up and mark either one as ready for review, depending on your preference.

@xymbol xymbol marked this pull request as draft October 27, 2023 18:31
@jamesmartin
Copy link
Owner

Thanks for continuing to push this forward. I haven't had the chance to properly think about the implications of this change. I'll try to get back to you soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Propshaft detection is load order dependant
3 participants