1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

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