From a95ead2a75e3d37c8db9064f27b4da1a5f877bb1 Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Tue, 28 Apr 2015 18:56:38 -0700 Subject: [PATCH] Add Railtie to delayed_job_active_record Explicitly requiring ActiveRecord models in `lib/delayed_job_active_record.rb` may result in losing custom configurations that are set in config/initializers on Rails. Let' use Railtie and ActiveSupport.on_load to set it up so that Delayed::Backend::ActiveRecord::Job model will respect arbitrary configs. --- lib/delayed/backend/active_record/railtie.rb | 14 ++++++++++++++ lib/delayed_job_active_record.rb | 11 ++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 lib/delayed/backend/active_record/railtie.rb diff --git a/lib/delayed/backend/active_record/railtie.rb b/lib/delayed/backend/active_record/railtie.rb new file mode 100644 index 00000000..d126bc9e --- /dev/null +++ b/lib/delayed/backend/active_record/railtie.rb @@ -0,0 +1,14 @@ +module Delayed + module Backend + module ActiveRecord + class Railtie < ::Rails::Railtie + initializer 'delayed_job_active_record' do |_app| + ActiveSupport.on_load(:active_record) do + require "delayed/backend/active_record" + Delayed::Worker.backend = :active_record + end + end + end + end + end +end diff --git a/lib/delayed_job_active_record.rb b/lib/delayed_job_active_record.rb index e6efd295..28cfea4e 100644 --- a/lib/delayed_job_active_record.rb +++ b/lib/delayed_job_active_record.rb @@ -1,5 +1,10 @@ -require "active_record" require "delayed_job" -require "delayed/backend/active_record" -Delayed::Worker.backend = :active_record +if defined?(Rails) + require "delayed/backend/active_record/railtie" +else + require "active_record" + require "delayed/backend/active_record" + + Delayed::Worker.backend = :active_record +end