Skip to content

Commit

Permalink
Merge pull request datamapper#21 from jwkoelewijn/relationships
Browse files Browse the repository at this point in the history
Applied dkubbs patch from http://pastie.org/1625163 to fix relationships
  • Loading branch information
dkubb committed May 24, 2011
2 parents 3adb145 + 6d1267e commit a077b66
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
15 changes: 11 additions & 4 deletions lib/dm-core/model.rb
Expand Up @@ -194,10 +194,12 @@ def self.extra_extensions
def self.extended(descendant)
descendants << descendant

descendant.instance_variable_set(:@valid, false)
descendant.instance_variable_set(:@base_model, descendant)
descendant.instance_variable_set(:@storage_names, {})
descendant.instance_variable_set(:@default_order, {})
descendant.instance_eval do
@valid = false unless instance_variable_defined?(:@valid)
@base_model ||= descendant
@storage_names ||= {}
@default_order ||= {}
end

descendant.extend(Chainable)

Expand All @@ -210,6 +212,11 @@ def self.extended(descendant)
def inherited(descendant)
descendants << descendant

descendant.instance_eval do
@valid = false unless instance_variable_defined?(:@valid)
@base_model ||= base_model
end

descendant.instance_variable_set(:@valid, false)
descendant.instance_variable_set(:@base_model, base_model)
descendant.instance_variable_set(:@storage_names, @storage_names.dup)
Expand Down
11 changes: 8 additions & 3 deletions lib/dm-core/model/property.rb
Expand Up @@ -9,13 +9,18 @@ module Property
extend Chainable

def self.extended(model)
model.instance_variable_set(:@properties, {})
model.instance_variable_set(:@field_naming_conventions, {})
model.instance_eval do
@properties ||= {}
@field_naming_conventions ||= {}
end
end

chainable do
def inherited(model)
model.instance_variable_set(:@properties, {})
model.instance_eval do
@properties ||= {}
end

model.instance_variable_set(:@field_naming_conventions, @field_naming_conventions.dup)

@properties.each do |repository_name, properties|
Expand Down
8 changes: 6 additions & 2 deletions lib/dm-core/model/relationship.rb
Expand Up @@ -18,7 +18,9 @@ module Relationship
#
# @api private
def self.extended(model)
model.instance_variable_set(:@relationships, {})
model.instance_eval do
@relationships ||= {}
end
end

chainable do
Expand All @@ -27,7 +29,9 @@ def self.extended(model)
#
# @api private
def inherited(model)
model.instance_variable_set(:@relationships, {})
model.instance_eval do
@relationships ||= {}
end

@relationships.each do |repository_name, relationships|
model_relationships = model.relationships(repository_name)
Expand Down
61 changes: 61 additions & 0 deletions spec/public/model/relationship_spec.rb
Expand Up @@ -1038,3 +1038,64 @@ class ::Company
end
end
end

describe DataMapper::Associations do
before :all do
module ::Vehicle
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

def vehicle_with_wheels(*args)
include DataMapper::Resource

property :id, DataMapper::Property::Serial
property :license_plate, String
end
end
end

class Exhaust
include DataMapper::Resource

property :id, Serial
belongs_to :car
end

class Suspension
include DataMapper::Resource

property :id, Serial
property :brand, String
end

class Car
include DataMapper::Resource
has n, :suspensions
end

class Car
include DataMapper::Resource
include Vehicle

vehicle_with_wheels

property :number_of_seats, Integer
has n, :exhausts

end

DataMapper.finalize
end

it "should have all relationships when Resource is included once" do
Exhaust.relationships.any? {|r| r.name == :car }.should be_true
end

it "should not forget about relationships when Resource is included more than once" do
Car.relationships.any? {|r| r.name == :exhausts }.should be_true
Car.relationships.any? {|r| r.name == :suspensions }.should be_true
end
end

0 comments on commit a077b66

Please sign in to comment.