From e0ab6cdbed54a6c4693125d0f9da1f7cd16c6a83 Mon Sep 17 00:00:00 2001 From: sseerrggii Date: Fri, 4 Nov 2016 14:18:35 +0100 Subject: [PATCH 1/2] Post tags list ordered case insensitive --- app/models/concerns/taggable.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb index fb7eefb50..a6c92f374 100644 --- a/app/models/concerns/taggable.rb +++ b/app/models/concerns/taggable.rb @@ -28,7 +28,12 @@ def tag_list end 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 { |k, v| [k, v.size] } + .sort_by { |array| array.first.downcase } + ] end def find_like_tag(pattern) From ed7f8c9327b0dd9e37c7d7c33e87efce893fbc6b Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 25 Jan 2018 17:47:24 +0100 Subject: [PATCH 2/2] Add spec for case insensitive tag group --- app/models/concerns/taggable.rb | 15 ++++++++++--- spec/models/taggable_spec.rb | 40 ++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb index a6c92f374..31f81e709 100644 --- a/app/models/concerns/taggable.rb +++ b/app/models/concerns/taggable.rb @@ -27,12 +27,16 @@ 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 Integer>] def tag_cloud Hash[ all_tags - .group_by(&:to_s) - .map { |k, v| [k, v.size] } - .sort_by { |array| array.first.downcase } + .group_by(&:to_s) + .map { |tag_name, values| [tag_name, values.size] } + .sort_by { |array| array.first.downcase } ] end @@ -40,6 +44,11 @@ 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>>] def alphabetical_grouped_tags tag_cloud.group_by { |tag_name, _| tag_name[0].capitalize } end diff --git a/spec/models/taggable_spec.rb b/spec/models/taggable_spec.rb index b8ae50a08..771b7a04e 100644 --- a/spec/models/taggable_spec.rb +++ b/spec/models/taggable_spec.rb @@ -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 @@ -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