From cd423f9cdcde7d13fdb9853700c6205554618c65 Mon Sep 17 00:00:00 2001 From: Jon Hinson Date: Mon, 26 Mar 2012 15:37:38 -0500 Subject: [PATCH] Add hook_rails configuration option to make the Rails extensions optional --- lib/sidekiq.rb | 1 + lib/sidekiq/rails.rb | 1 + test/test_extension_configuration.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/test_extension_configuration.rb diff --git a/lib/sidekiq.rb b/lib/sidekiq.rb index fda6b8fb..c959479d 100644 --- a/lib/sidekiq.rb +++ b/lib/sidekiq.rb @@ -16,6 +16,7 @@ module Sidekiq :require => '.', :environment => nil, :timeout => 5, + :hook_rails => true, } def self.options diff --git a/lib/sidekiq/rails.rb b/lib/sidekiq/rails.rb index ab31bd0a..1d272260 100644 --- a/lib/sidekiq/rails.rb +++ b/lib/sidekiq/rails.rb @@ -1,5 +1,6 @@ module Sidekiq def self.hook_rails! + return unless Sidekiq.options[:hook_rails] if defined?(ActiveRecord) ActiveRecord::Base.extend(Sidekiq::Extensions::ActiveRecord) ActiveRecord::Base.send(:include, Sidekiq::Extensions::ActiveRecord) diff --git a/test/test_extension_configuration.rb b/test/test_extension_configuration.rb new file mode 100644 index 00000000..4e7929dc --- /dev/null +++ b/test/test_extension_configuration.rb @@ -0,0 +1,28 @@ +require 'helper' +require 'sidekiq' + +class TestExtensionConfiguration < MiniTest::Unit::TestCase + describe 'sidekiq rails extensions configuration' do + before do + @options = Sidekiq.options + end + + after do + Sidekiq.options = @options + end + + it 'should set hook_rails option to true by default' do + assert_equal true, Sidekiq.options[:hook_rails] + end + + it 'should extend ActiveRecord and ActiveMailer if hook_rails is true' do + assert_equal ActionMailer::Base, Sidekiq.hook_rails! + end + + it 'should not extend ActiveRecord and ActiveMailer if hook_rails is false' do + Sidekiq.options = { :hook_rails => false } + assert_equal nil, Sidekiq.hook_rails! + end + + end +end