Skip to content

Commit 49c9f85

Browse files
committed
Merge pull request #16485 from seuros/activejob
Integrate ActiveJob / DeliverLater / GlobalID with Rails
2 parents b2708a6 + 080296b commit 49c9f85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2219
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rvm:
1212
env:
1313
- "GEM=railties"
1414
- "GEM=ap"
15-
- "GEM=am,amo,as,av"
15+
- "GEM=am,amo,as,av,aj"
1616
- "GEM=ar:mysql"
1717
- "GEM=ar:mysql2"
1818
- "GEM=ar:sqlite3"

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ end
3535
# AS
3636
gem 'dalli', '>= 2.2.1'
3737

38+
# ActiveJob
39+
gem 'globalid', github: 'rails/globalid'
40+
gem 'resque', require: false
41+
gem 'resque-scheduler', require: false
42+
gem 'sidekiq', require: false
43+
gem 'sucker_punch', require: false
44+
gem 'delayed_job', require: false
45+
gem 'queue_classic', require: false
46+
gem 'sneakers', '0.1.1.pre', require: false
47+
gem 'que', require: false
48+
gem 'backburner', require: false
49+
gem 'qu-rails', github: "bkeepers/qu", branch: "master", require: false
50+
gem 'qu-redis', require: false
51+
3852
# Add your own local bundler stuff
3953
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
4054
instance_eval File.read local_gemfile if File.exist? local_gemfile

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ task :build => "all:build"
1111
desc "Release all gems to rubygems and create a tag"
1212
task :release => "all:release"
1313

14-
PROJECTS = %w(activesupport activemodel actionpack actionview actionmailer activerecord railties)
14+
PROJECTS = %w(activesupport activemodel actionpack actionview actionmailer activerecord railties activejob)
1515

1616
desc 'Run all tests by default'
1717
task :default => %w(test test:isolated)

actionmailer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Make ActionMailer::Previews methods class methods. Previously they were
2+
instance methods and ActionMailer tries to render a message when they
3+
are called.
4+
5+
*Cristian Bica*
6+
17
* Deprecate `*_path` helpers in email views. When used they generate
28
non-working links and are not the intention of most developers. Instead
39
we recommend to use `*_url` helper.

actionmailer/lib/action_mailer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ module ActionMailer
4545
autoload :Previews, 'action_mailer/preview'
4646
autoload :TestCase
4747
autoload :TestHelper
48+
autoload :MessageDelivery
49+
autoload :DeliveryJob
4850
end

actionmailer/lib/action_mailer/base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ def set_payload_for_mail(payload, mail) #:nodoc:
548548
end
549549

550550
def method_missing(method_name, *args) # :nodoc:
551-
if respond_to?(method_name)
552-
new(method_name, *args).message
551+
if action_methods.include?(method_name.to_s)
552+
MessageDelivery.new(self, method_name, *args)
553553
else
554554
super
555555
end
@@ -586,6 +586,10 @@ def process(method_name, *args) #:nodoc:
586586
class NullMail #:nodoc:
587587
def body; '' end
588588

589+
def respond_to?(string, include_all=false)
590+
true
591+
end
592+
589593
def method_missing(*args)
590594
nil
591595
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'active_job'
2+
3+
module ActionMailer
4+
class DeliveryJob < ActiveJob::Base
5+
queue_as :mailers
6+
7+
def perform(mailer, mail_method, delivery_method, *args)
8+
mailer.constantize.public_send(mail_method, *args).send(delivery_method)
9+
end
10+
end
11+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'delegate'
2+
3+
module ActionMailer
4+
class MessageDelivery < Delegator
5+
def initialize(mailer, mail_method, *args)
6+
@mailer = mailer
7+
@mail_method = mail_method
8+
@args = args
9+
end
10+
11+
def __getobj__
12+
@obj ||= @mailer.send(:new, @mail_method, *@args).message
13+
end
14+
15+
def __setobj__(obj)
16+
@obj = obj
17+
end
18+
19+
def message #:nodoc:
20+
__getobj__
21+
end
22+
23+
def deliver_later!(options={})
24+
enqueue_delivery :deliver!, options
25+
end
26+
27+
def deliver_later(options={})
28+
enqueue_delivery :deliver, options
29+
end
30+
31+
private
32+
def enqueue_delivery(delivery_method, options={})
33+
args = @mailer.name, @mail_method.to_s, delivery_method.to_s, *@args
34+
enqueue_method = :enqueue
35+
if options[:at]
36+
enqueue_method = :enqueue_at
37+
args.unshift options[:at]
38+
elsif options[:in]
39+
enqueue_method = :enqueue_in
40+
args.unshift options[:in]
41+
end
42+
ActionMailer::DeliveryJob.send enqueue_method, *args
43+
end
44+
end
45+
end

actionmailer/lib/action_mailer/preview.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module Previews #:nodoc:
2222
# :nodoc:
2323
mattr_accessor :preview_interceptors, instance_writer: false
2424
self.preview_interceptors = []
25+
end
2526

27+
module ClassMethods
2628
# Register one or more Interceptors which will be called before mail is previewed.
2729
def register_preview_interceptors(*interceptors)
2830
interceptors.flatten.compact.each { |interceptor| register_preview_interceptor(interceptor) }

actionmailer/test/base_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def welcome
243243
end
244244
end
245245

246-
e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome }
246+
e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome.message }
247247
assert_match(/Can't add attachments after `mail` was called./, e.message)
248248
end
249249

@@ -255,7 +255,7 @@ def welcome
255255
end
256256
end
257257

258-
e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome }
258+
e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome.message }
259259
assert_match(/Can't add attachments after `mail` was called./, e.message)
260260
end
261261

0 commit comments

Comments
 (0)