Skip to content

Fix/tags order #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion app/models/concerns/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ def tag_list
all_tags.uniq.sort
end

# Builds a hash where the keys are the tags and the values are the number of
# their occurrences
#
# @return [Hash<String => Integer>]
def tag_cloud
Hash[all_tags.group_by(&:to_s).map { |k, v| [k, v.size] }.sort]
Hash[
all_tags
.group_by(&:to_s)
.map { |tag_name, values| [tag_name, values.size] }
.sort_by { |array| array.first.downcase }
]
end

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

# Builds a hash where the keys are the capital letters of the tags and the
# values are the individual tags together with the number of their
# occurrences
#
# @return [Hash<Array<Array<String, Integer>>>]
def alphabetical_grouped_tags
tag_cloud.group_by { |tag_name, _| tag_name[0].capitalize }
end
Expand Down
40 changes: 27 additions & 13 deletions spec/models/taggable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
require 'spec_helper'

describe Taggable do
let (:tags) { %w(foo bar baz) }
let (:more_tags) { %w(foo baz qux) }
let (:organization) { Fabricate(:organization) }
let! (:offer) { Fabricate(:offer,
organization: organization,
tags: tags) }
let! (:another_offer) { Fabricate(:offer,
organization: organization,
tags: more_tags) }
let(:organization) { Fabricate(:organization) }

let!(:offer) do
Fabricate(
:offer,
organization: organization,
tags: tags
)
end
let!(:another_offer) do
Fabricate(
:offer,
organization: organization,
tags: more_tags
)
end

context "class methods and scopes" do
let(:tags) { %w(foo bar baz) }
let(:more_tags) { %w(foo baz qux) }

it "tagged_with" do
expect(Offer.tagged_with("bar")).to eq [offer]
end
Expand All @@ -26,12 +35,17 @@
expect(Offer.find_like_tag("Foo")).to eq ["foo"]
expect(Offer.find_like_tag("none")).to eq []
end
end

describe '.alphabetical_grouped_tags' do
let(:tags) { %w(foo bar baz Boo) }
let(:more_tags) { %w(foo baz qux) }

it "alphabetical_grouped_tags" do
it 'sorts them by alphabetical order case insensitive' do
expect(Offer.alphabetical_grouped_tags).to eq({
"B" => [["bar", 1], ["baz", 2]],
"F" => [["foo", 2]],
"Q" => [["qux", 1]]
'B' => [['bar', 1], ['baz', 2], ['Boo', 1]],
'F' => [['foo', 2]],
'Q' => [['qux', 1]]
})
end
end
Expand Down