Commit 1cf91eb79014a5715065dc778daf635c66ab19b5

Authored by Rafael Manzo
Committed by Heitor
1 parent 8b0f4c99

Extracted ModuleResults ordering on ModuleTree into a helper method.

Signed off by: Diego Araújo <diegoamc90@gmail.com>
app/helpers/kalibro_modules_helper.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +module KalibroModulesHelper
  2 + def sort_by_granularity_and_name(module_results)
  3 + module_results.sort do |a,b|
  4 + (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
  5 + end
  6 + end
  7 +end
0 8 \ No newline at end of file
... ...
app/views/modules/_module_tree.html.erb
... ... @@ -15,9 +15,7 @@
15 15 <% end %>
16 16  
17 17 <% cache("#{@root_module_result.id}_tree") do %>
18   - <% children = @root_module_result.children.sort do %>
19   - <% |a,b| (a.module.granularity == b.module.granularity) ? a.module.name <=> b.module.name : -a.module.granularity.length <=> -b.module.granularity.length %>
20   - <% end %>
  18 + <% children = sort_by_granularity_and_name(@root_module_result.children) %>
21 19 <% unless children.empty? %>
22 20 <table class="table table-hover">
23 21 <thead>
... ...
spec/factories/kalibro_modules.rb
1 1 FactoryGirl.define do
2 2 factory :kalibro_module, class: KalibroClient::Entities::Processor::KalibroModule do
3 3 name 'Qt-Calculator'
4   - granlrty 'APPLICATION'
  4 + granlrty 'SOFTWARE'
  5 +
  6 + trait :package do
  7 + granlrty 'PACKAGE'
  8 + end
  9 +
  10 + trait :class do
  11 + granlrty 'CLASS'
  12 + end
  13 +
  14 + factory :kalibro_module_package, traits: [:package]
  15 + factory :kalibro_module_class, traits: [:class]
5 16 end
6 17 end
... ...
spec/helpers/kalibro_module_helper_spec.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +require 'rails_helper'
  2 +
  3 +describe KalibroModulesHelper, :type => :helper do
  4 + describe 'sort_by_granularity_and_name' do
  5 + let(:package) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_package)) }
  6 + let(:class_a) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'a')) }
  7 + let(:class_b) { FactoryGirl.build(:module_result, kalibro_module: FactoryGirl.build(:kalibro_module_class, name: 'b')) }
  8 +
  9 + it 'is expected to order the ModuleResults by the Granularity first and then the KalibroModule name' do
  10 + unordered_modules = [class_b, package, class_a]
  11 + expect(helper.sort_by_granularity_and_name(unordered_modules)).to eq([package, class_a, class_b])
  12 + end
  13 + end
  14 +end
0 15 \ No newline at end of file
... ...