Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial functionality

  • Loading branch information...
commit e5bbdc68f47d22452068f31c00dc4f8cb9148856 1 parent 8421741
@dgeb dgeb authored
Showing with 294 additions and 28 deletions.
  1. +2 −2 Gemfile.lock
  2. +55 −13 README.rdoc
  3. 0  app/assets/images/mailhopper/.gitkeep
  4. 0  app/assets/javascripts/mailhopper/.gitkeep
  5. 0  app/assets/stylesheets/mailhopper/.gitkeep
  6. 0  app/controllers/.gitkeep
  7. 0  app/helpers/.gitkeep
  8. +4 −0 app/mailers/mailhopper/mailer.rb
  9. 0  app/models/.gitkeep
  10. +17 −0 app/models/mailhopper/email.rb
  11. 0  app/views/.gitkeep
  12. +36 −0 lib/generators/mailhopper/mailhopper_generator.rb
  13. +28 −0 lib/generators/mailhopper/templates/README
  14. +3 −0  lib/generators/mailhopper/templates/initializer.rb
  15. +22 −0 lib/generators/mailhopper/templates/migrations/create_emails.rb
  16. +9 −0 lib/mailhopper.rb
  17. +19 −0 lib/mailhopper/base.rb
  18. +18 −0 lib/mailhopper/queue.rb
  19. +1 −10 mailhopper.gemspec
  20. 0  test/dummy/app/mailers/.gitkeep
  21. +5 −0 test/dummy/app/mailers/sample_mailer.rb
  22. +3 −0  test/dummy/config/initializers/mailhopper.rb
  23. +22 −0 test/dummy/db/migrate/20110801151741_create_emails.rb
  24. +29 −0 test/dummy/db/schema.rb
  25. +11 −0 test/fixtures/emails.yml
  26. +3 −3 test/integration/navigation_test.rb
  27. +7 −0 test/unit/email_test.rb
