Commit 54bc64ec00a402cb330f35df8e346d9448d4e2ca
Committed by
Daniela Feitosa
1 parent
f7a1b20d
Exists in
staging
and in
42 other branches
Sorting qualifiers and certifiers by name
(ActionItem1880)
Showing
6 changed files
with
67 additions
and
2 deletions
Show diff stats
app/helpers/manage_products_helper.rb
@@ -217,10 +217,10 @@ module ManageProductsHelper | @@ -217,10 +217,10 @@ module ManageProductsHelper | ||
217 | end | 217 | end |
218 | 218 | ||
219 | def qualifiers_for_select | 219 | def qualifiers_for_select |
220 | - [[_('Select...'), nil]] + environment.qualifiers.map{ |c| [c.name, c.id] } | 220 | + [[_('Select...'), nil]] + environment.qualifiers.sort.map{ |c| [c.name, c.id] } |
221 | end | 221 | end |
222 | def certifiers_for_select(qualifier) | 222 | def certifiers_for_select(qualifier) |
223 | - [[_('Self declared'), nil]] + qualifier.certifiers.map{ |c| [c.name, c.id] } | 223 | + [[_('Self declared'), nil]] + qualifier.certifiers.sort.map{ |c| [c.name, c.id] } |
224 | end | 224 | end |
225 | def select_qualifiers(product, selected = nil) | 225 | def select_qualifiers(product, selected = nil) |
226 | select_tag('selected_qualifier', options_for_select(qualifiers_for_select, selected), | 226 | select_tag('selected_qualifier', options_for_select(qualifiers_for_select, selected), |
app/models/certifier.rb
@@ -11,4 +11,9 @@ class Certifier < ActiveRecord::Base | @@ -11,4 +11,9 @@ class Certifier < ActiveRecord::Base | ||
11 | def link | 11 | def link |
12 | self[:link] || '' | 12 | self[:link] || '' |
13 | end | 13 | end |
14 | + | ||
15 | + def <=>(b) | ||
16 | + self.name.downcase.transliterate <=> b.name.downcase.transliterate | ||
17 | + end | ||
18 | + | ||
14 | end | 19 | end |
app/models/qualifier.rb
@@ -8,4 +8,8 @@ class Qualifier < ActiveRecord::Base | @@ -8,4 +8,8 @@ class Qualifier < ActiveRecord::Base | ||
8 | validates_presence_of :environment_id | 8 | validates_presence_of :environment_id |
9 | validates_presence_of :name | 9 | validates_presence_of :name |
10 | 10 | ||
11 | + def <=>(b) | ||
12 | + self.name.downcase.transliterate <=> b.name.downcase.transliterate | ||
13 | + end | ||
14 | + | ||
11 | end | 15 | end |
test/unit/certifier_test.rb
@@ -39,4 +39,23 @@ class CertifierTest < Test::Unit::TestCase | @@ -39,4 +39,23 @@ class CertifierTest < Test::Unit::TestCase | ||
39 | assert certifier.valid? | 39 | assert certifier.valid? |
40 | end | 40 | end |
41 | 41 | ||
42 | + should 'sort by name' do | ||
43 | + last = fast_create(Certifier, :name => "Zumm") | ||
44 | + first = fast_create(Certifier, :name => "Atum") | ||
45 | + assert_equal [first, last], Certifier.all.sort | ||
46 | + end | ||
47 | + | ||
48 | + should 'sorting is not case sensitive' do | ||
49 | + first = fast_create(Certifier, :name => "Aaaa") | ||
50 | + second = fast_create(Certifier, :name => "abbb") | ||
51 | + last = fast_create(Certifier, :name => "Accc") | ||
52 | + assert_equal [first, second, last], Certifier.all.sort | ||
53 | + end | ||
54 | + | ||
55 | + should 'discard non-ascii char when sorting' do | ||
56 | + first = fast_create(Certifier, :name => "Áaaa") | ||
57 | + last = fast_create(Certifier, :name => "Aáab") | ||
58 | + assert_equal [first, last], Certifier.all.sort | ||
59 | + end | ||
60 | + | ||
42 | end | 61 | end |
test/unit/manage_products_helper_test.rb
@@ -149,6 +149,24 @@ class ManageProductsHelperTest < Test::Unit::TestCase | @@ -149,6 +149,24 @@ class ManageProductsHelperTest < Test::Unit::TestCase | ||
149 | assert_equal 'Amount used in this product or service', label_amount_used(input) | 149 | assert_equal 'Amount used in this product or service', label_amount_used(input) |
150 | end | 150 | end |
151 | 151 | ||
152 | + should 'sort qualifiers by name' do | ||
153 | + fast_create(Qualifier, :name => 'Organic') | ||
154 | + fast_create(Qualifier, :name => 'Non Organic') | ||
155 | + result = qualifiers_for_select | ||
156 | + assert_equal ["Select...", "Non Organic", "Organic"], result.map{|i| i[0]} | ||
157 | + end | ||
158 | + | ||
159 | + should 'sort certifiers by name' do | ||
160 | + qualifier = fast_create(Qualifier, :name => 'Organic') | ||
161 | + fbes = fast_create(Certifier, :name => 'FBES') | ||
162 | + colivre = fast_create(Certifier, :name => 'Colivre') | ||
163 | + QualifierCertifier.create!(:qualifier => qualifier, :certifier => colivre) | ||
164 | + QualifierCertifier.create!(:qualifier => qualifier, :certifier => fbes) | ||
165 | + | ||
166 | + result = certifiers_for_select(qualifier) | ||
167 | + assert_equal ["Self declared", "Colivre", "FBES"], result.map{|i| i[0]} | ||
168 | + end | ||
169 | + | ||
152 | protected | 170 | protected |
153 | include NoosferoTestHelper | 171 | include NoosferoTestHelper |
154 | include ActionView::Helpers::TextHelper | 172 | include ActionView::Helpers::TextHelper |
test/unit/qualifier_test.rb
@@ -30,4 +30,23 @@ class QualifierTest < Test::Unit::TestCase | @@ -30,4 +30,23 @@ class QualifierTest < Test::Unit::TestCase | ||
30 | assert qualifier.valid? | 30 | assert qualifier.valid? |
31 | end | 31 | end |
32 | 32 | ||
33 | + should 'sort by name' do | ||
34 | + last = fast_create(Qualifier, :name => "Zumm") | ||
35 | + first = fast_create(Qualifier, :name => "Atum") | ||
36 | + assert_equal [first, last], Qualifier.all.sort | ||
37 | + end | ||
38 | + | ||
39 | + should 'sorting is not case sensitive' do | ||
40 | + first = fast_create(Qualifier, :name => "Aaaa") | ||
41 | + second = fast_create(Qualifier, :name => "abbb") | ||
42 | + last = fast_create(Qualifier, :name => "Accc") | ||
43 | + assert_equal [first, second, last], Qualifier.all.sort | ||
44 | + end | ||
45 | + | ||
46 | + should 'discard non-ascii char when sorting' do | ||
47 | + first = fast_create(Qualifier, :name => "Áaaa") | ||
48 | + last = fast_create(Qualifier, :name => "Aáab") | ||
49 | + assert_equal [first, last], Qualifier.all.sort | ||
50 | + end | ||
51 | + | ||
33 | end | 52 | end |