Merge pull request #40792 from ghiculescu/fixture-strict-loading

Ignore strict loading violations on instances loaded through fixtures
This commit is contained in:
Eileen M. Uchitelle 2020-12-10 20:03:00 -05:00 committed by GitHub
commit 5cb6338540
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

@ -773,9 +773,12 @@ def [](key)
def find
raise FixtureClassNotFound, "No class attached to find." unless model_class
model_class.unscoped do
object = model_class.unscoped do
model_class.find(fixture[model_class.primary_key])
end
# Fixtures can't be eagerly loaded
object.instance_variable_set(:@strict_loading, false)
object
end
end
end

@ -6,6 +6,8 @@
require "models/mentor"
require "models/project"
require "models/ship"
require "models/strict_zine"
require "models/interest"
class StrictLoadingTest < ActiveRecord::TestCase
fixtures :developers
@ -438,3 +440,20 @@ def assert_logged(message)
end
end
end
class StrictLoadingFixturesTest < ActiveRecord::TestCase
fixtures :strict_zines
test "strict loading violations are ignored on fixtures" do
ActiveRecord::FixtureSet.reset_cache
create_fixtures("strict_zines")
assert_nothing_raised do
strict_zines(:going_out).interests.to_a
end
assert_raises(ActiveRecord::StrictLoadingViolationError) do
StrictZine.first.interests.to_a
end
end
end

@ -0,0 +1,2 @@
going_out:
title: Hello

@ -0,0 +1,7 @@
# frozen_string_literal: true
require "models/zine"
class StrictZine < Zine
self.strict_loading_by_default = true
end

@ -1,5 +1,5 @@
# frozen_string_literal: true
class Zine < ActiveRecord::Base
has_many :interests, inverse_of: :zine
has_many :interests, inverse_of: :zine, foreign_key: "zine_id"
end

@ -1101,6 +1101,10 @@
t.string :title
end
create_table :strict_zines, force: true do |t|
t.string :title
end
create_table :wheels, force: true do |t|
t.integer :size
t.references :wheelable, polymorphic: true