mail_logger 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 - 2009 Fusionary Media
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ generators/mail_logger/lib/rake_commands.rb
2
+ generators/mail_logger/mail_logger_generator.rb
3
+ generators/mail_logger/templates/app/models/mail_log.rb
4
+ generators/mail_logger/templates/db/migrate/create_mail_logs.rb
5
+ generators/mail_logger/USAGE
6
+ lib/mail_logger/app/models/mail_log.rb
7
+ lib/mail_logger/lib/extensions/action_mailer.rb
8
+ lib/mail_logger.rb
9
+ mail_logger.gemspec
10
+ Manifest
11
+ MIT-LICENSE
12
+ rails/init.rb
13
+ Rakefile
14
+ README.textile
15
+ test/test_helper.rb
16
+ test/unit/action_mailer_extension_test.rb
17
+ test/unit/mail_log_test.rb
@@ -0,0 +1,31 @@
1
+ h1. Mail Logger
2
+
3
+ Braindead-simple email logger for ActionMailer using ActiveRecord
4
+
5
+ h2. Gem Installation for Rails 2.1+
6
+
7
+ In config/environment.rb:
8
+
9
+ config.gem "fusionary-mail_logger",
10
+ :lib => "mail_logger",
11
+ :source => "http://gems.github.com"
12
+
13
+ h2. Generator
14
+
15
+ Ensure the environment's database is present and then run
16
+
17
+ script/generate mail_logger
18
+
19
+ This will generate the migration and generate the MailLog ActiveRecord model.
20
+
21
+ The model includes a module that attaches all the necessary code, allowing you to add other code (named scopes, etc) without interference
22
+
23
+ h2. Ultra-Transparent
24
+
25
+ You could install this gem and not notice *anything* different with your application. It sits in the background, dumping all emails that get sent to your database. The to, from, and subject line, as well as created_at, are all indexed for your querying pleasure. Set up named scopes to search fields, or aggregate all common subjects and allow administrator accounts to filter/view them. Whatever the case, the data is yours; whipping together the controller and setting permissions is the easy part.
26
+
27
+ h2. About
28
+
29
+ The purpose of this is to provide a simple audit trail, specific to ActionMailer, for Rails apps. Without much work, you could write a controller and some views and then monitor the emails sent out; that's really beyond the scope of this project though.
30
+
31
+ Copyright (c) 2009 Fusionary Media, released under the MIT license
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'echoe'
5
+
6
+ Echoe.new("mail_logger", "0.1.5") do |p|
7
+ p.description = "Braindead-simple email logger for ActionMailer using ActiveRecord"
8
+ p.url = "http://github.com/fusionary/mail_logger"
9
+ p.author = ["Fusionary Media", "Josh Clayton"]
10
+ p.email = "joshua.clayton@gmail.com"
11
+ p.ignore_pattern = ["tmp/*"]
12
+ p.development_dependencies = ["actionmailer >= 2.1.0"]
13
+ end
@@ -0,0 +1 @@
1
+ script/generate mail_logger
@@ -0,0 +1,22 @@
1
+ Rails::Generator::Commands::Create.class_eval do
2
+ def rake_db_migrate
3
+ logger.rake "db:migrate"
4
+ unless system("rake db:migrate")
5
+ logger.rake "db:migrate failed. Rolling back"
6
+ command(:destroy).invoke!
7
+ end
8
+ end
9
+ end
10
+
11
+ Rails::Generator::Commands::Destroy.class_eval do
12
+ def rake_db_migrate
13
+ logger.rake "db:rollback"
14
+ system "rake db:rollback"
15
+ end
16
+ end
17
+
18
+ Rails::Generator::Commands::List.class_eval do
19
+ def rake_db_migrate
20
+ logger.rake "db:migrate"
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/rake_commands.rb")
2
+
3
+ class MailLoggerGenerator < Rails::Generator::Base
4
+ def manifest
5
+ record do |m|
6
+ m.directory File.join("app", "models")
7
+ ["app/models/mail_log.rb"].each do |file|
8
+ m.file file, file
9
+ end
10
+
11
+ unless ActiveRecord::Base.connection.table_exists?(:mail_logs)
12
+ m.migration_template 'db/migrate/create_mail_logs.rb', 'db/migrate', :migration_file_name => 'create_mail_logs'
13
+ end
14
+
15
+ m.rake_db_migrate
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class MailLog < ActiveRecord::Base
2
+ include MailLogger::App::Models::MailLog
3
+ end
@@ -0,0 +1,20 @@
1
+ class CreateMailLogs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :mail_logs do |t|
4
+ t.string :subject, :to, :from, :cc
5
+ t.text :body, :message
6
+ t.timestamps
7
+ end
8
+
9
+ change_table :mail_logs do |t|
10
+ t.index :subject
11
+ t.index :to
12
+ t.index :from
13
+ t.index :created_at
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :mail_logs
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ require "mail_logger/app/models/mail_log"
2
+ require "mail_logger/lib/extensions/action_mailer"
3
+
4
+ ActionMailer::Base.send :include, MailLogger::Lib::Extensions::ActionMailer
@@ -0,0 +1,34 @@
1
+ module MailLogger
2
+ module App
3
+ module Models
4
+ module MailLog
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def create_from_mail(email)
11
+ ensure_object_is_email(email)
12
+ ensure_required_attributes_are_assigned(email)
13
+
14
+ self.create(:to => email.to.join(", "), :from => email.from.join(", "), :subject => email.subject, :body => email.quoted_body, :message => email.encoded)
15
+ end
16
+
17
+ private
18
+
19
+ def ensure_object_is_email(email)
20
+ [:to, :from, :subject, :quoted_body, :encoded].each do |att|
21
+ raise(ArgumentError, "object is not an email") unless email.respond_to?(att)
22
+ end
23
+ end
24
+
25
+ def ensure_required_attributes_are_assigned(email)
26
+ [:to, :from].each do |required_att|
27
+ raise(ArgumentError, "email does not have attribute '#{required_att}' assigned") if email.send(required_att).blank?
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ module MailLogger
2
+ module Lib
3
+ module Extensions
4
+ module ActionMailer
5
+ def self.included(base)
6
+ base.class_eval do
7
+ def deliver_with_hooks!(*args)
8
+ logger ||= nil
9
+ mail = args.first || @mail # depend on ActionMailer::Base's @mail ivar is pretty nasty but has to be done
10
+ if !mail.nil?
11
+ begin
12
+ logger.info "MailLogger logged #{mail.encoded}" if logger
13
+ MailLog.create_from_mail(mail)
14
+ rescue
15
+ logger.info "MailLogger failed" if logger
16
+ end
17
+ end
18
+
19
+ deliver_without_hooks!(*args)
20
+ end
21
+
22
+ alias_method_chain :deliver!, :hooks
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{mail_logger}
5
+ s.version = "0.1.5"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Fusionary Media, Josh Clayton"]
9
+ s.date = %q{2009-05-15}
10
+ s.description = %q{Braindead-simple email logger for ActionMailer using ActiveRecord}
11
+ s.email = %q{joshua.clayton@gmail.com}
12
+ s.extra_rdoc_files = ["lib/mail_logger/app/models/mail_log.rb", "lib/mail_logger/lib/extensions/action_mailer.rb", "lib/mail_logger.rb", "README.textile"]
13
+ s.files = ["generators/mail_logger/lib/rake_commands.rb", "generators/mail_logger/mail_logger_generator.rb", "generators/mail_logger/templates/app/models/mail_log.rb", "generators/mail_logger/templates/db/migrate/create_mail_logs.rb", "generators/mail_logger/USAGE", "lib/mail_logger/app/models/mail_log.rb", "lib/mail_logger/lib/extensions/action_mailer.rb", "lib/mail_logger.rb", "mail_logger.gemspec", "Manifest", "MIT-LICENSE", "rails/init.rb", "Rakefile", "README.textile", "test/test_helper.rb", "test/unit/action_mailer_extension_test.rb", "test/unit/mail_log_test.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/fusionary/mail_logger}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Mail_logger", "--main", "README.textile"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{mail_logger}
19
+ s.rubygems_version = %q{1.3.2}
20
+ s.summary = %q{Braindead-simple email logger for ActionMailer using ActiveRecord}
21
+ s.test_files = ["test/test_helper.rb", "test/unit/action_mailer_extension_test.rb", "test/unit/mail_log_test.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<actionmailer>, [">= 2.1.0"])
29
+ else
30
+ s.add_dependency(%q<actionmailer>, [">= 2.1.0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<actionmailer>, [">= 2.1.0"])
34
+ end
35
+ end
@@ -0,0 +1,2 @@
1
+ require "action_mailer"
2
+ require "mail_logger"
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
5
+
6
+ require File.join(File.dirname(__FILE__), "../rails/init")
7
+
8
+ require 'redgreen'
9
+ require 'shoulda'
10
+
11
+ class MailLog
12
+ include MailLogger::App::Models::MailLog
13
+ end
14
+
15
+ class FakeMailer
16
+ def initialize(email)
17
+ @mail = email
18
+ end
19
+
20
+ def deliver!(*args)
21
+ "sent email"
22
+ end
23
+ end
24
+
25
+ FakeMailer.send :include, MailLogger::Lib::Extensions::ActionMailer
26
+
27
+ class ValidEmail
28
+ attr_accessor :to, :from, :subject, :quoted_body, :encoded
29
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), "../test_helper")
2
+
3
+ class ActionMailerExtensionTest < ActiveSupport::TestCase
4
+ context "sending a valid email" do
5
+ should "trigger the default #deliver!" do
6
+ mailer = FakeMailer.new(nil)
7
+ assert_equal "sent email", mailer.deliver!
8
+ end
9
+
10
+ should "not attempt to create a new mail log if mail is nil" do
11
+ mailer = FakeMailer.new(nil)
12
+ MailLog.expects(:create_from_mail).never
13
+ assert_equal "sent email", mailer.deliver!
14
+ end
15
+
16
+ should "attempt to create a new mail log if email if present" do
17
+ email = ValidEmail.new
18
+ email.to = email.from = ["user@example.com"]
19
+ mailer = FakeMailer.new(email)
20
+ MailLog.expects(:create_from_mail).with(email)
21
+ assert_equal "sent email", mailer.deliver!
22
+ end
23
+
24
+ should "proceed even if MailLog raises an error" do
25
+ mailer = FakeMailer.new("my email")
26
+ MailLog.expects(:create_from_mail).with("my email").raises("an error")
27
+ assert_equal "sent email", mailer.deliver!
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), "../test_helper")
2
+
3
+ class MailLogTest < ActiveSupport::TestCase
4
+ context "Mail log creation" do
5
+ setup { MailLog.stubs(:create) }
6
+
7
+ should "ensure the email responds to the correct methods" do
8
+ email = ValidEmail.new
9
+ email.to = email.from = ["user@example.com"]
10
+
11
+ [:to, :from, :subject, :quoted_body, :encoded].each do |att|
12
+ email.expects(:respond_to?).with(att).returns(true)
13
+ end
14
+
15
+ MailLog.create_from_mail(email)
16
+ end
17
+
18
+ should "raise an error if the object is not an email" do
19
+ assert_raise ArgumentError, "object is not an email" do
20
+ MailLog.create_from_mail("not an email")
21
+ end
22
+ end
23
+
24
+ should "raise an error if to or from is not set" do
25
+ email = ValidEmail.new
26
+ email.to = ["user@example.com"]
27
+ assert_raise ArgumentError, "email does not have attribute 'from' assigned" do
28
+ MailLog.create_from_mail(email)
29
+ end
30
+
31
+ email.from, email.to = ["user@example.com"]
32
+ assert_raise ArgumentError, "email does not have attribute 'to' assigned" do
33
+ MailLog.create_from_mail(email)
34
+ end
35
+ end
36
+
37
+ should "pass all attributes when creating a new mail log" do
38
+ email = ValidEmail.new
39
+ email.from = ["user-1@example.com", "user-2@example.com"]
40
+ email.to = ["user-3@example.com", "user-4@example.com"]
41
+ email.subject = "Subject line"
42
+ email.quoted_body = "This is a quoted body"
43
+ email.encoded = "Encoded message"
44
+
45
+ MailLog.expects(:create).with({:from => "user-1@example.com, user-2@example.com", :to => "user-3@example.com, user-4@example.com", :subject => "Subject line", :body => "This is a quoted body", :message => "Encoded message"})
46
+ MailLog.create_from_mail(email)
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Fusionary Media, Josh Clayton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-15 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionmailer
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.0
24
+ version:
25
+ description: Braindead-simple email logger for ActionMailer using ActiveRecord
26
+ email: joshua.clayton@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - lib/mail_logger/app/models/mail_log.rb
33
+ - lib/mail_logger/lib/extensions/action_mailer.rb
34
+ - lib/mail_logger.rb
35
+ - README.textile
36
+ files:
37
+ - generators/mail_logger/lib/rake_commands.rb
38
+ - generators/mail_logger/mail_logger_generator.rb
39
+ - generators/mail_logger/templates/app/models/mail_log.rb
40
+ - generators/mail_logger/templates/db/migrate/create_mail_logs.rb
41
+ - generators/mail_logger/USAGE
42
+ - lib/mail_logger/app/models/mail_log.rb
43
+ - lib/mail_logger/lib/extensions/action_mailer.rb
44
+ - lib/mail_logger.rb
45
+ - mail_logger.gemspec
46
+ - Manifest
47
+ - MIT-LICENSE
48
+ - rails/init.rb
49
+ - Rakefile
50
+ - README.textile
51
+ - test/test_helper.rb
52
+ - test/unit/action_mailer_extension_test.rb
53
+ - test/unit/mail_log_test.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/fusionary/mail_logger
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --line-numbers
61
+ - --inline-source
62
+ - --title
63
+ - Mail_logger
64
+ - --main
65
+ - README.textile
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "1.2"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: mail_logger
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Braindead-simple email logger for ActionMailer using ActiveRecord
87
+ test_files:
88
+ - test/test_helper.rb
89
+ - test/unit/action_mailer_extension_test.rb
90
+ - test/unit/mail_log_test.rb