Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8561

Show
Ignore:
Timestamp:
01/05/08 02:20:57 (4 months ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/fixtures.rb

    r8560 r8561  
    690690          path = File.join(@fixture_path, file) 
    691691          if File.file?(path) and file !~ @file_filter 
    692             self[file] = Fixture.new(path, @class_name
     692            self[file] = Fixture.new(path, model_class
    693693          end 
    694694        end 
     
    719719            end 
    720720 
    721             self[name] = Fixture.new(data, @class_name
     721            self[name] = Fixture.new(data, model_class
    722722          end 
    723723        end 
     
    732732        data = {} 
    733733        row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip } 
    734         self["#{Inflector::underscore(@class_name)}_#{i+=1}"]= Fixture.new(data, @class_name
     734        self["#{Inflector::underscore(@class_name)}_#{i+=1}"] = Fixture.new(data, model_class
    735735      end 
    736736    end 
     
    768768  end 
    769769 
    770   attr_reader :class_name 
    771  
    772   def initialize(fixture, class_name
     770  attr_reader :model_class 
     771 
     772  def initialize(fixture, model_class
    773773    case fixture 
    774774      when Hash, YAML::Omap 
     
    780780    end 
    781781 
    782     @class_name = class_name 
     782    @model_class = model_class.is_a?(Class) ? model_class : model_class.constantize rescue nil 
     783  end 
     784 
     785  def class_name 
     786    @model_class.name if @model_class 
    783787  end 
    784788 
     
    801805 
    802806  def value_list 
    803     klass = @class_name.constantize rescue nil 
    804  
    805807    list = @fixture.inject([]) do |fixtures, (key, value)| 
    806       col = klass.columns_hash[key] if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base) 
     808      col = model_class.columns_hash[key] if model_class.respond_to?(:ancestors) && model_class.ancestors.include?(ActiveRecord::Base) 
    807809      fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r") 
    808810    end 
     
    811813 
    812814  def find 
    813     klass = @class_name.is_a?(Class) ? @class_name : Object.const_get(@class_name) rescue nil 
    814     if klass 
    815       klass.find(self[klass.primary_key]) 
     815    if model_class 
     816      model_class.find(self[model_class.primary_key]) 
    816817    else 
    817       raise FixtureClassNotFound, "The class #{@class_name.inspect} was not found." 
     818      raise FixtureClassNotFound, "No class attached to find." 
    818819    end 
    819820  end