Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Last active April 26, 2017 10:24
Show Gist options
  • Save thinkclay/1931c0715eb6a276dd008305e60ef887 to your computer and use it in GitHub Desktop.
Save thinkclay/1931c0715eb6a276dd008305e60ef887 to your computer and use it in GitHub Desktop.
Rails Mailer Injection
# 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