Thanks Martyn,
This is an ACK.
On 04/05/2011 12:48 PM, mtaylor(a)redhat.com wrote:
From: Martyn Taylor<mtaylor(a)redhat.com>
---
.../admin/hardware_profiles_controller.rb | 17 ++++++++++++++---
src/app/models/hardware_profile.rb | 4 ++++
src/app/models/hardware_profile_property.rb | 14 ++++++++++++++
3 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/src/app/controllers/admin/hardware_profiles_controller.rb
b/src/app/controllers/admin/hardware_profiles_controller.rb
index 783c0e8..e82580b 100644
--- a/src/app/controllers/admin/hardware_profiles_controller.rb
+++ b/src/app/controllers/admin/hardware_profiles_controller.rb
@@ -146,7 +146,7 @@ class Admin::HardwareProfilesController< ApplicationController
{ :name => "Architecture", :sort_attr => :architecture },
{ :name => "Memory", :sort_attr => :memory},
{ :name => "Storage", :sort_attr => :storage },
- { :name => "Virtual CPU", :sort_attr => :cpus}
+ { :name => "Virtual CPU", :sort_attr => :cpu}
]
begin
@@ -173,12 +173,23 @@ class Admin::HardwareProfilesController< ApplicationController
{ :name => "Architecture", :sort_attr => :architecture },
{ :name => "Memory", :sort_attr => :memory},
{ :name => "Storage", :sort_attr => :storage },
- { :name => "Virtual CPU", :sort_attr => :cpus}
+ { :name => "Virtual CPU", :sort_attr => :cpu}
]
end
def load_hardware_profiles
- @hardware_profiles = HardwareProfile.all(:conditions => 'provider_id IS
NULL')
+ sort_field = params[:order_field].nil? ? "name" : params[:order_field]
+ sort_order = params[:order_dir] == "asc" || params[:order_dir].nil? ?
"" : "desc"
+ if sort_field == "name"
+ @hardware_profiles = HardwareProfile.all(:order => sort_field + " "
+ sort_order, :conditions => 'provider_id IS NULL')
+ else
+ @hardware_profiles = HardwareProfile.all(:conditions => 'provider_id IS
NULL')
+ if sort_order == ""
+ @hardware_profiles.sort! {|x,y|
x.get_property_map[sort_field].sort_value(true)<=>
y.get_property_map[sort_field].sort_value(true)}
+ else
+ @hardware_profiles.sort! {|x,y|
y.get_property_map[sort_field].sort_value(false)<=>
x.get_property_map[sort_field].sort_value(false)}
+ end
+ end
end
def load_hardware_profile
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb
index 9cfc770..836717e 100644
--- a/src/app/models/hardware_profile.rb
+++ b/src/app/models/hardware_profile.rb
@@ -83,6 +83,10 @@ class HardwareProfile< ActiveRecord::Base
validates_associated :cpu
validates_associated :architecture
+ def get_property_map
+ return {'memory' => memory, 'cpu' => cpu,
'architecture' => architecture, 'storage' => storage}
+ end
+
def provider_hardware_profile?
!provider.nil?
end
diff --git a/src/app/models/hardware_profile_property.rb
b/src/app/models/hardware_profile_property.rb
index f38d394..65c70b4 100644
--- a/src/app/models/hardware_profile_property.rb
+++ b/src/app/models/hardware_profile_property.rb
@@ -127,5 +127,19 @@ class HardwareProfileProperty< ActiveRecord::Base
"undefined"
end
end
+
+ def sort_value(ascending)
+ case kind
+ when "fixed"
+ sort_value = value
+ when "range"
+ sort_value = ascending ? range_from : range_to
+ when "enum"
+ entries = (property_enum_entries.map { |enum| enum.value }).sort!
+ sort_value = ascending ? entries.first : entries.last
+ end
+ return name == "architecture" ? sort_value : sort_value.to_f
+ end
+
end