Permalink
Browse files

Implmented updating index on update

  • Loading branch information...
Makoto Inoue
Makoto Inoue committed Nov 12, 2008
1 parent 5da1443 commit 5ba8e1ff2f0b4dee9c4ffe268dcd697bb37672e5
Showing with 36 additions and 15 deletions.
  1. +23 −0 lib/tc_adapter.rb
  2. +13 −15 spec/tc_adapater_spec.rb
View
@@ -68,6 +68,29 @@ def read_one(query)
def update(attributes, query)
item_id = get_id(query)
+
+ old_attributes = get_items_from_id(query, item_id)
+
+ old_attributes.reject{|k,v| k == :id || v == nil}.each do | k, v|
+ access_data(query.model, k) do |item|
+ items = item.getlist(v)
+ items = items - [item_id]
+ item.out(v)
+ if items.size > 0
+ item.putlist(v, items)
+ end
+ end
+ end
+
+ # Creating index for each attributes except id
+ attributes.each do |key, value|
+ unless key.name == :id
+ access_data(query.model, key.name) do |item|
+ item.putlist(value, [item_id])
+ end
+ end
+ end
+
access_data(query.model) do |item|
raw_data = item.get(item_id)
if raw_data
View
@@ -47,33 +47,31 @@
end
describe "update" do
- it "should update an item" do
+ before(:each) do
@user.name = 'peter'
@user.age = 22
@user.save
- user = User.get(@user.id)
- user.name.should == @user.name
- user.age.should == @user.age
+ end
+ it "should update an item" do
+ User.get(@user.id) == @user
end
- # it "should reflect index" do
- # @user.name = 'thomas'
- # @user.save
- # User.first(:name => 'thomas').should == @user
- # end
+ it "should reflect index" do
+ User.first(:name => @user.name).should == @user
+ end
end
-
+
describe "destroy" do
- it "should destroy an item" do
+ before(:each) do
@user.destroy
+ end
+ it "should destroy an item" do
lambda{User.get!(@user.id)}.should raise_error(DataMapper::ObjectNotFoundError)
- User.first(:name => @user.name).should == nil
- User.all(:name => @user.name).should == []
end
-
it "should reflect index" do
- pending
+ User.first(:name => @user.name).should == nil
+ User.all(:name => @user.name).should == []
end
end
end

0 comments on commit 5ba8e1f

Please sign in to comment.