From 0c5d0beac1f2479600993fb0c1ede36d7b9f1f81 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 9 Sep 2013 15:16:41 -0700 Subject: [PATCH] let the class cache object clean up user input --- activerecord/lib/active_record/fixtures.rb | 28 +++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 65648d82af..9a26e5df3f 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -440,23 +440,35 @@ module ActiveRecord 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 { |k,klass| + unless klass.is_a? Class + klass = klass.safe_constantize + ActiveSupport::Deprecation.warn("The ability to pass in strings as a class name will be removed in Rails 4.2, consider using the class itself instead.") + end + !insert_class(@class_names, k, klass) + } end def [](fs_name) @class_names.fetch(fs_name) { klass = default_fixture_model(fs_name, @config).safe_constantize - - # We only want to deal with AR objects. - if klass && klass < ActiveRecord::Base - @class_names[fs_name] = klass - else - @class_names[fs_name] = nil - end + insert_class(@class_names, fs_name, klass) } 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 @@ -479,7 +491,7 @@ module ActiveRecord fixture_sets = files_to_read.map do |fs_name| klass = class_names[fs_name] - conn = klass.respond_to?(:connection) ? klass.connection : connection + conn = klass ? klass.connection : connection fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new conn, fs_name,