|
From: Hans F. <ha...@fu...> - 2003-12-20 16:30:11
|
I have an idea for the ordering stuff. Position is stored in the
ingredient itself; why not make the ingredients array a set? Then the
array only ever changes when the set changes (addition or deletion) but
not when the ordering changes. To get the items in order, just do
recipe.ingredients.sort. (we need to def <=3D> for Ingredient, of course)
To change orders, just twiddle the position numbers - no array
notification since the array itself doesn't change, just the items.
Move the repositioning (promote/demote) into the presenter. When the
presenter has done its thing, it can call ingredientList.update.
/* Quoth lu...@us...
on Fri, 19 Dec 2003 at 22:38 -0800
in <E1A...@sc...> */
> Update of /cvsroot/neelix/neelix/model
> In directory sc8-pr-cvs1:/tmp/cvs-serv27370/model
>=20
> Modified Files:
> model.rb=20
> 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 c=
all
> 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).
>=20
>=20
> Index: model.rb
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> 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
> =20
> + class ObservableArray
> + # should these instead be in a subclass of ObservableArray?
> + def promote(obj)
> + i =3D self.index(obj)
> + return if i.nil? or i =3D=3D 0
> +=20
> + tmp =3D self[i]
> + send("__old_[]=3D", i, self[i - 1]);
> + send("__old_[]=3D", i - 1, tmp);
> + end
> + def demote(obj)
> + i =3D self.index(obj)
> + return if i.nil? or i =3D=3D self.length - 1
> +=20
> + tmp =3D self[i]
> + send("__old_[]=3D", i, self[i + 1]);
> + send("__old_[]=3D", i + 1, tmp);
> + end
> + wrap_method :promote, :demote
> + end
> +=20
> class Food
> include Observable
> ***************
> *** 166,169 ****
> --- 187,202 ----
> end
> =20
> + def position
> + @dbh.select_one("select position from ingredient where ingredient_id=
=3D?",@id)['position']
> + end
> + def position=3D(p)
> + p2 =3D position
> + if p2 !=3D p then
> + @dbh.do("update ingredient set position=3D? where ingredient_id=3D=
?", p, @id)
> + notify_observers
> + end
> + p
> + end
> +=20
> def to_s
> s =3D "#{quantity} #{measure} #{food}"
> ***************
> *** 186,190 ****
> @id =3D recipe_id
> @ingredients =3D ObservableArray.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe_id=
=3D?", @id).each do |row|
> @ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> --- 219,223 ----
> @id =3D recipe_id
> @ingredients =3D ObservableArray.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe_id=
=3D? 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 =3D Array.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe=
_id=3D?",@id).each do |row|
> old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> --- 225,229 ----
> # which ingredients were previously in the DB?
> old_ingredients =3D Array.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe=
_id=3D? 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=3D?",ingredient.i=
d)
> + end
> +=20
> + # reorder the rows in the database
> + position =3D 1
> + @ingredients.each do |ingredient|
> + ingredient.position =3D position
> + position +=3D 1
> end
> }
>=20
>=20
>=20
>=20
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
> Free Linux Tutorials. Learn everything from the bash shell to sys admin.
> Click now! http://ads.osdn.com/?ad_id=3D1278&alloc_id=3D3371&op=3Dclick
> _______________________________________________
> neelix-devel mailing list
> nee...@li...
> https://lists.sourceforge.net/lists/listinfo/neelix-devel
>=20
--=20
Hans Fugal | De gustibus non disputandum est.
http://hans.fugal.net/ | Debian, vim, mutt, ruby, text, gpg
http://gdmxml.fugal.net/ | WindowMaker, gaim, UTF-8, RISC, JS Bach
---------------------------------------------------------------------
GnuPG Fingerprint: 6940 87C5 6610 567F 1E95 CB5E FC98 E8CD E0AA D460
|