<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -38,7 +38,7 @@ module DataMapper
 
         model.relationships(repository_name)[name] = Relationship.new(
           name,
-          repository_name,
+          model.repository,
           model,
           options.fetch(:class_name, Extlib::Inflection.classify(name)),
           options
@@ -60,7 +60,7 @@ module DataMapper
           return false if @parent.nil?
           return true  unless parent.new_record?
 
-          DataMapper.repository(@relationship.repository_name) do
+          @relationship.with_repository(parent) do
             parent.save
           end
         end</diff>
      <filename>lib/dm-core/associations/many_to_one.rb</filename>
    </modified>
    <modified>
      <diff>@@ -46,7 +46,7 @@ module DataMapper
 
           opts[:child_model]            ||= opts.delete(:class_name)  || Extlib::Inflection.classify(name)
           opts[:parent_model]             =   model
-          opts[:repository_name]          =   repository_name
+          opts[:repository]               =   model.repository
           opts[:near_relationship_name]   =   opts.delete(:through)
           opts[:remote_relationship_name] ||= opts.delete(:remote_name) || name
           opts[:parent_key]               =   opts[:parent_key]
@@ -56,7 +56,7 @@ module DataMapper
         else
           Relationship.new(
             name,
-            repository_name,
+            model.repository,
             options.fetch(:class_name, Extlib::Inflection.classify(name)),
             model,
             options
@@ -255,7 +255,7 @@ module DataMapper
         end
 
         def save_resource(resource, parent = @parent)
-          DataMapper.repository(@relationship.repository_name) do
+          @relationship.with_repository(resource) do
             if parent.nil? &amp;&amp; resource.model.respond_to?(:many_to_many)
               resource.destroy
             else</diff>
      <filename>lib/dm-core/associations/one_to_many.rb</filename>
    </modified>
    <modified>
      <diff>@@ -40,7 +40,7 @@ module DataMapper
           RelationshipChain.new(
             :child_model              =&gt; options.fetch(:class_name, Extlib::Inflection.classify(name)),
             :parent_model             =&gt; model,
-            :repository_name          =&gt; repository_name,
+            :repository               =&gt; model.repository,
             :near_relationship_name   =&gt; options[:through],
             :remote_relationship_name =&gt; options.fetch(:remote_name, name),
             :parent_key               =&gt; options[:parent_key],
@@ -49,7 +49,7 @@ module DataMapper
         else
           Relationship.new(
             name,
-            repository_name,
+            model.repository,
             options.fetch(:class_name, Extlib::Inflection.classify(name)),
             model,
             options</diff>
      <filename>lib/dm-core/associations/one_to_one.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,31 +5,36 @@ module DataMapper
 
       OPTIONS = [ :class_name, :child_key, :parent_key, :min, :max, :through ]
 
-      attr_reader :name, :repository_name, :options, :query
+      attr_reader :name, :repository, :options, :query
 
       def child_key
         @child_key ||= begin
-          model_properties = child_model.properties(repository_name)
-
-          child_key = parent_key.zip(@child_properties || []).map do |parent_property,property_name|
-            # TODO: use something similar to DM::NamingConventions to determine the property name
-            parent_name = Extlib::Inflection.underscore(Extlib::Inflection.demodulize(parent_model))
-            property_name ||= &quot;#{parent_name}_#{parent_property.name}&quot;.to_sym
-
-            model_properties[property_name] || DataMapper.repository(repository_name) do
-              attributes = {}
-
-              [ :length, :precision, :scale ].each do |attribute|
-                attributes[attribute] = parent_property.send(attribute)
-              end
-
-              # NOTE: hack to make each many to many child_key a true key,
-              # until I can figure out a better place for this check
-              if child_model.respond_to?(:many_to_many)
-                attributes[:key] = true
+          child_key = nil
+          with_repository(child_model) do
+            model_properties = child_model.properties
+
+            child_key = parent_key.zip(@child_properties || []).map do |parent_property,property_name|
+              # TODO: use something similar to DM::NamingConventions to determine the property name
+              parent_name = Extlib::Inflection.underscore(Extlib::Inflection.demodulize(parent_model))
+              property_name ||= &quot;#{parent_name}_#{parent_property.name}&quot;.to_sym
+
+              if model_properties[property_name]
+                model_properties[property_name]
+              else
+                attributes = {}
+
+                [ :length, :precision, :scale ].each do |attribute|
+                  attributes[attribute] = parent_property.send(attribute)
+                end
+
+                # NOTE: hack to make each many to many child_key a true key,
+                # until I can figure out a better place for this check
+                if child_model.respond_to?(:many_to_many)
+                  attributes[:key] = true
+                end
+
+                child_model.property(property_name, parent_property.primitive, attributes)
               end
-
-              child_model.property(property_name, parent_property.primitive, attributes)
             end
           end
           PropertySet.new(child_key)
@@ -38,12 +43,14 @@ module DataMapper
 
       def parent_key
         @parent_key ||= begin
-          parent_key = if @parent_properties
-            parent_model.properties(repository_name).slice(*@parent_properties)
-          else
-            parent_model.key(repository_name)
+          parent_key = nil
+          with_repository(parent_model) do
+            parent_key = if @parent_properties
+              parent_model.properties.slice(*@parent_properties)
+            else
+              parent_model.key
+            end
           end
-
           PropertySet.new(parent_key)
         end
       end
@@ -59,8 +66,11 @@ module DataMapper
       # @api private
       def get_children(parent, options = {}, finder = :all, *args)
         bind_values  = parent_values = parent_key.get(parent)
-        query_values = DataMapper.repository(repository_name).identity_map(parent_model).keys.flatten
-        query_values.reject! { |k| DataMapper.repository(repository_name).identity_map(child_model)[[k]] }
+        query_values = []
+        with_repository(parent) do |r|
+          query_values = r.identity_map(parent_model).keys.flatten
+          query_values.reject! { |k| r.identity_map(child_model)[[k]] }
+        end
 
         association_accessor = &quot;#{self.name}_association&quot;
 
@@ -69,14 +79,14 @@ module DataMapper
           query[key] = query_values.empty? ? bind_values : query_values
         end
 
-        DataMapper.repository(repository_name) do
+        ret = []
+        with_repository(parent) do
           collection = child_model.send(finder, *(args &lt;&lt; @query.merge(options).merge(query)))
           return collection unless Collection === collection
           grouped_collection = collection.inject({}) do |grouped, model|
-            (grouped[get_parent(model)] ||= []) &lt;&lt; model
+            (grouped[get_parent(model, parent)] ||= []) &lt;&lt; model
             grouped
           end
-          ret = []
           grouped_collection.each do |parent, children|
             association = parent.send(association_accessor)
             parents_children = association.instance_variable_get(:@children)
@@ -87,38 +97,44 @@ module DataMapper
             end
             parent_key.get(parent) == parent_values ? ret = parents_children : association.instance_variable_set(:@children, parents_children)
           end
-          ret
         end
+        ret
       end
 
       # @api private
-      def get_parent(child)
-        bind_values = child_value = child_key.get(child)
-        return nil if child_value.any? { |bind_value| bind_value.nil? }
-        if parent = DataMapper.repository(repository_name).identity_map(parent_model)[child_value]
-          return parent
-        else
-          association_accessor = &quot;#{self.name}_association&quot;
-          children = DataMapper.repository(repository_name).identity_map(child_model)
-          children.each do |key, c|
-            bind_values |= child_key.get(c)
-          end
-          query_values = bind_values.reject { |k| DataMapper.repository(repository_name).identity_map(parent_model)[[k]] }
+      def get_parent(child, parent = nil)
+        ret = nil
+        with_repository(parent || child) do |r|
+          bind_values = child_value = child_key.get(child)
+          return nil if child_value.any? { |bind_value| bind_value.nil? }
+          if parent = r.identity_map(parent_model)[child_value]
+            return parent
+          else
+            association_accessor = &quot;#{self.name}_association&quot;
+            children = r.identity_map(child_model)
+            children.each do |key, c|
+              bind_values |= child_key.get(c)
+            end
+            query_values = bind_values.reject { |k| r.identity_map(parent_model)[[k]] }
 
-          query = {}
-          parent_key.each do |key|
-            query[key] = query_values.empty? ? bind_values : query_values
-          end
+            query = {}
+            parent_key.each do |key|
+              query[key] = query_values.empty? ? bind_values : query_values
+            end
 
-          DataMapper.repository(repository_name) do
             collection = parent_model.send(:all, query)
             collection.send(:lazy_load)
             children.each do |id, c|
-              c.send(association_accessor).instance_variable_set(:@parent, collection.get(child_key.get(c)))
+              c.send(association_accessor).instance_variable_set(:@parent, collection.get(*child_key.get(c)))
             end
-            child.send(association_accessor).instance_variable_get(:@parent)
+            ret = child.send(association_accessor).instance_variable_get(:@parent)
           end
         end
+        ret
+      end
+
+      def with_repository(instance = nil, &amp;block)
+        instance == nil ? yield(@repository) : yield(instance.repository)
       end
 
       # @api private
@@ -132,9 +148,9 @@ module DataMapper
       # and parent_properties refer to the PK.  For more information:
       # http://edocs.bea.com/kodo/docs41/full/html/jdo_overview_mapping_join.html
       # I wash my hands of it!
-      def initialize(name, repository_name, child_model, parent_model, options = {})
+      def initialize(name, repository, child_model, parent_model, options = {})
         assert_kind_of 'name',              name,              Symbol
-        assert_kind_of 'repository_name',   repository_name,   Symbol
+        # assert_kind_of 'repository_name',   repository_name,   Symbol
         assert_kind_of 'child_model',  child_model,  String, Class
         assert_kind_of 'parent_model', parent_model, String, Class
 
@@ -147,7 +163,7 @@ module DataMapper
         end
 
         @name              = name
-        @repository_name   = repository_name
+        @repository        = repository
         @child_model       = child_model
         @child_properties  = child_properties   # may be nil
         @query             = options.reject { |k,v| OPTIONS.include?(k) }</diff>
      <filename>lib/dm-core/associations/relationship.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,7 +2,7 @@ module DataMapper
   module Associations
     class RelationshipChain &lt; Relationship
       OPTIONS = [
-        :repository_name, :near_relationship_name, :remote_relationship_name,
+        :repository, :near_relationship_name, :remote_relationship_name,
         :child_model, :parent_model, :parent_key, :child_key,
         :min, :max
       ]
@@ -20,7 +20,7 @@ module DataMapper
 
         query[:links] = links
 
-        DataMapper.repository(parent.repository.name) do
+        with_repository(parent) do
           results = grandchild_model.send(finder, *(args &lt;&lt; query))
           # FIXME: remove the need for the uniq.freeze
           finder == :all ? (@mutable ? results.uniq : results.uniq.freeze) : results
@@ -55,7 +55,7 @@ module DataMapper
           raise ArgumentError, &quot;The options #{missing_options * ', '} are required&quot;, caller
         end
 
-        @repository_name          = options.fetch(:repository_name)
+        @repository               = options.fetch(:repository)
         @near_relationship_name   = options.fetch(:near_relationship_name)
         @remote_relationship_name = options.fetch(:remote_relationship_name)
         @child_model              = options.fetch(:child_model)</diff>
      <filename>lib/dm-core/associations/relationship_chain.rb</filename>
    </modified>
    <modified>
      <diff>@@ -529,6 +529,7 @@ if HAS_POSTGRES
       before :all do
         class Engine
           include DataMapper::Resource
+          def self.default_repository_name; :postgres end
 
           property :id, Serial
           property :name, String
@@ -536,14 +537,13 @@ if HAS_POSTGRES
 
         class Yard
           include DataMapper::Resource
+          def self.default_repository_name; :postgres end
 
           property :id, Serial
           property :name, String
           property :engine_id, Integer
 
-          repository(:postgres) do
-            belongs_to :engine
-          end
+          belongs_to :engine
         end
       end
 
@@ -618,25 +618,23 @@ if HAS_POSTGRES
       before :all do
         class Host
           include DataMapper::Resource
+          def self.default_repository_name; :postgres end
 
           property :id, Serial
           property :name, String
 
-          repository(:postgres) do
-            has n, :slices
-          end
+          has n, :slices
         end
 
         class Slice
           include DataMapper::Resource
+          def self.default_repository_name; :postgres end
 
           property :id, Serial
           property :name, String
           property :host_id, Integer
 
-          repository(:postgres) do
-            belongs_to :host
-          end
+          belongs_to :host
         end
       end
 </diff>
      <filename>spec/integration/postgres_adapter_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -33,6 +33,7 @@ describe &quot;Strategic Eager Loading&quot; do
 
       property :id, Serial
       property :name, String
+      property :zoo_id, Integer
 
       belongs_to :zoo
       has n, :animals
@@ -44,6 +45,7 @@ describe &quot;Strategic Eager Loading&quot; do
 
       property :id, Serial
       property :name, String
+      property :exhibit_id, Integer
 
       belongs_to :exhibit
     end</diff>
      <filename>spec/integration/strategic_eager_loading_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -14,7 +14,7 @@ describe DataMapper::Associations::ManyToOne::Proxy do
   before do
     @child        = mock('child', :kind_of? =&gt; true)
     @parent       = mock('parent')
-    @relationship = mock('relationship', :kind_of? =&gt; true, :repository_name =&gt; :default, :get_parent =&gt; @parent, :attach_parent =&gt; nil)
+    @relationship = mock('relationship', :kind_of? =&gt; true, :get_parent =&gt; @parent, :attach_parent =&gt; nil)
     @association  = DataMapper::Associations::ManyToOne::Proxy.new(@relationship, @child)
 
     @association.replace(@parent)
@@ -89,11 +89,13 @@ describe DataMapper::Associations::ManyToOne::Proxy do
       end
 
       it 'should save the parent' do
+        @relationship.should_receive(:with_repository).and_yield(@repository)
         @parent.should_receive(:save).with(no_args)
         @association.save
       end
 
       it 'should return the result of the save' do
+        @relationship.should_receive(:with_repository).and_yield(@repository)
         save_results = mock('save results')
         @parent.should_receive(:save).with(no_args).and_return(save_results)
         @association.save.object_id.should == save_results.object_id</diff>
      <filename>spec/unit/associations/many_to_one_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -36,8 +36,8 @@ describe DataMapper::Associations::OneToMany do
       end
 
       it 'should receive the repository name' do
-        DataMapper::Associations::Relationship.should_receive(:new) do |_,repository_name,_,_,_|
-          repository_name.should == :mock
+        DataMapper::Associations::Relationship.should_receive(:new) do |_,repository,_,_,_|
+          repository.name.should == :mock
         end
         repository(:mock) do
           @class.has(@class.n, :orders)
@@ -107,7 +107,7 @@ describe DataMapper::Associations::OneToMany::Proxy do
     @resource     = mock('resource', :null_object =&gt; true)
     @collection   = []
     @repository   = mock('repository', :save =&gt; nil, :kind_of? =&gt; true)
-    @relationship = mock('relationship', :get_children =&gt; @collection, :repository_name =&gt; :mock, :query =&gt; {}, :kind_of? =&gt; true)
+    @relationship = mock('relationship', :get_children =&gt; @collection, :query =&gt; {}, :kind_of? =&gt; true)
     @association  = DataMapper::Associations::OneToMany::Proxy.new(@relationship, @parent)
   end
 
@@ -124,6 +124,7 @@ describe DataMapper::Associations::OneToMany::Proxy do
     end
 
     it 'should persist the addition after saving the association' do
+      @relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
       do_add.should == return_value
       @relationship.should_receive(:attach_parent).with(@resource, @parent)
       @association.save
@@ -147,6 +148,7 @@ describe DataMapper::Associations::OneToMany::Proxy do
     end
 
     it 'should persist the removal after saving the association' do
+      @relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
       do_remove.should == return_value
       @relationship.should_receive(:attach_parent).with(@resource, nil)
       @association.save
@@ -237,6 +239,7 @@ describe DataMapper::Associations::OneToMany::Proxy do
 
     it 'should persist the removal after saving the association' do
       do_replace.should == return_value
+      @relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
       @relationship.should_receive(:attach_parent).with(@resource, nil)
       @association.save
     end
@@ -248,6 +251,7 @@ describe DataMapper::Associations::OneToMany::Proxy do
 
     it 'should persist the addition after saving the association' do
       do_replace.should == return_value
+      @relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
       @relationship.should_receive(:attach_parent).with(@children[0], @parent)
       @relationship.should_receive(:attach_parent).with(@children[1], @parent)
       @association.save</diff>
      <filename>spec/unit/associations/one_to_many_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,17 +1,20 @@
 require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
 
 describe DataMapper::Associations::Relationship do
+  before do
+    @repository = mock('repository', :name =&gt; :mock, :save =&gt; nil, :kind_of? =&gt; true)
+  end
   it &quot;should describe an association&quot; do
     belongs_to = DataMapper::Associations::Relationship.new(
       :manufacturer,
-      :mock,
+      @repository,
       'Vehicle',
       'Manufacturer',
       { :child_key =&gt; [ :manufacturer_id ] }
     )
 
     belongs_to.should respond_to(:name)
-    belongs_to.should respond_to(:repository_name)
+    belongs_to.should respond_to(:with_repository)
     belongs_to.should respond_to(:child_key)
     belongs_to.should respond_to(:parent_key)
   end
@@ -21,14 +24,16 @@ describe DataMapper::Associations::Relationship do
 
     belongs_to = DataMapper::Associations::Relationship.new(
       :manufacturer,
-      repository_name,
+      @repository,
       'Vehicle',
       'Manufacturer',
       { :child_key =&gt; [ :manufacturer_id ], :parent_key =&gt; [ :id ] }
     )
 
     belongs_to.name.should == :manufacturer
-    belongs_to.repository_name.should == repository_name
+    belongs_to.with_repository do |r|
+      r.name.should == repository_name
+    end
 
     belongs_to.child_key.should be_a_kind_of(DataMapper::PropertySet)
     belongs_to.parent_key.should be_a_kind_of(DataMapper::PropertySet)
@@ -42,14 +47,16 @@ describe DataMapper::Associations::Relationship do
 
     has_many = DataMapper::Associations::Relationship.new(
       :models,
-      repository_name,
+      @repository,
       'Vehicle',
       'Manufacturer',
       { :child_key =&gt; [:model_id] }
     )
 
     has_many.name.should == :models
-    has_many.repository_name.should == repository_name
+    has_many.with_repository do |r|
+      r.name.should == repository_name
+    end
 
     has_many.child_key.should be_a_kind_of(DataMapper::PropertySet)
     has_many.parent_key.should be_a_kind_of(DataMapper::PropertySet)</diff>
      <filename>spec/unit/associations/relationship_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>59858360b98208dce446115331b6e998828bed11</id>
    </parent>
    <parent>
      <id>37357689ff757a917dd96f8241b1030903371de5</id>
    </parent>
  </parents>
  <author>
    <name>Bernerd Schaefer</name>
    <email>bj.schaefer@gmail.com</email>
  </author>
  <url>http://github.com/sam/dm-core/commit/045d5cc50682e0c8db3df46a1882d36a7004e112</url>
  <id>045d5cc50682e0c8db3df46a1882d36a7004e112</id>
  <committed-date>2008-07-02T13:59:59-07:00</committed-date>
  <authored-date>2008-07-02T13:59:59-07:00</authored-date>
  <message>Merge branch 'belongs_to_sel'

* belongs_to_sel:
  Plenty of SEL fixes, including SEL for belongs_to side of relationships. Associations now use the repository of their child/parent class to preserve the identity maps.</message>
  <tree>39d3131aeae80eaf7152bd1bd277293aa7256aee</tree>
  <committer>
    <name>Bernerd Schaefer</name>
    <email>bj.schaefer@gmail.com</email>
  </committer>
</commit>
