|
From: <lu...@us...> - 2003-12-20 06:38:38
|
Update of /cvsroot/neelix/neelix/model
In directory sc8-pr-cvs1:/tmp/cvs-serv27370/model
Modified Files:
model.rb
Log Message:
"Move Up" works! We may want to find some nicer way of doing the
promote/demote, but this was the only way I could get around the notify call
halfway through the swap (which is a Bad Thing [tm], since one of the
ingredients is just plain deleted if the notification happens too soon).
Index: model.rb
===================================================================
RCS file: /cvsroot/neelix/neelix/model/model.rb,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** model.rb 15 Dec 2003 20:11:54 -0000 1.4
--- model.rb 20 Dec 2003 06:38:35 -0000 1.5
***************
*** 41,44 ****
--- 41,65 ----
end
+ class ObservableArray
+ # should these instead be in a subclass of ObservableArray?
+ def promote(obj)
+ i = self.index(obj)
+ return if i.nil? or i == 0
+
+ tmp = self[i]
+ send("__old_[]=", i, self[i - 1]);
+ send("__old_[]=", i - 1, tmp);
+ end
+ def demote(obj)
+ i = self.index(obj)
+ return if i.nil? or i == self.length - 1
+
+ tmp = self[i]
+ send("__old_[]=", i, self[i + 1]);
+ send("__old_[]=", i + 1, tmp);
+ end
+ wrap_method :promote, :demote
+ end
+
class Food
include Observable
***************
*** 166,169 ****
--- 187,202 ----
end
+ def position
+ @dbh.select_one("select position from ingredient where ingredient_id=?",@id)['position']
+ end
+ def position=(p)
+ p2 = position
+ if p2 != p then
+ @dbh.do("update ingredient set position=? where ingredient_id=?", p, @id)
+ notify_observers
+ end
+ p
+ end
+
def to_s
s = "#{quantity} #{measure} #{food}"
***************
*** 186,190 ****
@id = recipe_id
@ingredients = ObservableArray.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=?", @id).each do |row|
@ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
--- 219,223 ----
@id = recipe_id
@ingredients = ObservableArray.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=? order by position", @id).each do |row|
@ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
***************
*** 192,196 ****
# which ingredients were previously in the DB?
old_ingredients = Array.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=?",@id).each do |row|
old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
--- 225,229 ----
# which ingredients were previously in the DB?
old_ingredients = Array.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=? order by position",@id).each do |row|
old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
***************
*** 204,207 ****
--- 237,247 ----
(old_ingredients - @ingredients).each do |ingredient|
@dbh.do("delete from ingredient where ingredient_id=?",ingredient.id)
+ end
+
+ # reorder the rows in the database
+ position = 1
+ @ingredients.each do |ingredient|
+ ingredient.position = position
+ position += 1
end
}
|