Skip to content

Commit 277f83b

Browse files
authored
Merge pull request #241 from coopdevs/fix/tags-order
Fix/tags order
2 parents c80c92d + ed7f8c9 commit 277f83b

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

app/models/concerns/taggable.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,28 @@ def tag_list
2727
all_tags.uniq.sort
2828
end
2929

30+
# Builds a hash where the keys are the tags and the values are the number of
31+
# their occurrences
32+
#
33+
# @return [Hash<String => Integer>]
3034
def tag_cloud
31-
Hash[all_tags.group_by(&:to_s).map { |k, v| [k, v.size] }.sort]
35+
Hash[
36+
all_tags
37+
.group_by(&:to_s)
38+
.map { |tag_name, values| [tag_name, values.size] }
39+
.sort_by { |array| array.first.downcase }
40+
]
3241
end
3342

3443
def find_like_tag(pattern)
3544
all_tags.uniq.select { |t| t =~ /#{pattern}/i }
3645
end
3746

47+
# Builds a hash where the keys are the capital letters of the tags and the
48+
# values are the individual tags together with the number of their
49+
# occurrences
50+
#
51+
# @return [Hash<Array<Array<String, Integer>>>]
3852
def alphabetical_grouped_tags
3953
tag_cloud.group_by { |tag_name, _| tag_name[0].capitalize }
4054
end

spec/models/taggable_spec.rb

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
require 'spec_helper'
22

33
describe Taggable do
4-
let (:tags) { %w(foo bar baz) }
5-
let (:more_tags) { %w(foo baz qux) }
6-
let (:organization) { Fabricate(:organization) }
7-
let! (:offer) { Fabricate(:offer,
8-
organization: organization,
9-
tags: tags) }
10-
let! (:another_offer) { Fabricate(:offer,
11-
organization: organization,
12-
tags: more_tags) }
4+
let(:organization) { Fabricate(:organization) }
135

6+
let!(:offer) do
7+
Fabricate(
8+
:offer,
9+
organization: organization,
10+
tags: tags
11+
)
12+
end
13+
let!(:another_offer) do
14+
Fabricate(
15+
:offer,
16+
organization: organization,
17+
tags: more_tags
18+
)
19+
end
1420

1521
context "class methods and scopes" do
22+
let(:tags) { %w(foo bar baz) }
23+
let(:more_tags) { %w(foo baz qux) }
24+
1625
it "tagged_with" do
1726
expect(Offer.tagged_with("bar")).to eq [offer]
1827
end
@@ -26,12 +35,17 @@
2635
expect(Offer.find_like_tag("Foo")).to eq ["foo"]
2736
expect(Offer.find_like_tag("none")).to eq []
2837
end
38+
end
39+
40+
describe '.alphabetical_grouped_tags' do
41+
let(:tags) { %w(foo bar baz Boo) }
42+
let(:more_tags) { %w(foo baz qux) }
2943

30-
it "alphabetical_grouped_tags" do
44+
it 'sorts them by alphabetical order case insensitive' do
3145
expect(Offer.alphabetical_grouped_tags).to eq({
32-
"B" => [["bar", 1], ["baz", 2]],
33-
"F" => [["foo", 2]],
34-
"Q" => [["qux", 1]]
46+
'B' => [['bar', 1], ['baz', 2], ['Boo', 1]],
47+
'F' => [['foo', 2]],
48+
'Q' => [['qux', 1]]
3549
})
3650
end
3751
end

0 commit comments

Comments
 (0)