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

Support both Sprockets and Webpacker #103

Merged
merged 6 commits into from
Nov 13, 2019

Conversation

kylefox
Copy link
Contributor

@kylefox kylefox commented Oct 30, 2019

See discussion at #102 for context.

This PR introduces two new helpers inline_svg_tag and inline_svg_pack_tag which will search for SVG files using Sprockets and Webpacker, respectively.

The logic for finding and rendering an SVG has been moved into a private method, render_inline_svg.

The inline_svg helper will continue to function as expected (uses any configured asset_finder), but now raises a deprecation warning:

DEPRECATION WARNING:
  `inline_svg` is deprecated and will be removed from inline_svg 2.0
  (use `inline_svg_tag` or `inline_svg_pack_tag` instead)

However, because the behavior of inline_svg is effectively unchanged, there may be no need to deprecate it at all!

How it works

Under the hood we have a new method, with_asset_finder, which is passed a finder class (or nil) and a block. Calls to render_inline_svg within the block will use the finder passed to with_asset_finder.

This allows us to temporarily change InlineSvg.configuration.asset_finder for the duration of the block, which is how we're able to keep the existing inline_svg helper backwards-compatible.

Disclaimer: I have limited familiarity with the internal guts of this gem, so it's entirely possible that altering InlineSvg.configuration.asset_finder as I've done has side-effects on other parts of the gem's functionality. All the tests still pass, so I'm hoping there are no side-effects I've overlooked 🤞

I'll add tests for everything if this general approach is approved 👍

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.

@kylefox thanks for pulling this together. I like this approach as a stepping stone to completely deprecating the inline_svg method (at least publicly) in v2.0.

lib/inline_svg/action_view/helpers.rb Outdated Show resolved Hide resolved
@kylefox
Copy link
Contributor Author

kylefox commented Nov 5, 2019

@jamesmartin I updated the README and added tests for the new helpers.

(Actually, I didn't add any new tests. I extracted the existing examples into shared examples and used them to test all three helpers. Open to suggestions if you have other ideas for how to test this).

One thing I'm curious about in general — what other use-cases have you seen for using a custom asset_finder, besides webpacker support?

@jamesmartin
Copy link
Owner

One thing I'm curious about in general — what other use-cases have you seen for using a custom asset_finder, besides webpacker support?

One concrete use-case is in situations where development versus production differ. For example, when caching all SVG assets at Rails boot time it's useful to implement a separate asset finder for use in development mode to allow dynamic reloading from disk.

@jamesmartin jamesmartin merged commit 4d4baa9 into jamesmartin:master Nov 13, 2019
@jamesmartin jamesmartin mentioned this pull request Nov 13, 2019
3 tasks
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.

2 participants