Skip to content
This repository has been archived by the owner on Sep 2, 2019. It is now read-only.

Commit

Permalink
- inheritance based querys are working
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Feuerstack authored and Sebastian Feuerstack committed Feb 6, 2012
1 parent e82528e commit 9301c94
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
22 changes: 19 additions & 3 deletions lib/dm-redis-adapter/adapter.rb
Expand Up @@ -40,7 +40,7 @@ def read(query)
records.each do |record|
record[:model] = query.model.to_s.downcase if not record[:model]

record_data = @redis.hgetall("#{record[:model]}:#{record[redis_key_for(query.model)]}")
record_data = @redis.hgetall("#{record[:model]}:#{record[redis_key_for(query.model)]}")

query.fields.each do |property|
next if query.model.key.include?(property)
Expand Down Expand Up @@ -158,8 +158,24 @@ def records_for(query)
keys = []

if query.conditions.nil?
@redis.smembers(key_set_for(query.model)).each do |key|
keys << {redis_key_for(query.model) => key.to_i}

# there might be a datamapper bug, which results in a condition==nil if an inheritance based query is
# performed on the base class - we handle this by collecting and querying all descendants - which is slow
# but works

if query.model.properties.discriminator
descendants = query.model.descendants
descendants.each do |d|
@redis.smembers(key_set_for(d)).each do |key|
keys << {:model => d.to_s.downcase,redis_key_for(d) => key.to_i}
end
end

## end of dirty fix <---
else
@redis.smembers(key_set_for(query.model)).each do |key|
keys << {redis_key_for(query.model) => key.to_i}
end
end
else
query.conditions.operands.each do |operand|
Expand Down
32 changes: 17 additions & 15 deletions spec/dm_redis_inheritenance_spec.rb
Expand Up @@ -12,13 +12,13 @@
describe 'Inheritance' do

before :all do

module MINT
class Person
include DataMapper::Resource

property :name, String
property :job, String, :length => 1..255
property :type, Discriminator
property :classtype, Discriminator
property :id, Serial
end

Expand All @@ -29,35 +29,37 @@ class Son < Male; end
class Woman < Person; end
class Mother < Woman; end
class Daughter < Woman; end

end
DataMapper.finalize

end

it 'should select all women, mothers, and daughters based on Woman query' do
w = Woman.create(:name => "woman")
m = Mother.create(:name => "mother")
d = Daughter.create(:name => "daughter")

r = Woman.all
w = MINT::Woman.create(:name => "woman")
m = MINT::Mother.create(:name => "mother")
d = MINT::Daughter.create(:name => "daughter")

r = MINT::Woman.all
r.to_set.should == [w,m,d].to_set
r.size.should == [w,m,d].size
end

it 'should select all women, mothers, and daughters based on Person query' do
w = Woman.create(:name => "woman")
m = Mother.create(:name => "mother")
d = Daughter.create(:name => "daughter")
p = Person.all
w = MINT::Woman.create(:name => "woman")
m = MINT::Mother.create(:name => "mother")
d = MINT::Daughter.create(:name => "daughter")
p = MINT::Person.all
p.to_set.should == [w,m,d].to_set
p.size.should == [w,m,d].size
end

it 'should select all mothers' do
w = Woman.create(:name => "woman")
m = Mother.create(:name => "mother")
d = Daughter.create(:name => "daughter")
w = MINT::Woman.create(:name => "woman")
m = MINT::Mother.create(:name => "mother")
d = MINT::Daughter.create(:name => "daughter")

mo = Mother.all
mo = MINT::Mother.all
mo.to_set.should == [m].to_set
mo.size.should == [m].size
end
Expand Down

0 comments on commit 9301c94

Please sign in to comment.