https://svn.lrde.epita.fr/svn/ranch/trunk
Index: ChangeLog
from Nicolas Despr�s <nicolas.despres(a)gmail.com>
System list sorted by column.
* web/ranch/test/functional/systems_controller_test.rb: Test it.
* web/ranch/app/controllers/systems_controller.rb: Allow to choose
the order column and the order sens.
* web/ranch/app/views/systems/list.rhtml: Revers the order sens.
app/controllers/systems_controller.rb | 20 ++++++++++++-
app/views/systems/list.rhtml | 12 +++++++
test/functional/systems_controller_test.rb | 44 ++++++++++++++++++++++++++++-
3 files changed, 73 insertions(+), 3 deletions(-)
Index: web/ranch/test/functional/systems_controller_test.rb
--- web/ranch/test/functional/systems_controller_test.rb (revision 57)
+++ web/ranch/test/functional/systems_controller_test.rb (working copy)
@@ -19,12 +19,54 @@
assert_redirected_to(:action => 'list')
end
- def test_list
+ def test_list_default
get :list
assert_response :success
assert_template 'list'
assert_not_nil assigns(:systems)
+ assert "hostname", assigns(:order_by)
+ assert assigns(:ascendant)
+ end
+
+ def test_list_bad_order_by
+ get :list, { :order_by => 'bad_column_name' }
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:systems)
+ assert "hostname", assigns(:order_by)
+ assert assigns(:ascendant)
+ end
+
+ def test_list_good_order_by
+ get :list, { :order_by => 'mem_size' }
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:systems)
+ assert "mem_size", assigns(:order_by)
+ assert assigns(:ascendant)
+ end
+
+ def test_list_good_order_by_good_ascendant
+ get :list, { :order_by => 'mem_size', :ascendant => "false"
}
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:systems)
+ assert "mem_size", assigns(:order_by)
+ assert !assigns(:ascendant)
+ end
+
+ def test_list_good_order_by_bad_ascendant
+ get :list, { :order_by => 'mem_size', :ascendant => "bad ascendant
value" }
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:systems)
+ assert "mem_size", assigns(:order_by)
+ assert assigns(:ascendant)
end
def test_show_with_id
Index: web/ranch/app/controllers/systems_controller.rb
--- web/ranch/app/controllers/systems_controller.rb (revision 57)
+++ web/ranch/app/controllers/systems_controller.rb (working copy)
@@ -7,11 +7,29 @@
end
def list
- @systems = System.find :all, :order => "hostname"
+ @order_by = params[:order_by]
+ if @order_by.nil? or not field_name_exists?(@order_by)
+ @order_by = "hostname"
+ end
+
+ @ascendant = params[:ascendant] != "false"
+
+ @systems = System.find :all, {
+ :order => "#@order_by #{@ascendant ? "" :
"DESC"}"
+ }
end
def show
@system = System.find_by_id params[:system_id]
end
+ protected
+
+ def field_name_exists?(name)
+ System.columns.each do |column|
+ return true if column.name == name
+ end
+ false
+ end
+
end
Index: web/ranch/app/views/systems/list.rhtml
--- web/ranch/app/views/systems/list.rhtml (revision 57)
+++ web/ranch/app/views/systems/list.rhtml (working copy)
@@ -2,6 +2,9 @@
<p>
<table>
+
+<!-- HEADER -->
+
<%
rejected_col = [ "id", "info" ]
columns = System.columns.reject do |column|
@@ -10,9 +13,16 @@
%>
<tr>
<% for column in columns do %>
- <%= content_tag "td", column.name %>
+ <td>
+ <%= link_to column.name, :action => "list",
+ :order_by => column.name,
+ :ascendant => (@ascendant ^ true).to_s %>
+ </td>
<% end %>
</tr>
+
+<!-- RECORDS -->
+
<% for system in @systems do %>
<tr>
<% for column in columns do %>