Skip to content

Commit d6d2fcf

Browse files
committed
Backwards compatibility for ApplicationRecord.
If ApplicationRecord is not defined, then we should fall back to ActiveRecord::Base without anything breaking.
1 parent 7634cc9 commit d6d2fcf

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

activerecord/lib/active_record/application_configuration.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ApplicationConfiguration
44

55
module ClassMethods
66
def application_record(klass = nil)
7-
return ActiveRecord::Base unless klass
7+
return base_app_record unless klass
88

99
klass = klass.class unless klass.respond_to?(:parents)
1010

@@ -13,9 +13,15 @@ def application_record(klass = nil)
1313
elsif app_record = klass.parents.detect { |p| p.respond_to?(:application_record) }
1414
app_record
1515
else
16-
ActiveRecord::Base
16+
base_app_record
1717
end
1818
end
19+
20+
private
21+
22+
def base_app_record
23+
@base_app_record ||= defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
24+
end
1925
end
2026
end
2127
end

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def instance_method_already_implemented?(method_name)
105105
raise DangerousAttributeError, "#{method_name} is defined by Active Record"
106106
end
107107

108-
if superclass == Base || superclass == ApplicationRecord
108+
if superclass == ActiveRecord::Base.application_record || superclass == Base
109109
super
110110
else
111111
# If B < A and A defines its own attribute method, then we don't want to overwrite that.

activerecord/lib/active_record/reflection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def source_reflection_name # :nodoc:
630630
Ambiguous source reflection for through association. Please specify a :source
631631
directive on your declaration like:
632632
633-
class #{active_record.name} < ApplicationRecord
633+
class #{active_record.name} < ActiveRecord::Base.application_record
634634
#{macro} :#{name}, #{example_options}
635635
end
636636

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 < ApplicationRecord
6+
class SchemaMigration < ActiveRecord::Base.application_record
77
class << self
88

99
def table_name

activerecord/lib/active_record/translation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Translation
66
def lookup_ancestors #:nodoc:
77
klass = self
88
classes = [klass]
9-
return classes if klass == ActiveRecord::Base || klass == ApplicationRecord
9+
return classes if klass == ActiveRecord::Base.application_record || klass == ActiveRecord::Base
1010

1111
while klass != klass.base_class
1212
classes << klass = klass.superclass

railties/test/application/loading_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ class Post < ActiveRecord::Base
8787
require "#{rails_root}/config/environment"
8888
setup_ar!
8989

90-
assert_equal [ApplicationRecord, ActiveRecord::SchemaMigration], ActiveRecord::Base.descendants
90+
assert_equal [ActiveRecord::SchemaMigration, ApplicationRecord], ActiveRecord::Base.descendants
9191
get "/load"
92-
assert_equal [ApplicationRecord, Post], ActiveRecord::Base.descendants
92+
assert_equal [ActiveRecord::SchemaMigration, Post, ApplicationRecord], ActiveRecord::Base.descendants
9393
get "/unload"
94-
assert_equal [ApplicationRecord], ActiveRecord::Base.descendants
94+
assert_equal [ActiveRecord::SchemaMigration, ApplicationRecord], ActiveRecord::Base.descendants
9595
end
9696

9797
test "initialize cant be called twice" do

0 commit comments

Comments
 (0)