lib/fixture_dependencies.rb
# Load the individual fixture into the database, by loading all necessary
# belongs_to dependencies before saving the model, and all has_*
# dependencies after saving the model.  If the model already exists in
# the database, return it.  Will check the yaml file for fixtures if no
# fixtures yet exist for the model.  If the fixture isn't in the fixture
# hash, raise an error.
Load the individual fixture into the database, by loading all necessary
belongs_to dependencies before saving the model, and all has_
dependencies after saving the model.  If the model already exists in
the database, return it.  Will check the yaml file for fixtures if no
fixtures yet exist for the model.  If the fixture isn't in the fixture
hash, raise an error.
Load the individual fixture into the database by loading all necessary belongs_to dependencies before saving the model and all has_ dependencies after saving the model . If the model already exists in the database return it . Will check the yaml file for fixtures if no fixtures yet exist for the model . If the fixture isn ' t in the fixture hash raise an error .
def use(record, loading = [], procs = {})
    spaces = " " * loading.length
    puts "#{spaces}using #{record}" if verbose > 0
    puts "#{spaces}load stack:#{loading.inspect}" if verbose > 1
    loading.push(record)
    model_name, name = split_name(record)
    model = model_name.camelize.constantize
    unless loaded[model_name.to_sym]
      puts "#{spaces}loading #{model.table_name}.yml" if verbose > 0
      load_yaml(model_name) 
    end
    mtype = model_type(model)
    model_method(:raise_model_error, mtype, "Couldn't use fixture #{record.inspect}") unless attributes = fixtures[model_name.to_sym][name.to_sym]
    if existing_obj = model_method(:model_find_by_pk, mtype, model, attributes[model.primary_key.to_sym])
      puts "#{spaces}using #{record}: already in database" if verbose > 2
      return existing_obj
    end
    obj = model.new
    many_associations = []
    attributes.each do |attr, value|
      if reflection = model_method(:reflection, mtype, model, attr.to_sym)
        if [:belongs_to, :many_to_one].include?(model_method(:reflection_type, mtype, reflection))
          dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}".to_sym
          if dep_name == record
            puts "#{spaces}#{record}.#{attr}: belongs_to self-referential" if verbose > 1
            attr = model_method(:reflection_key, mtype, reflection)
            value = attributes[model.primary_key.to_sym]
          elsif loading.include?(dep_name)
            puts "#{spaces}#{record}.#{attr}: belongs-to cycle detected:#{dep_name}" if verbose > 1
            (procs[dep_name] ||= []) << Proc.new do |assoc|
              m = model_method(:model_find, mtype, model, attributes[model.primary_key.to_sym])
              m.send("#{attr}=", assoc)
              model_method(:model_save, mtype, m)
            end
            value = nil
          else
            puts "#{spaces}#{record}.#{attr}: belongs_to:#{dep_name}" if verbose > 1
            use(dep_name, loading, procs)
            value = get(dep_name)
          end
        elsif
          many_associations << [attr, reflection, model_method(:reflection_type, mtype, reflection) == :has_one ? [value] : value]
          next
        end
      end
      puts "#{spaces}#{record}.#{attr} = #{value.inspect}" if verbose > 2
      obj.send("#{attr}=", value)
    end
    puts "#{spaces}saving #{record}" if verbose > 1
    model_method(:model_save, mtype, obj)
    fixtures[model_name.to_sym][name.to_sym][model.primary_key.to_sym] ||= obj[model.primary_key.to_sym]
    loading.pop
    if procs[record]
      procs[record].each{|p| p.call(obj)} 
      procs.delete(record)
    end
    many_associations.each do |attr, reflection, values|
      values.each do |value|
        dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}".to_sym
        rtype = model_method(:reflection_type, mtype, reflection) if verbose > 1
        if dep_name == record
          puts "#{spaces}#{record}.#{attr}: #{rtype} self-referential" if verbose > 1
          model_method(:add_associated_object, mtype, reflection, attr, obj, obj)
        elsif loading.include?(dep_name)
          puts "#{spaces}#{record}.#{attr}: #{rtype} cycle detected:#{dep_name}" if verbose > 1
          (procs[dep_name] ||= []) << Proc.new do |assoc|
            model_method(:add_associated_object, mtype, reflection, attr, obj, assoc)
          end
        else
          puts "#{spaces}#{record}.#{attr}: #{rtype}:#{dep_name}" if verbose > 1
          model_method(:add_associated_object, mtype, reflection, attr, obj, use(dep_name, loading, procs))
        end
      end
    end
    obj
  end
