cwninja-inaction_mailer 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,16 @@
1
+ ## Usage:
2
+
3
+ Add the following lines to the appropriate `config/environments/*.rb` file:
4
+
5
+ config.gem "cwninja-inaction_mailer", :lib => 'inaction_mailer', :source => 'http://gems.github.com'
6
+ config.action_mailer.delivery_method = :inaction_file
7
+
8
+ Now fetch and unpack the gem with:
9
+
10
+ rake gems:install gems:unpack
11
+
12
+ As an extra step, for the sake of paranoia, you can check the delivery mechanism with:
13
+
14
+ ./script/runner 'p ActionMailer::Base.delivery_method'
15
+
16
+ Now when you app send e-mails, it will deliver them to `RAILS_ROOT/tmp/sent_mails/mail.*.txt`.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+ require 'date'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the inaction_mailer plugin.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ spec = Gem::Specification.new do |s|
19
+ s.name = %q{inaction_mailer}
20
+ s.version = "0.2"
21
+ s.summary = %q{An ActionMailer delivery mechanism to save mails as files in a directory.}
22
+ s.description = %q{An ActionMailer delivery mechanism to save mails as files in a directory.}
23
+
24
+ s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'test/**/*.rb']
25
+ s.require_path = 'lib'
26
+ s.test_files = Dir[*['test/**/*_test.rb']]
27
+
28
+ s.has_rdoc = true
29
+ s.extra_rdoc_files = ["README.markdown"]
30
+ s.rdoc_options = ['--line-numbers', '--inline-source', "--main", "README.markdown"]
31
+
32
+ s.authors = ["Tom Lea"]
33
+ s.email = %q{commits@tomlea.co.uk}
34
+
35
+ s.platform = Gem::Platform::RUBY
36
+ end
37
+
38
+ Rake::GemPackageTask.new spec do |pkg|
39
+ pkg.need_tar = true
40
+ pkg.need_zip = true
41
+ end
42
+
43
+ desc "Clean files generated by rake tasks"
44
+ task :clobber => [:clobber_rdoc, :clobber_package]
45
+
46
+ desc "Generate a gemspec file"
47
+ task :gemspec do
48
+ File.open("#{spec.name}.gemspec", 'w') do |f|
49
+ f.write spec.to_ruby
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ module InactionMailer
2
+
3
+ def perform_delivery_inaction_file(mail)
4
+ mail.ready_to_send
5
+ InactionMailer.new_mail_file do |file|
6
+ file.write mail.encoded
7
+ end
8
+ end
9
+
10
+ class << self
11
+ def hook!
12
+ ActionMailer::Base.__send__(:include, self)
13
+ end
14
+
15
+ def new_mail_file(&block)
16
+ ensure_output_dir_exists
17
+ id = 0
18
+ id += 1 while File.exist? "#{output_dir}/mail.#{id}.txt"
19
+ File.open("#{output_dir}/mail.#{id}.txt", "w", &block)
20
+ end
21
+
22
+ protected
23
+ def output_dir
24
+ if defined? Rails.root
25
+ "#{Rails.root}/tmp/sent_mails"
26
+ else
27
+ "sent_mails"
28
+ end
29
+ end
30
+
31
+ def ensure_output_dir_exists
32
+ File.mkdir output_dir unless File.directory? output_dir
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,31 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class InactionMailerTest < Test::Unit::TestCase
4
+ class Mailer
5
+ include InactionMailer
6
+ end
7
+
8
+ def test_should_write_mail_to_file
9
+ with_stubbed_file_store do
10
+ mailer = Mailer.new
11
+ mail_content = "Hello world, this is an e-mail!\n"*10
12
+ mail = stub(:ready_to_send => nil, :encoded => mail_content)
13
+ mailer.perform_delivery_inaction_file(mail)
14
+ assert_equal File.read("sent_mails/mail.0.txt"), mail_content
15
+ end
16
+ end
17
+
18
+ def test_should_write_two_mails_to_files
19
+ with_stubbed_file_store do
20
+ mailer = Mailer.new
21
+ mail_1_content = "Hello world, this is an e-mail!"
22
+ mail_2_content = "I am a pirate, yarrr!"
23
+ mail1 = stub(:ready_to_send => nil, :encoded => mail_1_content)
24
+ mail2 = stub(:ready_to_send => nil, :encoded => mail_2_content)
25
+ mailer.perform_delivery_inaction_file(mail1)
26
+ mailer.perform_delivery_inaction_file(mail2)
27
+ assert_equal File.read("sent_mails/mail.0.txt"), mail_1_content
28
+ assert_equal File.read("sent_mails/mail.1.txt"), mail_2_content
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,58 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require "inaction_mailer"
3
+ require "test/unit"
4
+ gem "mocha"
5
+ require "mocha"
6
+
7
+ class Test::Unit::TestCase
8
+ class FileSystemStub
9
+ attr_reader :fake_file_system
10
+
11
+ def initialize(fake_file_system = {})
12
+ @fake_file_system = fake_file_system
13
+ end
14
+
15
+ def open(filename, mode = "r")
16
+ @fake_file_system[filename] ||= StringIO.new if mode == "w"
17
+ raise Errno::ENOENT, "No such file or directory - #{filename}" unless @fake_file_system[filename]
18
+ @fake_file_system[filename].rewind
19
+ if block_given?
20
+ yield @fake_file_system[filename]
21
+ else
22
+ @fake_file_system[filename]
23
+ end
24
+ end
25
+
26
+ def read(filename)
27
+ File.open(filename, "r") do |io|
28
+ io.read
29
+ end
30
+ end
31
+
32
+ def exist?(filename)
33
+ @fake_file_system.has_key? filename
34
+ end
35
+
36
+ def directory?(d)
37
+ @fake_file_system.has_key?(d.split("/").join("/") + "/")
38
+ end
39
+
40
+ def mkdir(d)
41
+ @fake_file_system[d.split("/").join("/") + "/"] = :directory
42
+ end
43
+
44
+ def method_missing(m, *args)
45
+ raise NotImplementedError, "You are using a stubbed file system, and File.#{m} was called, but the stubbed file system does not understand that menthod. Sorry."
46
+ end
47
+ end
48
+
49
+ def with_stubbed_file_store(fake_file_system = {}, &block)
50
+ file_class = File
51
+ Object.send :remove_const, :File
52
+ Object.const_set(:File, FileSystemStub.new(fake_file_system))
53
+ yield
54
+ ensure
55
+ Object.send :remove_const, :File
56
+ Object.const_set(:File, file_class)
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cwninja-inaction_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Tom Lea
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: An ActionMailer delivery mechanism to save mails as files in a directory.
17
+ email: commits@tomlea.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - Rakefile
26
+ - README.markdown
27
+ - lib/inaction_mailer.rb
28
+ - test/functional_test.rb
29
+ - test/test_helper.rb
30
+ has_rdoc: true
31
+ homepage:
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --line-numbers
35
+ - --inline-source
36
+ - --main
37
+ - README.markdown
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: An ActionMailer delivery mechanism to save mails as files in a directory.
59
+ test_files:
60
+ - test/functional_test.rb