I’m a litle new to RoR and trying to set up an app that lists all containers/files on a Windows Azure account. Using the waz-storage gem, I’m able to find the containers and the blobs within them but the problem I’m having is trying to order a returned list of containers by one of their metadata properties as well as filtering them based on a user email.
Using container.list I get a list of each container like this:
[#<WAZ::Blobs::Container:0x60295f8 @name="12345">, #<WAZ::Blobs::Container:0x60295b0 @name="23456">, #<WAZ::Blobs::Container:0x6029580 @name="34567">]
Each container has their own metadata I can get by calling @container.metadata:
{:transfer_encoding=>"chunked", :last_modified=>"Thu, 13 Dec 2012 06:01:23 GMT", :etag=>"\"0x8CFA70E6A590AD9\"", :server=>"Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0", :x_ms_request_id=>"a2cbc9ba-9e59-4c27-8a50-c54b405cbb1b", :x_ms_version=>"2011-08-18", :x_ms_meta_customername=>"Sample Name", :x_ms_meta_useremail=>"user@example.com", :date=>"Thu, 13 Dec 2012 16:29:16 GMT"}
In the view I want to list each container and link to it using the :x_ms_meta_customername property. I’ve gotten to that point in the view with:
<% @containerlist.each do |container| %>
<li><%= link_to container.metadata[:x_ms_meta_customername], root_url(:container => container.name) %></li>
<% end %>
The links in the view get created in order of the container.name value which from the list above is “12345”, “23456” and “34567”. Instead I’d like the list to be ordered by :x_ms_meta_customername.
I’ve tried a few different things to get the list to sort but end up either not sorting it or getting errors about converting a string to an integer. I may me going about this all wrong.
Another thing I’d like to do is filter the list based on the :x_ms_meta_useremail property. Almost all users will have the entire list shown at all times but there will be some users that only have access to containers that have their email in the metadata.
I’ve done some searching and can’t seem to find anything that helps. I’ve sort of hit a dead end.
Another thing I’d like to know is, should I be doing all of this work in the View? Or move this logic to the Model or Controller?
Any help would be greatly appreciated!
You can use
sortto sort the records.Or even easier:
To filter you can use
reject!.As a general rule of thumb, you should try to keep logic out of your views. You also want to keep your controllers as skinny as possible. If I were doing that, I would create a service class that I could call from my controller.