Prefer to instantiate fixtures with model classes instead of their names, avoiding excess constant lookups. Closes #10677 [nwilmes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8561 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2008-01-05 02:20:57 +00:00
parent f42063d038
commit 83f65594de

View File

@@ -689,7 +689,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
Dir.entries(@fixture_path).each do |file|
path = File.join(@fixture_path, file)
if File.file?(path) and file !~ @file_filter
self[file] = Fixture.new(path, @class_name)
self[file] = Fixture.new(path, model_class)
end
end
end
@@ -718,7 +718,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{name} (nil)"
end
self[name] = Fixture.new(data, @class_name)
self[name] = Fixture.new(data, model_class)
end
end
end
@@ -731,7 +731,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
reader.each do |row|
data = {}
row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip }
self["#{Inflector::underscore(@class_name)}_#{i+=1}"]= Fixture.new(data, @class_name)
self["#{Inflector::underscore(@class_name)}_#{i+=1}"] = Fixture.new(data, model_class)
end
end
@@ -767,9 +767,9 @@ class Fixture #:nodoc:
class FormatError < FixtureError #:nodoc:
end
attr_reader :class_name
attr_reader :model_class
def initialize(fixture, class_name)
def initialize(fixture, model_class)
case fixture
when Hash, YAML::Omap
@fixture = fixture
@@ -779,7 +779,11 @@ class Fixture #:nodoc:
raise ArgumentError, "Bad fixture argument #{fixture.inspect} during creation of #{class_name} fixture"
end
@class_name = class_name
@model_class = model_class.is_a?(Class) ? model_class : model_class.constantize rescue nil
end
def class_name
@model_class.name if @model_class
end
def each
@@ -800,21 +804,18 @@ class Fixture #:nodoc:
end
def value_list
klass = @class_name.constantize rescue nil
list = @fixture.inject([]) do |fixtures, (key, value)|
col = klass.columns_hash[key] if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base)
col = model_class.columns_hash[key] if model_class.respond_to?(:ancestors) && model_class.ancestors.include?(ActiveRecord::Base)
fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
end
list * ', '
end
def find
klass = @class_name.is_a?(Class) ? @class_name : Object.const_get(@class_name) rescue nil
if klass
klass.find(self[klass.primary_key])
if model_class
model_class.find(self[model_class.primary_key])
else
raise FixtureClassNotFound, "The class #{@class_name.inspect} was not found."
raise FixtureClassNotFound, "No class attached to find."
end
end