Skip to content

Commit

Permalink
push constant lookup in the a cache object
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 5, 2013
1 parent 3b35103 commit 8588082
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions activerecord/lib/active_record/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,26 @@ def self.instantiate_all_loaded_fixtures(object, load_instances = true)
cattr_accessor :all_loaded_fixtures
self.all_loaded_fixtures = {}

class ClassCache
def initialize(class_names, config)
@class_names = class_names.dup.stringify_keys
@config = config
end

def [](fs_name)
@class_names[fs_name] ||= default_fixture_model(fs_name, @config).safe_constantize
end

private

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

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

# FIXME: Apparently JK uses this.
connection = block_given? ? yield : ActiveRecord::Base.connection
Expand All @@ -452,10 +469,11 @@ def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}
fixtures_map = {}

fixture_sets = files_to_read.map do |fs_name|
klass = class_names[fs_name]
fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new
connection,
fs_name,
class_names[fs_name] || (default_fixture_model_name(fs_name, config).safe_constantize),
klass,
::File.join(fixtures_directory, fs_name))
end

Expand Down Expand Up @@ -613,6 +631,7 @@ def primary_key_name
end

def handle_habtm(rows, row, association)
# This is the case when the join table has no fixtures file
if (targets = row.delete(association.name.to_s))
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
table_name = association.join_table
Expand Down

0 comments on commit 8588082

Please sign in to comment.