Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Update README to show how to init draper for all ActionMailer methods. #141

Merged
merged 1 commit into from

6 participants

@joevandyk

No description provided.

@joevandyk

Alternatively (and this is probably better), Draper could modify ActionMailer::Base and automatically add the proc to the defaults. I'll leave that as an exercise to the reader. :)

@joevandyk

Actually, I have a question about this. After ActionMailer sets the view context, what sets it back to the controller's context?

@steveklabnik steveklabnik merged commit b50fb9c into drapergem:master
@steveklabnik

Merging this for now, we can do the Right Thing later. :)

I am not sure about the answer to your question, actually.

@deefour

I think the line is supposed to be

default 'init-draper' => Proc.new { set_current_view_context }

not

defaults 'init-draper' => Proc.new { set_current_view_context }
@steveklabnik
Owner

You're right! Fixed.

@deefour

I haven't found the cause yet, but (for me at least) putting the set_current_view_context into a proc at class level causes the following error on 0.11.1 (tried git master repo too)

NoMethodError: undefined method `index' for #<#<Class:0x007fbb6b4f5bb8>:0x007fbb6b3a5790>
from /path/to/.rvm/gems/ruby-1.9.2-p290@my-gemset/gems/mail-2.4.4/lib/mail/encodings.rb:117:in `value_decode'

I'm not opening this as a separate ticket, as I'm not yet convinced I'm not simply missing something. Putting set_current_view_context directly into the mailer methods works as expected.

@stevenharman

@deefour I'm seeing the same thing on. Running on Rails 3.1

undefined method `index' for #<#<Class:0x007fc0918220e0>:0x007fc092d99fe0>
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/encodings.rb:117:in `value_decode'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/encodings.rb:101:in `decode_encode'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/fields/unstructured_field.rb:74:in `do_decode'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/fields/unstructured_field.rb:56:in `decoded'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/fields/unstructured_field.rb:107:in `wrapped_value'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/fields/unstructured_field.rb:70:in `do_encode'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/fields/unstructured_field.rb:52:in `encoded'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/field.rb:123:in `method_missing'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/header.rb:190:in `block in encoded'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/header.rb:189:in `each'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/header.rb:189:in `encoded'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/message.rb:1708:in `encoded'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.4/lib/action_mailer/base.rb:451:in `set_payload_for_mail'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.4/lib/action_mailer/base.rb:431:in `block in deliver_mail'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.1.4/lib/active_support/notifications.rb:53:in `block in instrument'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.1.4/lib/active_support/notifications.rb:53:in `instrument'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.4/lib/action_mailer/base.rb:430:in `deliver_mail'
/Users/steven/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/mail-2.3.3/lib/mail/message.rb:230:in `deliver'

Can someone else confirm that this does work? If so, we should open a new issue.

@steveklabnik

I'm talking with Rails Core about what to do in this case. Turns out the view context stuff is kinda complicated. See my comment here.

@dlangevin

I'm getting this using Rails 3.1.4 and Draper 0.12.3 What it looks like is that ActionMailer is adding init-draper as a header with the proc as the value and dies when it tries to encode the proc, thinking it is a string.

Any ideas?

@steveklabnik
Owner

I'm not sure, Rails 3.1.x isn't really supported for anything but security fixes anymore (by Rails Core), so...

@dlangevin

Fair enough.

FYI, that method of setting the view context struck me as a little weird. You're basically co-opting a method that's used to set default headers and using it to evaluate a block in the context of an instance of ActionMailer::Base.

As a quick fix in my app, I just overrode #mail and added this behavior

class ApplicationMailer < ActionMailer::Base
  def mail(*args, &block)
    # Draper requires that we set the current_view_context
    self.set_current_view_context 
    super
  end
end
@steveklabnik
Owner

Yeah, it is the current Right Way To Do It, but Rails is a bit awkward in this respect. Working on it.

@calebthompson

Also running into @deefour's issue with undefined method 'index' on Rails 3.2.1. FWIW.

/cc @steveklabnik

Ended up working around the issue with @dlangevin's #mail override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 6 deletions.
  1. +2 −6 Readme.markdown
View
8 Readme.markdown
@@ -235,15 +235,11 @@ Use the new methods in your views like any other model method (ex: `@article.pub
### Using in Mailers
To use decorators in mailers that use helpers, you have to call `set_current_view_context` in your
-mailer method:
+ActionMailer class.
```ruby
class ArticleMailer < ActionMailer::Base
- def new_article(article)
- set_current_view_context
- @article_decorator = ArticleDecorator.decorate(article)
- mail(:to => 'come@me.bro', :subject => "New Article: #{@article_decorator.title}")
- end
+ defaults 'init-draper' => Proc.new { set_current_view_context }
end
```
### Integration with RSpec
Something went wrong with that request. Please try again.