def use ( record , loading = [ ] , procs = { } ) spaces = " " * loading . length puts "#{spaces}using #{record}" if verbose > 0 puts "#{spaces}load stack:#{loading.inspect}" if verbose > 1 loading . push ( record ) model_name , name = split_name ( record ) model = model_name . camelize . constantize unless loaded [ model_name . to_sym ] puts "#{spaces}loading #{model.table_name}.yml" if verbose > 0 load_yaml ( model_name ) end mtype = model_type ( model ) model_method ( :raise_model_error , mtype , "Couldn't use fixture #{record.inspect}" ) unless attributes = fixtures [ model_name . to_sym ] [ name . to_sym ] if existing_obj = model_method ( :model_find_by_pk , mtype , model , attributes [ model . primary_key . to_sym ] ) puts "#{spaces}using #{record}: already in database" if verbose > 2 return existing_obj end obj = model . new many_associations = [ ] attributes . each do | attr , value | if reflection = model_method ( :reflection , mtype , model , attr . to_sym ) if [ :belongs_to , :many_to_one ] . include? ( model_method ( :reflection_type , mtype , reflection ) ) dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}" . to_sym if dep_name == record puts "#{spaces}#{record}.#{attr}: belongs_to self-referential" if verbose > 1 attr = model_method ( :reflection_key , mtype , reflection ) value = attributes [ model . primary_key . to_sym ] elsif loading . include? ( dep_name ) puts "#{spaces}#{record}.#{attr}: belongs-to cycle detected:#{dep_name}" if verbose > 1 ( procs [ dep_name ] ||= [ ] ) << Proc . new do | assoc | m = model_method ( :model_find , mtype , model , attributes [ model . primary_key . to_sym ] ) m . send ( "#{attr}=" , assoc ) model_method ( :model_save , mtype , m ) end value = nil else puts "#{spaces}#{record}.#{attr}: belongs_to:#{dep_name}" if verbose > 1 use ( dep_name , loading , procs ) value = get ( dep_name ) end elsif many_associations << [ attr , reflection , model_method ( :reflection_type , mtype , reflection ) == :has_one ? [ value ] : value ] next end end puts "#{spaces}#{record}.#{attr} = #{value.inspect}" if verbose > 2 obj . send ( "#{attr}=" , value ) end puts "#{spaces}saving #{record}" if verbose > 1 model_method ( :model_save , mtype , obj ) fixtures [ model_name . to_sym ] [ name . to_sym ] [ model . primary_key . to_sym ] ||= obj [ model . primary_key . to_sym ] loading . pop if procs [ record ] procs [ record ] . each { | p | p . call ( obj ) } procs . delete ( record ) end many_associations . each do | attr , reflection , values | values . each do | value | dep_name = "#{model_method(:reflection_class, mtype, reflection).name.underscore}__#{value}" . to_sym rtype = model_method ( :reflection_type , mtype , reflection ) if verbose > 1 if dep_name == record puts "#{spaces}#{record}.#{attr}: #{rtype} self-referential" if verbose > 1 model_method ( :add_associated_object , mtype , reflection , attr , obj , obj ) elsif loading . include? ( dep_name ) puts "#{spaces}#{record}.#{attr}: #{rtype} cycle detected:#{dep_name}" if verbose > 1 ( procs [ dep_name ] ||= [ ] ) << Proc . new do | assoc | model_method ( :add_associated_object , mtype , reflection , attr , obj , assoc ) end else puts "#{spaces}#{record}.#{attr}: #{rtype}:#{dep_name}" if verbose > 1 model_method ( :add_associated_object , mtype , reflection , attr , obj , use ( dep_name , loading , procs ) ) end end end obj end
Load the individual fixture into the database, by loading all necessary
belongs_to dependencies before saving the model, and all has_
dependencies after saving the model.
Load the individual fixture into the database by loading all necessary belongs_to dependencies before saving the model and all has_ dependencies after saving the model .
# return if object has already been loaded into the database # Self referential record, use primary key # Association cycle detected, set foreign key for this model afterward using procs # This is will fail if the column is set to not null or validates_presence_of # Regular assocation, load it # after saving the model, we set the primary key within the fixture hash, in case it was not explicitly specified in the fixture and was generated by an auto_increment / serial field # Update the circular references  # Update the has_many and habtm associations # Self referential, add association # Cycle Detected, add association to this object after saving other object # Regular association, add it
record
<not_specific>
loading
<not_specific>
procs
<not_specific>