Include Draper::ViewContextFilter in ActionMailer::Base

* Only send the :before_filter method if it exists
* This will allow decorators to be used when rendering email
This commit is contained in:
Benny Wong 2011-10-28 13:16:25 -04:00
parent d72f2a1ee8
commit c56ac5ed2c
3 changed files with 22 additions and 6 deletions

View File

@ -229,6 +229,21 @@ Use the new methods in your views like any other model method (ex: `@article.pub
<h1><%= @article.title %> <%= @article.published_at %></h1>
```
### Using in Mailers
To use decorators in mailers that use helpers, you have to call `set_current_view_context` in your
mailer method:
```ruby
class ActicleMailer < 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
end
```
## Possible Decoration Methods
Here are some ideas of what you might do in decorator methods:

View File

@ -1,7 +1,8 @@
module Draper
class System
class System
def self.setup
ActionController::Base.send(:include, Draper::ViewContextFilter) if defined?(ActionController::Base)
ActionMailer::Base.send(:include, Draper::ViewContextFilter) if defined?(ActionMailer::Base)
end
end
end
end

View File

@ -8,14 +8,14 @@ module Draper
Thread.current[:current_view_context] = input
end
end
module ViewContextFilter
def set_current_view_context
Draper::ViewContext.current = self.view_context
end
def self.included(source)
source.send(:before_filter, :set_current_view_context)
source.send(:before_filter, :set_current_view_context) if source.respond_to?(:before_filter)
end
end
end
end