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

Using #inline_html with a Rails view context #35

Closed
ajb opened this issue Nov 30, 2015 · 2 comments
Closed

Using #inline_html with a Rails view context #35

ajb opened this issue Nov 30, 2015 · 2 comments

Comments

@ajb
Copy link

ajb commented Nov 30, 2015

So I'm converting some Erector code to Fortitude. Outside of a normal ActionView context (say, in a model,) we've got a method that looks like this:

def to_table_cell(attachment_ids)
  Erector.inline do
    Attachment.find(attachment_ids).each do |attachment|
      a attachment.upload.raw_filename,
        href: download_attachment_path(attachment),
        target: '_blank'
    end
  end.to_html
end  # => "<a href="/attachment/12/download" target="_blank">myfile.jpg</a>"

I've noticed that Fortitude has an inline_html method, to which you can pass a block. However, it appears that since we're outside of the ActionView context, we don't have access to the download_attachment_path helper that Rails provides.

@ageweke: I've kept up a bit with the recent issue comments about rendering context... but I'm unsure if there's a way you'd recommend to implement this.

It looks like I could probably create an instance of ActionView::Base and somehow use it as my rendering context. I could probably package it up in a nice little method that would be easy to call in the future. But I feel like I'm probably doing something wrong here...

@ageweke
Copy link
Owner

ageweke commented Nov 30, 2015

Hmmm. This is a really difficult problem. While I’ve seen Rails people say “don’t do that in a model!”, I’ve wanted to do exactly the same kind of thing, for the same reason. (It’d be one thing to render a whole wad of HTML in a model, but a list of links is another thing…)

You’re exactly right about the reasons why there is no download_attachment_path helper there. Perhaps take a look at my latest reply (posted just now) to #34: it gives a tiny bit of code that’ll let you supply Fortitude with a helpers object that will give your widget access to whatever helpers you want — assuming you mix in the module (here, some module I know you can get access to that has all of your routes helpers in it).

It strikes me, looking at it, that Fortitude’s inline_html doesn’t let you pass in a RenderingContext for its internal call to to_html. I’ll try to add that, and keep this bug open for that. In the mean time, you can just call SomeWidgetClass.inline_subclass { … }.new(assigns).to_html(my_rendering_context), which does exactly the same thing.

@ageweke
Copy link
Owner

ageweke commented Oct 12, 2016

In 0.9.5, which I just released, #inline_html can take a second argument, a Fortitude::RenderingContext. While it’s impossible for Fortitude to magically make the case above “just work”, for the reasons referenced in here, this should make it about as easy as is possible.

@ageweke ageweke closed this as completed Oct 12, 2016
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

No branches or pull requests

2 participants