Last active
April 26, 2017 10:24
-
-
Save thinkclay/1931c0715eb6a276dd008305e60ef887 to your computer and use it in GitHub Desktop.
Rails Mailer Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/mailer_injection.rb | |
# This allows `request` to be accessed from ActionMailer Previews | |
# And @request to be accessed from rendered view templates | |
# Easy to inject any other variables like current_user here as well | |
module MailerInjection | |
def inject(hash) | |
hash.keys.each do |key| | |
define_method key.to_sym do | |
eval " @#{key} = hash[key] " | |
end | |
end | |
end | |
end | |
class ActionMailer::Preview | |
extend MailerInjection | |
end | |
class ActionMailer::Base | |
extend MailerInjection | |
end | |
class ActionController::Base | |
before_filter :inject_request | |
def inject_request | |
ActionMailer::Preview.inject({ request: request }) | |
ActionMailer::Base.inject({ request: request }) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment