Skip to content

Commit

Permalink
Merge pull request #718 from nflorentin/search-improvements
Browse files Browse the repository at this point in the history
Search improvements
  • Loading branch information
markets authored Dec 7, 2023
2 parents 12affd6 + 78d0fb0 commit 38164d6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/models/concerns/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def tag_cloud
end

def find_like_tag(pattern)
all_tags.uniq.select { |t| t =~ /#{pattern}/i }
transliterated_pattern = pattern.present? ? ActiveSupport::Inflector.transliterate(pattern) : ""
all_tags.uniq.select { |t| ActiveSupport::Inflector.transliterate(t) =~ /#{transliterated_pattern}/i }
end

# Builds a hash where the keys are the capital letters of the tags and the
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="col-md-12">
<%= search_form_for(@search, class: "navbar-form navbar-left", url: users_path) do |f| %>
<div class="form-group">
<%= f.search_field :member_search_cont, value: params.dig(:q, :member_search_cont), class: "form-control" %>
<%= f.search_field :member_search_unaccent_cont, value: params.dig(:q, :member_search_unaccent_cont), class: "form-control" %>
</div>
<button class="btn btn-default" type="submit">
<%= t 'global.search' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/manage.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="col-md-12">
<%= search_form_for(@search, class: "navbar-form navbar-left", url: manage_users_path) do |f| %>
<div class="form-group">
<%= f.search_field :member_search_cont, value: params.dig(:q, :member_search_cont), class: "form-control" %>
<%= f.search_field :member_search_unaccent_cont, value: params.dig(:q, :member_search_unaccent_cont), class: "form-control" %>
</div>
<button class="btn btn-default" type="submit">
<%= t 'global.search' %>
Expand Down
8 changes: 8 additions & 0 deletions config/initializers/ransack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Ransack.configure do |config|
config.add_predicate 'unaccent_cont',
arel_predicate: 'matches',
formatter: proc { |s| ActiveSupport::Inflector.transliterate("%#{s}%") },
validator: proc { |s| s.present? },
compounds: true,
type: :string
end
12 changes: 7 additions & 5 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
user = Fabricate(:user, username: 'foo', email: '[email protected]')
member = Fabricate(:member, user: user, organization: test_organization, member_uid: 1000)

get :index, params: { q: { member_search_cont: 1000 } }
get :index, params: { q: { member_search_unaccent_cont: 1000 } }

expect(assigns(:members)).to include(member)
end
Expand All @@ -133,7 +133,7 @@
user = Fabricate(:user, username: 'foo', email: '[email protected]')
member = Fabricate(:member, user: user, organization: test_organization, member_uid: 1000, tags: ["Boss"])

get :index, params: { q: { member_search_cont: "Bos" } }
get :index, params: { q: { member_search_unaccent_cont: "Bos" } }

expect(assigns(:members)).to include(member)
end
Expand All @@ -151,8 +151,10 @@
user = Fabricate(:user, username: 'fôô', email: '[email protected]')
member = Fabricate(:member, user: user, organization: test_organization)

get :index, params: { q: { member_search_cont: "foo" } }
get :index, params: { q: { member_search_unaccent_cont: "foo" } }
expect(assigns(:members)).to include(member)

get :index, params: { q: { member_search_unaccent_cont: "föö" } }
expect(assigns(:members)).to include(member)
end
end
Expand Down Expand Up @@ -200,7 +202,7 @@
user = Fabricate(:user, phone: 123456789)
member = Fabricate(:member, user: user, organization: test_organization)

get :manage, params: { q: { member_search_cont: 123456789 } }
get :manage, params: { q: { member_search_unaccent_cont: 123456789 } }

expect(assigns(:members)).to include(member)
end
Expand All @@ -209,7 +211,7 @@
user = Fabricate(:user)
member = Fabricate(:member, user: user, organization: test_organization, member_uid: 1000, tags: ["Boss"])

get :index, params: { q: { member_search_cont: "Bos" } }
get :index, params: { q: { member_search_unaccent_cont: "Bos" } }

expect(assigns(:members)).to include(member)
end
Expand Down
6 changes: 4 additions & 2 deletions spec/models/taggable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe Taggable do
let(:organization) { Fabricate(:organization) }
let(:tags) { %w(foo bar baz) }
let(:more_tags) { %w(foo baz qux) }
let(:tags) { %w(foo bar baz test) }
let(:more_tags) { %w(foo baz qux têst) }
let!(:offer) do
Fabricate(
:offer,
Expand Down Expand Up @@ -30,6 +30,8 @@
expect(Offer.find_like_tag("foo")).to eq ["foo"]
expect(Offer.find_like_tag("Foo")).to eq ["foo"]
expect(Offer.find_like_tag("none")).to eq []
expect(Offer.find_like_tag("test")).to match_array ["test", "têst"]
expect(Offer.find_like_tag("têst")).to match_array ["test", "têst"]
end

describe '.alphabetical_grouped_tags' do
Expand Down

0 comments on commit 38164d6

Please sign in to comment.