Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Updated relationship initialization to be able to support more complex r... #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/dm-core/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,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)

extra_extensions.each { |mod| descendant.extend(mod) }
Expand All @@ -224,6 +225,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
10 changes: 7 additions & 3 deletions lib/dm-core/model/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ module Property
Model.append_extensions self, DataMapper::Property::Lookup

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


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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ module Relationship
#
# @api private
def self.extended(model)
model.instance_variable_set(:@relationships, {})
model.instance_eval do
@relationships ||= {}
end
end

# When DataMapper model is inherited, relationships
# of parent are duplicated and copied to subclass 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
Original file line number Diff line number Diff line change
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