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

Cells in mailer views #14

Open
Startouf opened this issue Nov 29, 2016 · 8 comments
Open

Cells in mailer views #14

Startouf opened this issue Nov 29, 2016 · 8 comments

Comments

@Startouf
Copy link

Startouf commented Nov 29, 2016

What's the best way to add cells in my mailers ?

The cellmethod is not recognized in my mailers, I can still call the cells by doing.

MyCell.new(@myObject).cell_view.html_safe

What's the proper way to include cells in Rails mailers, so I can call cell(:my_cell, @my_object).(:cell_view)

Or is there an other way to call a cell and run the builder code to use the appropariate inherited cell.

@apotonick
Copy link
Member

@Startouf
Copy link
Author

Startouf commented Nov 29, 2016

So you're suggesting I should make a PR with similar code for ActionMailer ? (The code you pointed to only injects into ActionController and it's view_context but not ActionMailer)

And otherwise sorry, I was calling MyCell.new(@myObject).method instead of using the call syntax MyCell.().(), which is why I didn't get the builder resolution thingy...

@apotonick
Copy link
Member

Uhm, yeah, why not, I actually never thought of the ActionMailer patch. Is that really necessary, though??

@Startouf
Copy link
Author

Well just now I was telling someone about the awesomeness of cells, and that we could also use it for Mailer views, then we got stuck for 30 minutes because cell(xx) wasn't working and we tried different combinations of helper Cell:xxx into ApplicationMailer which weren't working

@apotonick
Copy link
Member

Yeah but then you run into the next problem, what will be the parent controller in a mailer, and so on...

The Cell.() works everywhere since it's pure Ruby.

@apotonick
Copy link
Member

But I agree, Cells are awesome! 😆

@Startouf
Copy link
Author

Startouf commented Aug 9, 2017

Hey I still have some problems in cells in mailers with URL helpers.
cells-rails include a default parent_controller in the context that is used to resolve link_to links. When calling the raw Cell with MyCell.call(@myObject).call(:view_name) this property is not passed and calling link_to in mailer views cause this problem

cells-rails-0.0.6/lib/cell/rails.rb:58:in `parent_controller'
actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:228:in `call'
actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:177:in `call'
actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:295:in `block (2 levels) in define_url_helper'
app/cells/mailer/appointment/line_that_calls_link_to

My current solution is not to call directly link_to but only use directly the url_helper in the href attribute <a href="<%= my_resource_url(model) %>

The following seems to be the only way to make it work

class MyCell
    def url_helpers
      Rails.application.routes.url_helpers
    end
    delegate(
      :my_url_helper_url,
      to: :url_helpers
    )

@Startouf
Copy link
Author

This problem is still causing me headaches whenever I call new url_helpers in my mailer cell view and those are not delegated using the trick mentioned above. The stack trace also remains enigmatic so it took me some time to remember I had to re-delegate the helpers in my cell.rb file.
I've noticed there was a TRB repository with cells made especially for mailers, but that's not something I can use right now and it feels like it was quite abandonned... ?
Also I just watched the Ruby is Dead talk and I can't help but wink @apotonick https://youtu.be/x0JlOxdXA_g?t=23m28s

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