Skip to content

Commit

Permalink
Merge pull request #47827 from shouichi/remove-class-cache
Browse files Browse the repository at this point in the history
Remove ActiveRecord::FixtureSet::ClassCache
  • Loading branch information
rafaelfranca committed Jun 26, 2023
2 parents 4f36572 + 7753c36 commit 587cc9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
42 changes: 7 additions & 35 deletions activerecord/lib/active_record/fixtures.rb
Expand Up @@ -484,39 +484,6 @@ class FixtureSet

cattr_accessor :all_loaded_fixtures, default: {}

class ClassCache # :nodoc:
def initialize(class_names, config)
@class_names = class_names.stringify_keys
@config = config

# Remove string values that aren't constants or subclasses of AR
@class_names.delete_if do |klass_name, klass|
!insert_class(@class_names, klass_name, klass)
end
end

def [](fs_name)
@class_names.fetch(fs_name) do
klass = default_fixture_model(fs_name, @config).safe_constantize
insert_class(@class_names, fs_name, klass)
end
end

private
def insert_class(class_names, name, klass)
# We only want to deal with AR objects.
if klass && klass < ActiveRecord::Base
class_names[name] = klass
else
class_names[name] = nil
end
end

def default_fixture_model(fs_name, config)
ActiveRecord::FixtureSet.default_fixture_model_name(fs_name, config)
end
end

class << self
def default_fixture_model_name(fixture_set_name, config = ActiveRecord::Base) # :nodoc:
config.pluralize_table_names ?
Expand Down Expand Up @@ -571,7 +538,7 @@ def instantiate_all_loaded_fixtures(object, load_instances = true)

def create_fixtures(fixtures_directories, fixture_set_names, class_names = {}, config = ActiveRecord::Base, &block)
fixture_set_names = Array(fixture_set_names).map(&:to_s)
class_names = ClassCache.new class_names, config
class_names.stringify_keys!

# FIXME: Apparently JK uses this.
connection = block_given? ? block : lambda { ActiveRecord::Base.connection }
Expand Down Expand Up @@ -693,7 +660,6 @@ def initialize(_, name, class_name, path, config = ActiveRecord::Base)
@config = config

self.model_class = class_name

@fixtures = read_fixture_files(path)

@table_name = model_class&.table_name || self.class.default_fixture_table_name(name, config)
Expand Down Expand Up @@ -766,13 +732,19 @@ def read_fixture_files(path)
yaml_files.each_with_object({}) do |file, fixtures|
FixtureSet::File.open(file) do |fh|
self.model_class ||= fh.model_class if fh.model_class
self.model_class ||= default_fixture_model_class
self.ignored_fixtures ||= fh.ignored_fixtures
fh.each do |fixture_name, row|
fixtures[fixture_name] = ActiveRecord::Fixture.new(row, model_class)
end
end
end
end

def default_fixture_model_class
klass = ActiveRecord::FixtureSet.default_fixture_model_name(@name, @config).safe_constantize
klass if klass && klass < ActiveRecord::Base
end
end

class Fixture # :nodoc:
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -956,6 +956,16 @@ def test_uses_set_fixture_class
end
end

class FixtureWithSetModelClassPrevailsOverNamingConventionTest < ActiveRecord::TestCase
def test_model_class_in_fixture_file_is_respected
Object.const_set(:OtherPost, Class.new(ActiveRecord::Base))
other_posts = create_fixtures("other_posts").first
assert_kind_of Post, other_posts["second_welcome"].find
ensure
Object.send(:remove_const, :OtherPost)
end
end

class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
set_fixture_class funny_jokes: Joke
fixtures :funny_jokes
Expand Down

0 comments on commit 587cc9b

Please sign in to comment.