Permalink
Browse files

Make sure properties with indexes are deleted properly

This fixes a problem with deleting an object and it not being deleted
out of an association
  • Loading branch information...
1 parent 2c0c98f commit 65cf46f3e544ea7cfcaa53309a5148041dbc4cff @sheuer sheuer committed Apr 27, 2012
Showing with 43 additions and 2 deletions.
  1. +2 −2 lib/dm-redis-adapter/adapter.rb
  2. +41 −0 spec/dm_redis_associations_spec.rb
@@ -92,10 +92,10 @@ def update(attributes, collection)
# @api semipublic
def delete(collection)
collection.each do |record|
- @redis.del("#{collection.query.model.to_s.downcase}:#{record[redis_key_for(collection.query.model)]}")
+ @redis.del("#{collection.query.model.storage_name}:#{record[redis_key_for(collection.query.model)]}")
@redis.srem(key_set_for(collection.query.model), record[redis_key_for(collection.query.model)])
record.model.properties.select {|p| p.index}.each do |p|
- @redis.srem("#{collection.query.model.to_s.downcase}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)])
+ @redis.srem("#{collection.query.model.storage_name}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)])
end
end
end
@@ -10,6 +10,47 @@
@redis.flushdb
end
+ it "should delete from association" do
+ class Book
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :name, String
+
+ has n, :book_tags
+ has n, :tags, :through => :book_tags
+ end
+
+ class Tag
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :name, String
+
+ has n, :book_tags
+ has n, :books, :through => :book_tags
+ end
+
+ class BookTag
+ include DataMapper::Resource
+
+ property :id, Serial
+
+ belongs_to :book
+ belongs_to :tag
+ end
+
+ DataMapper.finalize
+
+ b = Book.create(:name => "Harry Potter")
+ t = b.tags.create(:name => "fiction")
+
+ t.destroy
+
+ b2 = Book.get(b.id)
+ b2.tags.should_not == [t]
+ end
+
it "should allow has n :through" do
class Book
include DataMapper::Resource

0 comments on commit 65cf46f

Please sign in to comment.