Get search dictionary based on I18n.default_locale (merge pull request #3856)

Implementation tries to be open for further extensions, such as deciding on
search dictionary based on configuration option or by locale set for
given user.
This commit is contained in:
Paweł Świątkowski
2020-04-12 14:22:36 +02:00
committed by GitHub
parent 426c1c5fd2
commit d99875cde2
8 changed files with 94 additions and 13 deletions

View File

@@ -1410,7 +1410,7 @@ describe "Proposals" do
end
end
scenario "Order by relevance by default", :js do
scenario "Order by relevance by default", :spanish_search, :js do
create(:proposal, title: "Show you got", cached_votes_up: 10)
create(:proposal, title: "Show what you got", cached_votes_up: 1)
create(:proposal, title: "Show you got", cached_votes_up: 100)

View File

@@ -0,0 +1,30 @@
require "rails_helper"
describe SearchDictionarySelector do
context "from I18n default locale" do
before { allow(subject).to receive(:call).and_call_original }
around do |example|
original_i18n_default = I18n.default_locale
begin
example.run
ensure
I18n.default_locale = original_i18n_default
end
end
it "returns correct dictionary for simple locale" do
I18n.default_locale = :es
expect(subject.call).to eq("spanish")
end
it "returns correct dictionary for compound locale" do
I18n.default_locale = :"pt-BR"
expect(subject.call).to eq("portuguese")
end
it "returns simple for unsupported locale" do
expect(I18n).to receive(:default_locale).and_return(:pl) # avoiding I18n::InvalidLocale
expect(subject.call).to eq("simple")
end
end
end

View File

@@ -503,7 +503,7 @@ describe Debate do
end
context "stemming" do
it "searches word stems" do
it "searches word stems in Spanish", :spanish_search do
debate = create(:debate, title: "limpiar")
results = Debate.search("limpiará")

View File

@@ -558,7 +558,7 @@ describe Proposal do
end
context "case" do
it "searches case insensite" do
it "searches case insensitive" do
proposal = create(:proposal, title: "SHOUT")
results = Proposal.search("shout")

View File

@@ -128,6 +128,10 @@ RSpec.configure do |config|
allow(Time).to receive(:zone).and_return(application_zone)
end
config.before(:each, :spanish_search) do |example|
allow(SearchDictionarySelector).to receive(:call).and_return("spanish")
end
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options.
config.example_status_persistence_file_path = "spec/examples.txt"