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

Add automatic support for Grape entities #17

Closed
andyw8 opened this issue Nov 30, 2012 · 7 comments
Closed

Add automatic support for Grape entities #17

andyw8 opened this issue Nov 30, 2012 · 7 comments

Comments

@andyw8
Copy link
Contributor

andyw8 commented Nov 30, 2012

I'm trying to cache the output generated from a custom entity:

get '/products/:id' do
  cache do
    product = Product.find params[:id]
    present product, with: Entities::Product
  end
end

But this fails with:

no _dump_data is defined for class StringIO

I guess the stream somehow needs to be converted to a String first?

@fancyremarker
Copy link
Contributor

Hi @andyw8, yes, converting to a String would be one way to work around this. At the core of this issue is the fact that ActiveSupport::Cache::Store doesn't support serializing StringIO objects. Off the top of my head, I think the only way we would solve this in Garner would be to detect cache objects of type StringIO, and then:

  1. Call read on the object before storing.
  2. Annotate the cache object to indicate that it was originally of type StringIO (something we don't do currently).
  3. Re-wrap the cached string in a StringIO object on fetching.

I couldn't find much on the topic, but the fact that ActiveSupport::Cache::Store doesn't do this by default leads me to believe it might not be prudent.

An alternative is to only cache the product = Product.find params[:id], and to present the product outside the cache block. But this would incur the cost of re-presenting on each request.

@dblock
Copy link
Contributor

dblock commented Nov 30, 2012

We could also build support for StringIO when defined in Garner proper within the Grape extension. So this issue can be renamed to "support Grape Entity presenters". I think it would be as simple as checking that the type is StringIO and calling to_s on it.

@fancyremarker
Copy link
Contributor

Going to close and mark this one wontfix, since Garner v0.4.0+ defers this sort of decision to the underlying cache/marshaling implementation.

@andoriyu
Copy link

So what is the work around this?

@dblock
Copy link
Contributor

dblock commented Jun 27, 2014

@andoriyu It depends on your scenario. If you have a StringIO in the garner block, add a to_s to it, for example.

@andoriyu
Copy link

I figured it out. Maybe makes sense to add this to documentation:

            garner.bind(@song) do
              song = present @song, with: ::AdminSongPresenter
              song.as_json
            end

@dblock
Copy link
Contributor

dblock commented Jun 27, 2014

I'd welcome any README change that makes sense :)

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

No branches or pull requests

4 participants