diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-05 02:20:57 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-05 02:20:57 +0000 |
commit | 83f65594dedd2f969bc28ba1ce424a40224f3161 (patch) | |
tree | 701a750cd54c5391ef780081fff55b0e6c4f210b /activerecord | |
parent | f42063d038a04d7c43ce617d727b0fc6f91f888b (diff) | |
download | rails-83f65594dedd2f969bc28ba1ce424a40224f3161.tar.gz rails-83f65594dedd2f969bc28ba1ce424a40224f3161.tar.bz2 rails-83f65594dedd2f969bc28ba1ce424a40224f3161.zip |
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
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index a0f62e890e..bbd42c5a80 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -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 |