tag:blogger.com,1999:blog-1502761093196431512.post8773164958551324784..comments2024-01-06T14:58:41.477+11:00Comments on Ruby-coloured glasses: Super Cool Smart Column SortingTaryn Easthttp://www.blogger.com/profile/00647732421144825421noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-1502761093196431512.post-56091090218859757472008-09-04T11:41:00.000+10:002008-09-04T11:41:00.000+10:00Hi Esebee,I implemented this too, but it adds quit...Hi Esebee,<BR/><BR/>I implemented this too, but it adds quite a bit of complexity and I wa never truly happy with the results. Mainy because sorting by something other than the actual model meant I needed to sort (and paginate) over a ruby collection, rather than doing my pagination all nicely in ActiveRecord == quicker SQL.<BR/><BR/>I'm sure there would be an aternative, but we got stuck with the implementation.<BR/><BR/>In any case what we did required adding a "special_sort" tag for these<BR/>columns - which got passed into the sort_link (and thus was picked up by the<BR/>controller when the user clicked on them. Display of the column headings was<BR/>exactly the same.<BR/><BR/>The controller then went into the "pull everything out and sort it<BR/>afterwards" mode when it received this tag - and it used the name of the<BR/>column to figure out what sort to use.<BR/><BR/>Eg we had and Order which would :belong_to a user, and we'd want to display<BR/>a "user name" column. So we'd have:<BR/><%= column_sort_link 'User', :user, :special_sort => true -%><BR/><BR/>Then in the model we'd have a list of the allowed special columns vs the<BR/>method to use to sort_by:<BR/><BR/>def special_sorts<BR/> {'user' => {:method => lambda {|x| x.user ? x.user.email : '' },<BR/> :string => true},<BR/> 'status' => {:method => lambda {|x| x.status.to_s },<BR/> :string => true},<BR/> 'num_widgets' => {:method => lambda {|x| x.widgets.size } },<BR/> }<BR/>end<BR/><BR/><BR/>then in the controller it was something along the line of:<BR/><BR/>if params[:special_sort]<BR/> @orders = Order.find(:all)<BR/> opts = Order.special_sorts[params[:col]]<BR/> @orders.sort do |a,b| <BR/> obj_cmp(a,b,dir, opts[:method], opts[:string] ? opts[:string] : false)<BR/> end<BR/>end<BR/><BR/>Note I had to separate string and numeric sorting - dpending on what we were sorting... Note also I used the sorting for related objects, and also generated things (eg the "displayable status field").<BR/><BR/>Hope this helps.Taryn Easthttps://www.blogger.com/profile/00647732421144825421noreply@blogger.comtag:blogger.com,1999:blog-1502761093196431512.post-50793272960471600132008-09-04T07:07:00.000+10:002008-09-04T07:07:00.000+10:00HiThis is awesome stuff. I have a situation in my ...Hi<BR/>This is awesome stuff. I have a situation in my RoR project where I have models dependent on each other.<BR/><BR/>class XYZ < ActiveRecord::Base<BR/> belongs_to :zone<BR/> belongs_to :keyvalue<BR/> belongs_to :category<BR/>end <BR/><BR/>When I display XYZ, the user wants to see zone names from the zone table, etc. They also want to be able to sort the zone names in this view. Can you give me an idea how to approach this sort?<BR/><BR/>Thanks a lot<BR/>Somoes_es_beehttps://www.blogger.com/profile/11090514613692753502noreply@blogger.com