diff --git a/app/helpers/kalibro_modules_helper.rb b/app/helpers/kalibro_modules_helper.rb
new file mode 100644
index 0000000..45fe71c
--- /dev/null
+++ b/app/helpers/kalibro_modules_helper.rb
@@ -0,0 +1,7 @@
+module KalibroModulesHelper
+ def sort_by_granularity_and_name(module_results)
+ module_results.sort do |a,b|
+ (a.kalibro_module.granularity == b.kalibro_module.granularity) ? a.kalibro_module.name <=> b.kalibro_module.name : -a.kalibro_module.granularity.length <=> -b.kalibro_module.granularity.length
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/views/modules/_module_tree.html.erb b/app/views/modules/_module_tree.html.erb
index d8e5570..75450dc 100644
--- a/app/views/modules/_module_tree.html.erb
+++ b/app/views/modules/_module_tree.html.erb
@@ -15,9 +15,7 @@
<% end %>
<% cache("#{@root_module_result.id}_tree") do %>
- <% children = @root_module_result.children.sort do %>
- <% |a,b| (a.module.granularity == b.module.granularity) ? a.module.name <=> b.module.name : -a.module.granularity.length <=> -b.module.granularity.length %>
- <% end %>
+ <% children = sort_by_granularity_and_name(@root_module_result.children) %>
<% unless children.empty? %>
diff --git a/spec/factories/kalibro_modules.rb b/spec/factories/kalibro_modules.rb
index 266a94e..cb0040c 100644
--- a/spec/factories/kalibro_modules.rb
+++ b/spec/factories/kalibro_modules.rb
@@ -1,6 +1,17 @@
FactoryGirl.define do
factory :kalibro_module, class: KalibroClient::Entities::Processor::KalibroModule do
name 'Qt-Calculator'
- granlrty 'APPLICATION'
+ granlrty 'SOFTWARE'
+
+ trait :package do
+ granlrty 'PACKAGE'
+ end
+
+ trait :class do
+ granlrty 'CLASS'
+ end
+
+ factory :kalibro_module_package, traits: [:package]
+ factory :kalibro_module_class, traits: [:class]
end
end
diff --git a/spec/helpers/kalibro_module_helper_spec.rb b/spec/helpers/kalibro_module_helper_spec.rb
new file mode 100644
index 0000000..44a25be
--- /dev/null
+++ b/spec/helpers/kalibro_module_helper_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+describe KalibroModulesHelper, :type => :helper do
+ describe 'sort_by_granularity_and_name' do
+ let(:package) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_package)) }
+ let(:class_a) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'a')) }
+ let(:class_b) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'b')) }
+
+ it 'is expected to order the ModuleResults by the Granularity first and then the KalibroModule name' do
+ unordered_modules = [class_b, package, class_a]
+ expect(helper.sort_by_granularity_and_name(unordered_modules)).to eq([package, class_a, class_b])
+ end
+ end
+end
\ No newline at end of file
--
libgit2 0.21.2