mail_magnet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = Mail Magnet
2
+
3
+ Mail Magnet allows you to override e-mail recipients in ActionMailer, e.g. for testing purposes.
4
+
5
+ http://gem-session.com/2010/06/overriding-e-mail-recipients-in-actionmailer
6
+
7
+ == Installation
8
+
9
+ Install the gem with
10
+ sudo gem install mail_magnet
11
+
12
+ ....
13
+
14
+ == Rails 3 compatibility
15
+
16
+ We cannot guarantee Rails 3 compatibility at this point, but we will upgrade the gem when Rails 3 is released.
17
+
18
+ === Credits
19
+
20
+ Arne Hartherz
21
+
22
+ {makandra.com}[http://makandra.com/]
23
+
24
+ {gem-session.com}[http://gem-session.com/]
@@ -0,0 +1,14 @@
1
+ ActionMailer::Base.class_eval do
2
+
3
+ @@override_recipients = nil
4
+ cattr_accessor :override_recipients
5
+
6
+ def deliver_with_override!(mail = @mail)
7
+ if override_recipients.present?
8
+ mail.override_recipients! override_recipients
9
+ end
10
+ deliver_without_override! mail
11
+ end
12
+ alias_method_chain :deliver!, :override
13
+
14
+ end
@@ -0,0 +1,27 @@
1
+ TMail::Mail.class_eval do
2
+
3
+ def override_recipients!(recipients)
4
+ recipients = Array(recipients)
5
+
6
+ original_addresses = {
7
+ :to => override(:to, recipients),
8
+ :cc => override(:cc, recipients),
9
+ :bcc => override(:bcc, recipients)
10
+ }
11
+
12
+ parts = [ "--- Original recipients ---" ]
13
+ [ :to, :cc, :bcc ].each do |target|
14
+ parts << target.to_s.capitalize + ": " + (original_addresses[target] ? original_addresses[target].join(', ') : '(none)')
15
+ end
16
+ parts << "---------------------------"
17
+
18
+ self.body = parts.join("\n") + "\n\n" + self.body
19
+ end
20
+
21
+ def override(method, recipients)
22
+ original_recipients = send("#{method}")
23
+ self.send "#{method}=", recipients if original_recipients
24
+ original_recipients
25
+ end
26
+
27
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,10 @@
1
+ class Mailer < ActionMailer::Base
2
+
3
+ def letter
4
+ recipients 'original.to@example.com'
5
+ cc 'original.cc@example.com'
6
+ bcc 'original.bcc@example.com'
7
+ subject 'Hello Universe!'
8
+ end
9
+
10
+ end
@@ -0,0 +1 @@
1
+ Hello!
@@ -0,0 +1,114 @@
1
+ # Allow customization of the rails framework path
2
+ RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
3
+
4
+ # Don't change this file!
5
+ # Configure your app in config/environment.rb and config/environments/*.rb
6
+
7
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
8
+
9
+ module Rails
10
+ class << self
11
+ def boot!
12
+ unless booted?
13
+ preinitialize
14
+ pick_boot.run
15
+ end
16
+ end
17
+
18
+ def booted?
19
+ defined? Rails::Initializer
20
+ end
21
+
22
+ def pick_boot
23
+ (vendor_rails? ? VendorBoot : GemBoot).new
24
+ end
25
+
26
+ def vendor_rails?
27
+ File.exist?(RAILS_FRAMEWORK_ROOT)
28
+ end
29
+
30
+ def preinitialize
31
+ load(preinitializer_path) if File.exist?(preinitializer_path)
32
+ end
33
+
34
+ def preinitializer_path
35
+ "#{RAILS_ROOT}/config/preinitializer.rb"
36
+ end
37
+ end
38
+
39
+ class Boot
40
+ def run
41
+ load_initializer
42
+ Rails::Initializer.run(:set_load_path)
43
+ end
44
+ end
45
+
46
+ class VendorBoot < Boot
47
+ def load_initializer
48
+ require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
49
+ Rails::Initializer.run(:install_gem_spec_stubs)
50
+ end
51
+ end
52
+
53
+ class GemBoot < Boot
54
+ def load_initializer
55
+ self.class.load_rubygems
56
+ load_rails_gem
57
+ require 'initializer'
58
+ end
59
+
60
+ def load_rails_gem
61
+ if version = self.class.gem_version
62
+ gem 'rails', version
63
+ else
64
+ gem 'rails'
65
+ end
66
+ rescue Gem::LoadError => load_error
67
+ $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.)
68
+ exit 1
69
+ end
70
+
71
+ class << self
72
+ def rubygems_version
73
+ Gem::RubyGemsVersion rescue nil
74
+ end
75
+
76
+ def gem_version
77
+ if defined? RAILS_GEM_VERSION
78
+ RAILS_GEM_VERSION
79
+ elsif ENV.include?('RAILS_GEM_VERSION')
80
+ ENV['RAILS_GEM_VERSION']
81
+ else
82
+ parse_gem_version(read_environment_rb)
83
+ end
84
+ end
85
+
86
+ def load_rubygems
87
+ require 'rubygems'
88
+ min_version = '1.1.1'
89
+ unless rubygems_version >= min_version
90
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
91
+ exit 1
92
+ end
93
+
94
+ rescue LoadError
95
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
96
+ exit 1
97
+ end
98
+
99
+ def parse_gem_version(text)
100
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
101
+ end
102
+
103
+ private
104
+ def read_environment_rb
105
+ environment_rb = "#{RAILS_ROOT}/config/environment.rb"
106
+ environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
107
+ File.read(environment_rb)
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ # All that for this:
114
+ Rails.boot!
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'boot')
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.cache_classes = false
5
+ config.whiny_nils = true
6
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
7
+ config.plugin_locators.unshift(
8
+ Class.new(Rails::Plugin::Locator) do
9
+ def plugins
10
+ [Rails::Plugin.new(File.expand_path('.'))]
11
+ end
12
+ end
13
+ ) unless defined?(PluginTestHelper::PluginLocator)
14
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect ':controller/:action/:id'
3
+ map.connect ':controller/:action/:id.:format'
4
+ end
@@ -0,0 +1,4 @@
1
+ # Loads fixtures into the database when running the test app via the console
2
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
3
+ Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
4
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'mail_magnet' do
4
+
5
+ before :each do
6
+ ActionMailer::Base.delivery_method = :test
7
+ ActionMailer::Base.override_recipients = nil
8
+ end
9
+
10
+ it 'should allow to override recipients, cc and bcc for all mailers' do
11
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
12
+ Mailer.deliver_letter
13
+ ActionMailer::Base.deliveries.last.to.should == ['overridden.to@example.com']
14
+ ActionMailer::Base.deliveries.last.cc.should == ['overridden.to@example.com']
15
+ ActionMailer::Base.deliveries.last.bcc.should == ['overridden.to@example.com']
16
+ end
17
+
18
+ it 'should allow to override recipients, cc, and bcc with multiple recipients' do
19
+ overrides = %w[ overridden.to@example.com other.overridden.to@example.com ]
20
+ ActionMailer::Base.override_recipients = overrides
21
+ Mailer.deliver_letter
22
+ ActionMailer::Base.deliveries.last.to.should == overrides
23
+ ActionMailer::Base.deliveries.last.cc.should == overrides
24
+ ActionMailer::Base.deliveries.last.bcc.should == overrides
25
+ end
26
+
27
+ it 'should put the original recipients, cc and bcc into the mail body' do
28
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
29
+ Mailer.deliver_letter
30
+ ActionMailer::Base.deliveries.last.body.should include('To: original.to@example.com')
31
+ ActionMailer::Base.deliveries.last.body.should include('Cc: original.cc@example.com')
32
+ ActionMailer::Base.deliveries.last.body.should include('Bcc: original.bcc@example.com')
33
+ end
34
+
35
+ it 'should leave original recipients untouched if it is not activated' do
36
+ Mailer.deliver_letter
37
+ ActionMailer::Base.deliveries.last.to.should == ['original.to@example.com']
38
+ ActionMailer::Base.deliveries.last.cc.should == ['original.cc@example.com']
39
+ ActionMailer::Base.deliveries.last.bcc.should == ['original.bcc@example.com']
40
+ end
41
+
42
+ it 'should not touch the subject' do
43
+ ActionMailer::Base.override_recipients = 'overridden.to@example.com'
44
+ Mailer.deliver_letter
45
+ ActionMailer::Base.deliveries.last.subject.should == 'Hello Universe!'
46
+ end
47
+
48
+ end
@@ -0,0 +1,21 @@
1
+ $: << File.join(File.dirname(__FILE__), "/../lib" )
2
+
3
+ # Set the default environment to sqlite3's in_memory database
4
+ ENV['RAILS_ENV'] ||= 'in_memory'
5
+
6
+ # Load the Rails environment and testing framework
7
+ require "#{File.dirname(__FILE__)}/app_root/config/environment"
8
+ require "#{File.dirname(__FILE__)}/../lib/mail_magnet"
9
+ require 'spec/rails'
10
+
11
+ # Undo changes to RAILS_ENV
12
+ silence_warnings {RAILS_ENV = ENV['RAILS_ENV']}
13
+
14
+ # Run the migrations
15
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
16
+
17
+ Spec::Runner.configure do |config|
18
+ config.use_transactional_fixtures = true
19
+ config.use_instantiated_fixtures = false
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_magnet
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Arne Hartherz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-23 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Override ActionMailer recipients so all mails go to a given address
22
+ email: github@makandra.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - lib/mail_magnet/action_mailer_ext.rb
31
+ - lib/mail_magnet/tmail_ext.rb
32
+ - spec/app_root/app/views/mailer/letter.erb
33
+ - README.rdoc
34
+ has_rdoc: true
35
+ homepage: http://github.com/makandra/mail_magnet
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Override ActionMailer recipients so all mails go to a given address
64
+ test_files:
65
+ - spec/app_root/config/boot.rb
66
+ - spec/app_root/config/environment.rb
67
+ - spec/app_root/config/routes.rb
68
+ - spec/app_root/config/environments/in_memory.rb
69
+ - spec/app_root/config/environments/mysql.rb
70
+ - spec/app_root/config/environments/postgresql.rb
71
+ - spec/app_root/config/environments/sqlite.rb
72
+ - spec/app_root/config/environments/sqlite3.rb
73
+ - spec/app_root/lib/console_with_fixtures.rb
74
+ - spec/app_root/app/controllers/application_controller.rb
75
+ - spec/app_root/app/models/mailer.rb
76
+ - spec/spec_helper.rb
77
+ - spec/mail_magnet_spec.rb