View
4 Gemfile.lock
@@ -2,7 +2,7 @@ PATH
remote: .
specs:
mailhopper (0.0.1)
- rails (>= 3.1.0.rc5)
+ rails (>= 3.0.0)
GEM
remote: http://rubygems.org/
@@ -74,7 +74,7 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
- rdoc (3.8)
+ rdoc (3.9.1)
sprockets (2.0.0.beta.12)
hike (~> 1.2)
rack (~> 1.0)
View
68 README.rdoc
@@ -1,22 +1,64 @@
= Mailhopper
-Mailhopper provides an ActiveRecord based queue for email storage and delivery.
+Mailhopper provides an ActiveRecord based queue for email storage and delivery in Rails apps.
-Mailhopper is meant to be used along with a delivery agent such as DelayedMailhopper, which uses DelayedJob to deliver email from the Mailhopper queue.
+Mailhopper is intended to be used along with a delivery agent such as DelayedMailhopper, which uses DelayedJob to deliver email from the Mailhopper queue. Without use of a delivery agent, emails will accumulate in the Mailhopper queue but won't be delivered.
-== Usage
+== Requirements
+Rails 3.0+
-== Contributing to Mailhopper
-
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
-* Fork the project
-* Start a feature/bugfix branch
-* Commit and push until you are happy with your contribution
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+== Installation for Rails
+
+Add to your project's Gemfile:
+
+ gem 'mailhopper'
+
+Install with bundler:
+
+ bundle install
+
+Generate default initializer and migration files:
+
+ rails generate mailhopper
+
+Migrate your database:
+
+ rake db:migrate
+
+!!! Don't forget to also install a delivery agent, such as DelayedMailhopper, so that emails will be delivered from your queue !!!
+
+== Configuration
+
+If you want all of your application's email to be queued in Mailhopper, configure mailers either in environment.rb or your application's environment-specific configuration files:
+
+ MyApp::Application.configure do
+ config.action_mailer.delivery_method = :mailhopper
+ end
+
+Alternatively, or additionally, configure individual mailers to use Mailhopper:
+
+ class MyMailer < ActionMailer::Base
+ ActionMailer::Base.delivery_method = :mailhopper
+ end
+
+== Options
+
+The following options can be configured in your initializer (config/initializers/mailhopper):
+
+ Mailhopper::Base.setup do |config|
+ # The base email class used by Mailhopper
+ config.email_class = Mailhopper::Email
+
+ # The base mailer class used by Mailhopper
+ config.mailer_class = Mailhopper::Mailer
+
+ # The method used by the delivery agent to deliver emails from your queue
+ config.default_delivery_method = :smtp
+ end
+
+It's preferable to leave these options out of your initializer if the defaults, shown above, are acceptable. Delivery agents may override some defaults (e.g. DelayedMailhopper sets email_class = DelayedMailhopper::Email).
== Copyright
-Copyright (c) 2011 Cerebris Corporation. See MIT-LICENSE for further details.
+Copyright (c) 2011 Cerebris Corporation. Released under the MIT License. See MIT-LICENSE for further details.
View
0  app/assets/images/mailhopper/.gitkeep
No changes.
View
0  app/assets/javascripts/mailhopper/.gitkeep
No changes.
View
0  app/assets/stylesheets/mailhopper/.gitkeep
No changes.
View
0  app/controllers/.gitkeep
No changes.
View
0  app/helpers/.gitkeep
No changes.
View
4 app/mailers/mailhopper/mailer.rb
@@ -0,0 +1,4 @@
+module Mailhopper
+ class Mailer < ActionMailer::Base
+ end
+end
View
0  app/models/.gitkeep
No changes.
View
17 app/models/mailhopper/email.rb
@@ -0,0 +1,17 @@
+module Mailhopper
+ class Email < ActiveRecord::Base
+ default_scope :order => 'created_at DESC'
+ scope :unsent, :conditions => 'sent_at is null'
+
+ validates :from_address, :presence => true
+
+ def send!(delivery_method = nil)
+ mail = Mail.new(self.content)
+ mail[:bcc] = self.bcc_address unless self.bcc_address.blank?
+ Base.mailer_class.wrap_delivery_behavior(mail, delivery_method || Base.default_delivery_method)
+ mail.deliver
+ self.sent_at = Time.now
+ self.save!
+ end
+ end
+end
View
0  app/views/.gitkeep
No changes.
View
36 lib/generators/mailhopper/mailhopper_generator.rb
@@ -0,0 +1,36 @@
+require 'rails/generators'
+require 'rails/generators/migration'
+
+module Mailhopper
+ module Generators
+ class MailhopperGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+
+ desc 'Generates Mailhopper files.'
+
+ def self.source_root
+ File.join(File.dirname(__FILE__), 'templates')
+ end
+
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ def create_migration_file
+ migration_template 'migrations/create_emails.rb', 'db/migrate/create_emails.rb'
+ end
+
+ def copy_initializer
+ template 'initializer.rb', 'config/initializers/mailhopper.rb'
+ end
+
+ def show_readme
+ readme 'README' if behavior == :invoke
+ end
+ end
+ end
+end
View
28 lib/generators/mailhopper/templates/README
@@ -0,0 +1,28 @@
+
+===============================================================================
+Mailhopper has been installed.
+===============================================================================
+
+To configure your application to use Mailhopper:
+
+1) Migrate your database with "rake db:migrate".
+
+2) Configure mailers either in environment.rb or environment-specific config files:
+
+ MyApp::Application.configure do
+ config.action_mailer.delivery_method = :mailhopper
+ end
+
+Alternatively, or additionally, you can configure individual mailers:
+
+ class MyMailer < ActionMailer::Base
+ ActionMailer::Base.delivery_method = :mailhopper
+ end
+
+3) Install a delivery agent, such as DelayedMailhopper, so that emails will be delivered from your queue.
+
+4) Configure a delivery method in "config/initializers/mailhopper" for your delivery agent to use (the default is smtp).
+
+ Mailhopper::Base.setup do |config|
+ config.default_delivery_method = :smtp
+ end
View
3  lib/generators/mailhopper/templates/initializer.rb
@@ -0,0 +1,3 @@
+Mailhopper::Base.setup do |config|
+ config.default_delivery_method = :smtp
+end
View
22 lib/generators/mailhopper/templates/migrations/create_emails.rb
@@ -0,0 +1,22 @@
+# Everything listed in this migration will be added to a migration file
+# inside of your main app.
+class CreateEmails < ActiveRecord::Migration
+ def self.up
+ create_table :emails do |t|
+ t.string :from_address, :null => false
+ t.string :to_address,
+ :cc_address,
+ :bcc_address,
+ :reply_to_address,
+ :subject
+ t.text :content
+ t.datetime :sent_at
+ t.integer :delayed_job_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :emails
+ end
+end
View
9 lib/mailhopper.rb
@@ -1,4 +1,13 @@
require "mailhopper/engine"
+require 'mailhopper/queue'
+require 'mailhopper/base'
module Mailhopper
+ class << self
+ def setup(&block)
+ Mailhopper::Base.setup(&block)
+ end
+ end
end
+
+ActionMailer::Base.add_delivery_method :mailhopper, Mailhopper::Queue
View
19 lib/mailhopper/base.rb
@@ -0,0 +1,19 @@
+require File.expand_path('../../../app/models/mailhopper/email', __FILE__)
+require File.expand_path('../../../app/mailers/mailhopper/mailer', __FILE__)
+
+module Mailhopper
+ class Base
+ cattr_accessor :email_class
+ self.email_class = Mailhopper::Email
+
+ cattr_accessor :mailer_class
+ self.mailer_class = Mailhopper::Mailer
+
+ cattr_accessor :default_delivery_method
+ self.default_delivery_method = :smtp
+
+ def self.setup
+ yield self
+ end
+ end
+end
View
18 lib/mailhopper/queue.rb
@@ -0,0 +1,18 @@
+module Mailhopper
+ class Queue
+ def initialize(options)
+ end
+
+ def deliver!(mail)
+ Base.email_class.create({
+ :to_address => mail.to.to_s,
+ :from_address => mail.from.to_s,
+ :cc_address => mail.cc.to_s,
+ :bcc_address => mail.bcc.to_s,
+ :reply_to_address => mail.reply_to.to_s,
+ :subject => mail.subject,
+ :content => mail.to_s
+ })
+ end
+ end
+end
View
11 mailhopper.gemspec
@@ -1,4 +1,3 @@
-# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version:
@@ -14,18 +13,10 @@ Gem::Specification.new do |s|
s.summary = "An ActiveRecord based queue for email storage and delivery."
s.description = "Mailhopper stores your application's emails in a queue for delayed delivery."
- s.rubyforge_project = "mailhopper"
-
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
- # If your gem is dependent on a specific version (or higher) of Rails:
- s.add_dependency "rails", ">= 3.1.0.rc5"
-
- # If your gem contains any jquery-specific javascript:
- # s.add_dependency "jquery-rails"
+ s.add_dependency "rails", ">= 3.0.0"
- # Declare development-specific dependencies:
s.add_development_dependency "sqlite3"
- # s.add_development_dependency "rspec"
end
View
0  test/dummy/app/mailers/.gitkeep
No changes.
View
5 test/dummy/app/mailers/sample_mailer.rb
@@ -0,0 +1,5 @@
+class SampleMailer < ActionMailer::Base
+ ActionMailer::Base.delivery_method = :mailhopper
+
+ default :from => "from@example.com"
+end
View
3  test/dummy/config/initializers/mailhopper.rb
@@ -0,0 +1,3 @@
+Mailhopper::Base.setup do |config|
+ config.default_delivery_method = :smtp
+end
View
22 test/dummy/db/migrate/20110801151741_create_emails.rb
@@ -0,0 +1,22 @@
+# Everything listed in this migration will be added to a migration file
+# inside of your main app.
+class CreateEmails < ActiveRecord::Migration
+ def self.up
+ create_table :emails do |t|
+ t.string :from_address, :null => false
+ t.string :to_address,
+ :cc_address,
+ :bcc_address,
+ :reply_to_address,
+ :subject
+ t.text :content
+ t.datetime :sent_at
+ t.integer :delayed_job_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :emails
+ end
+end
View
29 test/dummy/db/schema.rb
@@ -0,0 +1,29 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110801151741) do
+
+ create_table "emails", :force => true do |t|
+ t.string "from_address", :null => false
+ t.string "to_address"
+ t.string "cc_address"
+ t.string "bcc_address"
+ t.string "reply_to_address"
+ t.string "subject"
+ t.text "content"
+ t.datetime "sent_at"
+ t.integer "delayed_job_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
11 test/fixtures/emails.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
6 test/integration/navigation_test.rb
@@ -3,8 +3,8 @@
class NavigationTest < ActionDispatch::IntegrationTest
fixtures :all
- # test "the truth" do
- # assert true
- # end
+ test "truth" do
+ assert_kind_of Dummy::Application, Rails.application
+ end
end
View
7 test/unit/email_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class EmailTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.