Skip to content

Commit

Permalink
move active_record and mongoid version checking to bullet/dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Jun 6, 2012
1 parent 2602914 commit 6368686
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 43 deletions.
20 changes: 5 additions & 15 deletions lib/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
module Bullet
extend Dependency

if Rails.version =~ /\A3\.0/
autoload :ActiveRecord, 'bullet/active_record3'
elsif Rails.version =~ /\A3\.[12]/
autoload :ActiveRecord, 'bullet/active_record31'
else
autoload :ActiveRecord, 'bullet/active_record2'
autoload :ActionController, 'bullet/action_controller2'
if active_record?
autoload :ActiveRecord, "bullet/#{active_record_version}"
autoload :ActionController, 'bullet/action_controller2' if active_record2?
end
if mongoid?
if Mongoid::VERSION =~ /\A2\.4/
autoload :Mongoid, 'bullet/mongoid24'
elsif Mongoid::VERSION =~ /\A3/
autoload :Mongoid, 'bullet/mongoid3'
end
autoload :Mongoid, "bullet/#{mongoid_version}"
end
autoload :Rack, 'bullet/rack'
autoload :BulletLogger, 'bullet/logger'
Expand Down Expand Up @@ -55,9 +47,7 @@ def enable=(enable)
end
if active_record?
Bullet::ActiveRecord.enable
if Rails.version =~ /\A2./
Bullet::ActionController.enable
end
Bullet::ActionController.enable if active_record2?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bullet/action_controller2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Bullet
class ActionController
def self.enable
require 'action_controller'
if Rails.version =~ /\A2.3/
if active_record23?
::ActionController::Dispatcher.middleware.use Bullet::Rack
::ActionController::Dispatcher.class_eval do
class <<self
Expand All @@ -13,7 +13,7 @@ def reload_application
end
end
end
elsif Rails.version =~ /\A2.[12]/
elsif active_record21? || active_record22?
::ActionController::Dispatcher.class_eval do
alias_method :origin_reload_application, :reload_application
def reload_application
Expand Down
62 changes: 62 additions & 0 deletions lib/bullet/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,67 @@ def active_record?
false
end
end

def active_record_version
@active_record_version ||= begin
if active_record2?
'active_record2'
elsif active_record30?
'active_record3'
elsif active_record31? || active_record32?
'active_record31'
end
end
end

def mongoid_version
@mongoid_version ||= begin
if mongoid24?
'mongoid24'
elsif mongoid3?
'mongoid3'
end
end
end

def active_record2?
::ActiveRecord::VERSION::MAJOR == 2
end

def active_record23?
active_record2? && ::ActiveRecord::VERSION::MINOR == 3
end

def active_record22?
active_record2? && ::ActiveRecord::VERSION::MINOR == 2
end

def active_record21?
active_record2? && ::ActiveRecord::VERSION::MINOR == 1
end

def active_record3?
::ActiveRecord::VERSION::MAJOR == 3
end

def active_record30?
active_record3? && ::ActiveRecord::VERSION::MINOR == 0
end

def active_record31?
active_record3? && ::ActiveRecord::VERSION::MINOR == 1
end

def active_record32?
active_record3? && ::ActiveRecord::VERSION::MINOR == 2
end

def mongoid24?
::Mongoid::VERSION =~ /\A2\.4/
end

def mongoid3?
::Mongoid::VERSION =~ /\A3/
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/association_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

if Rails.version.to_i > 2
if active_record3?
describe Bullet::Detector::Association, 'has_many' do
before(:each) do
Bullet.clear
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/counter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

if Rails.version.to_i > 2
if active_record3?
describe Bullet::Detector::Counter do
before(:each) do
Bullet.start_request
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/rails2/association_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

if Rails.version.to_i == 2
if active_record2?
describe Bullet::Detector::Association, 'has_many' do
before(:each) do
Bullet.clear
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/rails2/counter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

if Rails.version.to_i > 2
if active_record2?
describe Bullet::Detector::Counter do
before(:each) do
Bullet.start_request
Expand Down
4 changes: 3 additions & 1 deletion spec/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Post < ActiveRecord::Base
extend Bullet::Dependency

belongs_to :category
belongs_to :writer
has_many :comments

if Rails.version.to_i == 2
if active_record2?
named_scope :preload_comments, lambda { {:include => :comments} }
named_scope :in_category_name, lambda { |name|
{:conditions => ['categories.name = ?', name], :include => :category}
Expand Down
42 changes: 21 additions & 21 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# rails 2.3
require 'initializer'
end
require 'active_record'
require 'action_controller'

module Rails
Expand All @@ -20,38 +19,39 @@ def root
require 'bullet'
extend Bullet::Dependency
Bullet.enable = true
ActiveRecord::Migration.verbose = false

MODELS = File.join(File.dirname(__FILE__), "models")
$LOAD_PATH.unshift(MODELS)

# Autoload every model for the test suite that sits in spec/models.
Dir[ File.join(MODELS, "*.rb") ].sort.each do |filename|
name = File.basename(filename, ".rb")
autoload name.camelize.to_sym, name
end

SUPPORT = File.join(File.dirname(__FILE__), "support")
Dir[ File.join(SUPPORT, "*.rb") ].reject { |filename| filename =~ /mongo_seed.rb$/ }.sort.each { |file| require file }
Dir[ File.join(SUPPORT, "*.rb") ].reject { |filename| filename =~ /_seed.rb$/ }.sort.each { |file| require file }

RSpec.configure do |config|
config.before(:suite) do
Support::SqliteSeed.setup_db
Support::SqliteSeed.seed_db
end

config.after(:suite) do
Support::SqliteSeed.teardown_db
end

config.extend Bullet::Dependency

config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end

if active_record?
ActiveRecord::Migration.verbose = false

# Autoload every active_record model for the test suite that sits in spec/models.
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require file }
require File.join(SUPPORT, "sqlite_seed.rb")

RSpec.configure do |config|
config.before(:suite) do
Support::SqliteSeed.setup_db
Support::SqliteSeed.seed_db
end

config.after(:suite) do
Support::SqliteSeed.teardown_db
end
end
end

if mongoid?
# Autoload every model for the test suite that sits in spec/models.
# Autoload every mongoid model for the test suite that sits in spec/models.
Dir[ File.join(MODELS, "mongoid", "*.rb") ].sort.each { |file| require file }
require File.join(SUPPORT, "mongo_seed.rb")

Expand Down

0 comments on commit 6368686

Please sign in to comment.