Skip to content

Commit 26e9b14

Browse files
wangjohnrafaelfranca
authored andcommitted
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 61e440d commit 26e9b14

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
@@ -837,7 +837,7 @@ def after_teardown
837837
self.use_transactional_fixtures = true
838838
self.use_instantiated_fixtures = false
839839
self.pre_loaded_fixtures = false
840-
self.config = ActiveRecord::Base
840+
self.config = ActiveRecord::Base.application_model
841841

842842
self.fixture_class_names = Hash.new do |h, fixture_set_name|
843843
h[fixture_set_name] = ActiveRecord::FixtureSet.default_fixture_model_name(fixture_set_name, self.config)
@@ -916,7 +916,7 @@ def run_in_transaction?
916916
!self.class.uses_transaction?(method_name)
917917
end
918918

919-
def setup_fixtures(config = ActiveRecord::Base)
919+
def setup_fixtures
920920
if pre_loaded_fixtures && !use_transactional_fixtures
921921
raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
922922
end
@@ -925,6 +925,7 @@ def setup_fixtures(config = ActiveRecord::Base)
925925
@fixture_connections = []
926926
@@already_loaded_fixtures ||= {}
927927

928+
config = ActiveRecord::Base.application_model
928929
# Load fixtures once and begin transaction.
929930
if run_in_transaction?
930931
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
def primary_key
99
nil
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)