Skip to content

Commit 7308c88

Browse files
committed
Method for finding the correct application model.
This method first checks to see if an application configuration is defined, then gets the correct ApplicationModel. Reverts to ActiveRecord::Base if it can't find anything.
1 parent cb5fa3f commit 7308c88

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

activerecord/lib/active_record/application_configuration.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@ module ApplicationConfiguration
33
extend ActiveSupport::Concern
44

55
module ClassMethods
6-
def configs_from(application)
6+
def configs_from(mod)
77
app_model = self
88

9-
application.singleton_class.instance_eval do
9+
mod.singleton_class.instance_eval do
1010
define_method(:application_model) { app_model }
1111
end
1212

1313
define_singleton_method(:configs_from_application) { application }
1414
end
15+
16+
def application_model(klass = nil)
17+
return ActiveRecord::Base unless klass
18+
19+
klass = klass.class unless klass.respond_to?(:parents)
20+
21+
if klass.respond_to?(:application_model)
22+
klass.application_model
23+
elsif app_model = klass.parents.detect { |p| p.respond_to?(:application_model) }
24+
app_model
25+
else
26+
ActiveRecord::Base
27+
end
28+
end
1529
end
1630
end
1731
end

activerecord/lib/active_record/fixtures.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def after_teardown
815815
self.use_transactional_fixtures = true
816816
self.use_instantiated_fixtures = false
817817
self.pre_loaded_fixtures = false
818-
self.config = ActiveRecord::Base
818+
self.config = ActiveRecord::Base.application_model
819819

820820
self.fixture_class_names = Hash.new do |h, fixture_set_name|
821821
h[fixture_set_name] = ActiveRecord::FixtureSet.default_fixture_model_name(fixture_set_name, self.config)
@@ -919,7 +919,7 @@ def run_in_transaction?
919919
!self.class.uses_transaction?(method_name)
920920
end
921921

922-
def setup_fixtures(config = ActiveRecord::Base)
922+
def setup_fixtures
923923
if pre_loaded_fixtures && !use_transactional_fixtures
924924
raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
925925
end
@@ -928,6 +928,7 @@ def setup_fixtures(config = ActiveRecord::Base)
928928
@fixture_connections = []
929929
@@already_loaded_fixtures ||= {}
930930

931+
config = ActiveRecord::Base.application_model
931932
# Load fixtures once and begin transaction.
932933
if run_in_transaction?
933934
if @@already_loaded_fixtures[self.class]

activerecord/lib/active_record/schema_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'active_record/base'
44

55
module ActiveRecord
6-
class SchemaMigration < ActiveRecord::Base
6+
class SchemaMigration < ApplicationModel
77
class << self
88

99
def table_name
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
class ApplicationModel < ActiveRecord::Base
22
self.abstract_class = true
3-
configs_from Rails.application
43
end

railties/test/application/rake_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_should_not_eager_load_model_for_rake
9999
end
100100

101101
def test_code_statistics_sanity
102-
assert_match "Code LOC: 9 Test LOC: 0 Code to Test Ratio: 1:0.0",
102+
assert_match "Code LOC: 8 Test LOC: 0 Code to Test Ratio: 1:0.0",
103103
Dir.chdir(app_path){ `rake stats` }
104104
end
105105

0 commit comments

Comments
 (0)