├── Rakefile ├── Gemfile ├── .gitignore ├── lib ├── actionmailer_with_request │ └── version.rb ├── actionmailer-with-request.rb └── actionmailer_with_request.rb ├── CHANGELOG.md ├── actionmailer-with-request.gemspec ├── README.md └── LICENSE.txt /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/actionmailer_with_request/version.rb: -------------------------------------------------------------------------------- 1 | module ActionMailerWithRequest 2 | VERSION = "0.5.0".freeze 3 | end 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | #### Release 0.5.0 4 | 5 | - CHANGED: Minimum Rails version is now 5. 6 | - CHANGED: Minimum Ruby version is now 2.2.2+ as Rails 5. 7 | 8 | #### Release 0.4.0 9 | 10 | - CHANGED: Fix to avoid thread collision. 11 | 12 | #### Release 0.3.0 13 | 14 | - CHANGED: Minimum Rails version is now 3. 15 | 16 | #### Release 0.2.0 17 | 18 | - CHANGED: Minimum Rails version is now 2. 19 | 20 | -------------------------------------------------------------------------------- /lib/actionmailer-with-request.rb: -------------------------------------------------------------------------------- 1 | # This file is required for BC with the existing 2 | # "actionmailer-with-request" Gem. When Ruby (the Rails process) loads 3 | # the library, it attempts to require a file called with the same name 4 | # of the Gem. But because the Gem file is called with underscores, 5 | # it won't be loaded. 6 | # 7 | # The choices were: 8 | # 9 | # 1. rename the existing Gem 10 | # 2. add this hack 11 | # 12 | require 'actionmailer_with_request' 13 | -------------------------------------------------------------------------------- /actionmailer-with-request.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $LOAD_PATH.push File.expand_path("../lib", __FILE__) 3 | require "actionmailer_with_request/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "actionmailer-with-request" 7 | s.version = ActionMailerWithRequest::VERSION 8 | s.authors = ["Simone Carletti"] 9 | s.email = ["weppos@weppos.net"] 10 | s.homepage = "https://github.com/weppos/actionmailer_with_request" 11 | s.summary = "Let's ActionMailer know about the website" 12 | s.description = "Let's ActionMailer know about the request context to avoid having to set a number of defaults manually." 13 | 14 | s.require_paths = ["lib"] 15 | s.files = `git ls-files`.split("\n") 16 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 17 | s.license = 'MIT' 18 | 19 | s.add_dependency "rails", ">= 5" 20 | end 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ActionMailer with Request 2 | 3 | This is a simple plugin that aims to solve the problem to pass a dynamic host for generating URLs in ActionMailer. 4 | 5 | For more information, read my blog post at 6 | http://www.simonecarletti.com/blog/2009/10/actionmailer-and-host-value/ 7 | 8 | 9 | ## Requirements 10 | 11 | - Rails 5 12 | 13 | For an older versions of Ruby use a previous release. 14 | 15 | 16 | ## Installation 17 | 18 | You can install the gem manually: 19 | 20 | ```shell 21 | $ gem install actionmailer-with-request 22 | ``` 23 | 24 | Or use Bundler and define it as a dependency in your `Gemfile`: 25 | 26 | ```ruby 27 | gem 'actionmailer-with-request' 28 | ``` 29 | 30 | 31 | ## Changelog 32 | 33 | See the [CHANGELOG.md](CHANGELOG.md) file for details. 34 | 35 | 36 | ## License 37 | 38 | Copyright (c) 2009-2017 [Simone Carletti](https://simonecarletti.com/). This is Free Software distributed under the MIT license. 39 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2016 Simone Carletti 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /lib/actionmailer_with_request.rb: -------------------------------------------------------------------------------- 1 | require_relative "actionmailer_with_request/version" 2 | 3 | module ActionMailerWithRequest 4 | 5 | module RequestRecorder 6 | def self.included(base) 7 | base.class_eval do 8 | before_action :store_request 9 | end 10 | end 11 | 12 | def store_request 13 | Thread.current["actiondispatch.request"] = request 14 | end 15 | end 16 | 17 | module DefaultUrlOptionsOverride 18 | # Extends ActionMailer#default_url_options capabilities 19 | # by merging the latest request context into the default url options. 20 | # 21 | # Returns the default url options Hash. 22 | def default_url_options(*args) 23 | defaults = {} 24 | request = Thread.current["actiondispatch.request"] 25 | 26 | if request 27 | host = request.host 28 | port = request.port 29 | protocol = request.protocol 30 | standard_port = request.standard_port 31 | 32 | defaults[:protocol] = protocol 33 | defaults[:host] = host 34 | defaults[:port] = port if port != standard_port 35 | end 36 | 37 | super.merge(defaults) 38 | end 39 | end 40 | 41 | module RequestAccess 42 | # Get the current request. This assists in making request-based 43 | # e-mail addresses. For example: 44 | # 45 | # mail :from => "no-reply@#{request.try(:domain) || 'example.com'}", ..... 46 | # 47 | # Remember if the mailer is delivered outside the context of a 48 | # request then this method returns nil. Hence the try(:domain) 49 | # as well as the fallback domain. 50 | def request 51 | Thread.current["actiondispatch.request"] 52 | end 53 | end 54 | 55 | class Railtie < Rails::Railtie 56 | initializer 'actionmailer.with_request' do 57 | ActionController::Base.send :include, ActionMailerWithRequest::RequestRecorder 58 | ActionMailer::Base.prepend ActionMailerWithRequest::DefaultUrlOptionsOverride 59 | ActionMailer::Base.include ActionMailerWithRequest::RequestAccess 60 | end 61 | end 62 | end 63 | --------------------------------------------------------------------------------