Andrew Baldwin
2010-Oct-07 13:57 UTC
Creating a grid layout based on (pairs of) model attributes
Hi, I''ve tried looking for an answer to this via Google, various forums etc but have not found one yet - which seems surprising as I don''t think the requirement is that unusual. Is there an ''off the shelf'' gem or rails plugin to create a grid or matrix based upon attributes of a model? I''ve written a mechanism to suit my immediate needs but I''m wondering if there''s a general purpose one available; or whether it''s worth making my implementation more generic and releasing it for others to use. Basically, what I''m looking to do is take a list of objects which have attributes ''name'',''A'' and ''B'' and produce a table with A across the top, B down the side and ''name'' in cells where the object matches both A and B. eg my object contains "name", "category" and "context" the range of values for each of "category" and "context" is fixed. What I''d like is (and this looks OK in fixed width fonts) +-----------+------------+-----------------+-------------+---------------------+ | | Office | Home | Car | Boat | +-----------+------------+-----------------+-------------+---------------------+ | CD Player | Acme1000 | MegaCD-III | Dashplayer | Portaplayer | +-----------+------------+-----------------+-------------+---------------------+ | Television| | BigPix HD | | miniscreen | +-----------+------------+-----------------+-------------+---------------------+ | GPS | | | Whereami-x10| Titanic-II | | | | | GetmeHome++ | Vandervecken-XX| +-----------+------------+-----------------+-------------+---------------------+ where, for example, I have an object Item and Item.name="Acme1000" Item.category="CD Player" Item.context="Office" For what it''s worth, my current implementation (which is a little more involved than this as I''m bunching several contexts together) uses a pair of hash tables to convert attributes to row and column numbers. These are used to index a 2D array whose cells are themselves arrays of names ie col=colhash(item.context) row=rowhash(item.category) matrix[row][col] << item.name I can then use nested loops in the view to create a tabular output. Is there a less messy way than this? One that can accommodate new categories or contexts easily ? Thanks -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andrew Baldwin
2010-Oct-07 14:04 UTC
Re: Creating a grid layout based on (pairs of) model attributes
Apologies - it looke OK until I hit submit ! the table looks messy in proportional fonts -- in a CSV equivalent # Category, Office, Home, Car, Boat CD Player, Acme1000, MegaCD-III, Dashplayer, Portaplayer Television, ,BigPix HD, , miniscreen GPS , , , Whereami-x10, Titanic-II GPS , , , GetmeHome++, Vandervecken-XX| -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.