Skip to content

Fix #144. Add support for Tempfile / ActiveStorage #186

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

javierav
Copy link

If we want to use this gem with an SVG stored in Active Storage, we have to temporarily download the file and then use it with gem helper

module SvgHelper
  def remote_svg(blob, **)
    blob.open do |file|
      inline_svg_tag file, **
    end
  end
end

# in views
<%= remote_svg(user.icon) %>

The problem is that Active Storage uses a temporary file with the Tempfile class that uses DelegateClass over File, so technically it behaves like a File but does not inherit from it, so it could not be used with this gem.

This PR fixes that issue by providing support for Tempfile.

Comment on lines 9 to 7
def self.default_for(object)
case object
when StringIO then ''
when IO then 1
end
object.is_a?(IO) || object.is_a?(StringIO) || object.is_a?(Tempfile)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

hello, I understand that default_for appears not to be used by this library and it is not even tested, but is there a reason to remove it entirely?

Copy link
Author

Choose a reason for hiding this comment

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

I want to remind you that the main reason is that it is not used in the library and it makes no sense to have code that is not used, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

it makes no sense to have code that is not used, right?

I completely agree. However, it’s important to consider possible breaking changes to the public API. That doesn’t seem to be the case here, and since the master branch already includes breaking changes, now is a good time to remove it. Keeping the commit isolated is also better in case it needs a revert

Additionally, the commit message doesn’t clarify the reason for the removal, so it appears to be related to this new functionality.

I’ve separated the removal into its own commit with a clear message and investigated why the code is unnecessary or was never needed, so this PR can just address the ActiveStorage support. Ref: #187.

Copy link
Author

Choose a reason for hiding this comment

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

Great, I think it's a great option. As soon as the other PR is up, I'll update this one!

tagliala added a commit to tagliala/inline_svg that referenced this pull request Jul 20, 2025
This method, introduced in the 4b13f77 commit along with the IOResource
class, is not utilized in either production or test code.

To the best of my understanding, this discrepancy may be attributed to
a copy-and-paste error.

Ref: jamesmartin#186
@tagliala
Copy link
Contributor

@javierav can you please rebase and add an entry in the changelog?

@javierav javierav force-pushed the support-active-storage branch from 2f7df15 to f5d6b15 Compare July 22, 2025 07:44
@javierav javierav changed the title Fix #144. Add support to Tempfile / ActiveStorage Fix #144. Add support for Tempfile / ActiveStorage Jul 22, 2025
@tagliala
Copy link
Contributor

tagliala commented Jul 22, 2025

Thanks. I'm performing some tests. Can you please clarify how to use this feature?

Let's say that I have

class User < ApplicationRecord
  has_one_attached :avatar
end

Rails 8.0

I was expecting

<%= inline_svg_tag(user.avatar) %>

but I'm getting

undefined method 'end_with?' for an instance of ActiveStorage::Attached::One

(which is the same issue on master and the same issue we see at #144)


edit: I guess that the helper in the opening post is mandatory, and this PR only supports Tempfile (which is a requirement for ActiveStorage support), so if you don't have the helper something like

<%= user.avatar.open { |file| inline_svg_tag file } %>

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