Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 8d4811d69d24831ef43ed5cd55e705b93093ddb7 0 parents
@henning-koch henning-koch authored
Showing with 435 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +20 −0 MIT-LICENSE
  3. +24 −0 README.rdoc
  4. +36 −0 Rakefile
  5. +1 −0  VERSION
  6. +2 −0  lib/mail_magnet.rb
  7. +14 −0 lib/mail_magnet/action_mailer_ext.rb
  8. +27 −0 lib/mail_magnet/tmail_ext.rb
  9. +53 −0 mail_magnet.gemspec
  10. +2 −0  spec/app_root/app/controllers/application_controller.rb
  11. +10 −0 spec/app_root/app/models/mailer.rb
  12. +1 −0  spec/app_root/app/views/mailer/letter.erb
  13. +114 −0 spec/app_root/config/boot.rb
  14. +21 −0 spec/app_root/config/database.yml
  15. +14 −0 spec/app_root/config/environment.rb
  16. 0  spec/app_root/config/environments/in_memory.rb
  17. 0  spec/app_root/config/environments/mysql.rb
  18. 0  spec/app_root/config/environments/postgresql.rb
  19. 0  spec/app_root/config/environments/sqlite.rb
  20. 0  spec/app_root/config/environments/sqlite3.rb
  21. +4 −0 spec/app_root/config/routes.rb
  22. +4 −0 spec/app_root/lib/console_with_fixtures.rb
  23. +1 −0  spec/app_root/log/.gitignore
  24. +7 −0 spec/app_root/script/console
  25. +48 −0 spec/mail_magnet_spec.rb
  26. +21 −0 spec/spec_helper.rb
  27. +2 −0  spec/support/rcov.opts
  28. +4 −0 spec/support/spec.opts
5 .gitignore
@@ -0,0 +1,5 @@
+doc
+pkg
+*.gem
+.idea
+
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Henning Koch
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 README.rdoc
@@ -0,0 +1,24 @@
+= Mail Magnet
+
+Mail Magnet allows you to override e-mail recipients in ActionMailer, e.g. for testing purposes.
+
+http://gem-session.com/2010/06/overriding-e-mail-recipients-in-actionmailer
+
+== Installation
+
+Install the gem with
+ sudo gem install mail_magnet
+
+....
+
+== Rails 3 compatibility
+
+We cannot guarantee Rails 3 compatibility at this point, but we will upgrade the gem when Rails 3 is released.
+
+=== Credits
+
+Arne Hartherz
+
+{makandra.com}[http://makandra.com/]
+
+{gem-session.com}[http://gem-session.com/]
36 Rakefile
@@ -0,0 +1,36 @@
+require 'rake'
+require 'rake/rdoctask'
+require 'spec/rake/spectask'
+
+desc 'Default: Run all specs.'
+task :default => :spec
+
+desc "Run all specs"
+Spec::Rake::SpecTask.new() do |t|
+ t.spec_opts = ['--options', "\"spec/support/spec.opts\""]
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+desc 'Generate documentation for the mail_magnet gem.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'mail_magnet'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "mail_magnet"
+ gemspec.summary = "Override ActionMailer recipients so all mails go to a given address"
+ gemspec.email = "github@makandra.com"
+ gemspec.homepage = "http://github.com/makandra/mail_magnet"
+ gemspec.description = "Override ActionMailer recipients so all mails go to a given address"
+ gemspec.authors = ["Arne Hartherz"]
+ end
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
+end
+
1  VERSION
@@ -0,0 +1 @@
+0.1.0
2  lib/mail_magnet.rb
@@ -0,0 +1,2 @@
+require 'mail_magnet/action_mailer_ext'
+require 'mail_magnet/tmail_ext'
14 lib/mail_magnet/action_mailer_ext.rb
@@ -0,0 +1,14 @@
+ActionMailer::Base.class_eval do
+
+ @@override_recipients = nil
+ cattr_accessor :override_recipients
+
+ def deliver_with_override!(mail = @mail)
+ if override_recipients.present?
+ mail.override_recipients! override_recipients
+ end
+ deliver_without_override! mail
+ end
+ alias_method_chain :deliver!, :override
+
+end
27 lib/mail_magnet/tmail_ext.rb
@@ -0,0 +1,27 @@
+TMail::Mail.class_eval do
+
+ def override_recipients!(recipients)
+ recipients = Array(recipients)
+
+ original_addresses = {
+ :to => override(:to, recipients),
+ :cc => override(:cc, recipients),
+ :bcc => override(:bcc, recipients)
+ }
+
+ parts = [ "--- Original recipients ---" ]
+ [ :to, :cc, :bcc ].each do |target|
+ parts << target.to_s.capitalize + ": " + (original_addresses[target] ? original_addresses[target].join(', ') : '(none)')
+ end
+ parts << "---------------------------"
+
+ self.body = parts.join("\n") + "\n\n" + self.body
+ end
+
+ def override(method, recipients)
+ original_recipients = send("#{method}")
+ self.send "#{method}=", recipients if original_recipients
+ original_recipients
+ end
+
+end
53 mail_magnet.gemspec
@@ -0,0 +1,53 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE
+# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{mail_magnet}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Arne Hartherz"]
+ s.date = %q{2010-07-23}
+ s.description = %q{Override ActionMailer recipients so all mails go to a given address}
+ s.email = %q{github@makandra.com}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ "lib/mail_magnet/action_mailer_ext.rb",
+ "lib/mail_magnet/tmail_ext.rb",
+ "spec/app_root/app/views/mailer/letter.erb"
+ ]
+ s.homepage = %q{http://github.com/makandra/mail_magnet}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{Override ActionMailer recipients so all mails go to a given address}
+ s.test_files = [
+ "spec/app_root/config/boot.rb",
+ "spec/app_root/config/environment.rb",
+ "spec/app_root/config/routes.rb",
+ "spec/app_root/config/environments/in_memory.rb",
+ "spec/app_root/config/environments/mysql.rb",
+ "spec/app_root/config/environments/postgresql.rb",
+ "spec/app_root/config/environments/sqlite.rb",
+ "spec/app_root/config/environments/sqlite3.rb",
+ "spec/app_root/lib/console_with_fixtures.rb",
+ "spec/app_root/app/controllers/application_controller.rb",
+ "spec/app_root/app/models/mailer.rb",
+ "spec/spec_helper.rb",
+ "spec/mail_magnet_spec.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
2  spec/app_root/app/controllers/application_controller.rb
@@ -0,0 +1,2 @@
+class ApplicationController < ActionController::Base
+end
10 spec/app_root/app/models/mailer.rb
@@ -0,0 +1,10 @@
+class Mailer < ActionMailer::Base
+
+ def letter
+ recipients 'original.to@example.com'
+ cc 'original.cc@example.com'
+ bcc 'original.bcc@example.com'
+ subject 'Hello Universe!'
+ end
+
+end
1  spec/app_root/app/views/mailer/letter.erb
@@ -0,0 +1 @@
+Hello!
114 spec/app_root/config/boot.rb
@@ -0,0 +1,114 @@
+# Allow customization of the rails framework path
+RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
+
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?(RAILS_FRAMEWORK_ROOT)
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ require 'rubygems'
+ min_version = '1.1.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ environment_rb = "#{RAILS_ROOT}/config/environment.rb"
+ environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
+ File.read(environment_rb)
+ end
+ end
+ end
+end
+
+# All that for this:
+Rails.boot!
21 spec/app_root/config/database.yml
@@ -0,0 +1,21 @@
+in_memory:
+ adapter: sqlite3
+ database: ":memory:"
+ verbosity: quiet
+sqlite:
+ adapter: sqlite
+ dbfile: plugin_test.sqlite.db
+sqlite3:
+ adapter: sqlite3
+ dbfile: plugin_test.sqlite3.db
+postgresql:
+ adapter: postgresql
+ username: postgres
+ password: postgres
+ database: plugin_test
+mysql:
+ adapter: mysql
+ host: localhost
+ username: root
+ password:
+ database: plugin_test
14 spec/app_root/config/environment.rb
@@ -0,0 +1,14 @@
+require File.join(File.dirname(__FILE__), 'boot')
+
+Rails::Initializer.run do |config|
+ config.cache_classes = false
+ config.whiny_nils = true
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
+ config.plugin_locators.unshift(
+ Class.new(Rails::Plugin::Locator) do
+ def plugins
+ [Rails::Plugin.new(File.expand_path('.'))]
+ end
+ end
+ ) unless defined?(PluginTestHelper::PluginLocator)
+end
0  spec/app_root/config/environments/in_memory.rb
No changes.
0  spec/app_root/config/environments/mysql.rb
No changes.
0  spec/app_root/config/environments/postgresql.rb
No changes.
0  spec/app_root/config/environments/sqlite.rb
No changes.
0  spec/app_root/config/environments/sqlite3.rb
No changes.
4 spec/app_root/config/routes.rb
@@ -0,0 +1,4 @@
+ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+ map.connect ':controller/:action/:id.:format'
+end
4 spec/app_root/lib/console_with_fixtures.rb
@@ -0,0 +1,4 @@
+# Loads fixtures into the database when running the test app via the console
+(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
+ Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
+end
1  spec/app_root/log/.gitignore
@@ -0,0 +1 @@
+*.log
7 spec/app_root/script/console
@@ -0,0 +1,7 @@
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+libs = " -r irb/completion"
+libs << " -r test/test_helper"
+libs << " -r console_app"
+libs << " -r console_with_helpers"
+libs << " -r console_with_fixtures"
+exec "#{irb} #{libs} --simple-prompt"
48 spec/mail_magnet_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe 'mail_magnet' do
+
+ before :each do
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.override_recipients = nil
+ end
+
+ it 'should allow to override recipients, cc and bcc for all mailers' do
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
+ Mailer.deliver_letter
+ ActionMailer::Base.deliveries.last.to.should == ['overridden.to@example.com']
+ ActionMailer::Base.deliveries.last.cc.should == ['overridden.to@example.com']
+ ActionMailer::Base.deliveries.last.bcc.should == ['overridden.to@example.com']
+ end
+
+ it 'should allow to override recipients, cc, and bcc with multiple recipients' do
+ overrides = %w[ overridden.to@example.com other.overridden.to@example.com ]
+ ActionMailer::Base.override_recipients = overrides
+ Mailer.deliver_letter
+ ActionMailer::Base.deliveries.last.to.should == overrides
+ ActionMailer::Base.deliveries.last.cc.should == overrides
+ ActionMailer::Base.deliveries.last.bcc.should == overrides
+ end
+
+ it 'should put the original recipients, cc and bcc into the mail body' do
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
+ Mailer.deliver_letter
+ ActionMailer::Base.deliveries.last.body.should include('To: original.to@example.com')
+ ActionMailer::Base.deliveries.last.body.should include('Cc: original.cc@example.com')
+ ActionMailer::Base.deliveries.last.body.should include('Bcc: original.bcc@example.com')
+ end
+
+ it 'should leave original recipients untouched if it is not activated' do
+ Mailer.deliver_letter
+ ActionMailer::Base.deliveries.last.to.should == ['original.to@example.com']
+ ActionMailer::Base.deliveries.last.cc.should == ['original.cc@example.com']
+ ActionMailer::Base.deliveries.last.bcc.should == ['original.bcc@example.com']
+ end
+
+ it 'should not touch the subject' do
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
+ Mailer.deliver_letter
+ ActionMailer::Base.deliveries.last.subject.should == 'Hello Universe!'
+ end
+
+end
21 spec/spec_helper.rb
@@ -0,0 +1,21 @@
+$: << File.join(File.dirname(__FILE__), "/../lib" )
+
+# Set the default environment to sqlite3's in_memory database
+ENV['RAILS_ENV'] ||= 'in_memory'
+
+# Load the Rails environment and testing framework
+require "#{File.dirname(__FILE__)}/app_root/config/environment"
+require "#{File.dirname(__FILE__)}/../lib/mail_magnet"
+require 'spec/rails'
+
+# Undo changes to RAILS_ENV
+silence_warnings {RAILS_ENV = ENV['RAILS_ENV']}
+
+# Run the migrations
+ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
+
+Spec::Runner.configure do |config|
+ config.use_transactional_fixtures = true
+ config.use_instantiated_fixtures = false
+end
+
2  spec/support/rcov.opts
@@ -0,0 +1,2 @@
+--exclude "spec/*,gems/*"
+--rails
4 spec/support/spec.opts
@@ -0,0 +1,4 @@
+--colour
+--format progress
+--loadby mtime
+--reverse
Please sign in to comment.
Something went wrong with that request. Please try again.