Fix search_by_title_or_id method

Results were not including records without translations for current
locale (I18n.locale). Now we search for given title against all
translation fallbacks for current locale.
This commit is contained in:
Senén Rodero Rodríguez
2019-02-18 16:51:22 +01:00
committed by voodoorai2000
parent 66f885f8e4
commit bb2ee6dd3c
3 changed files with 44 additions and 13 deletions

View File

@@ -119,7 +119,9 @@ class Budget
end end
def self.sort_by_title def self.sort_by_title
all.sort_by(&:title) includes(:translations).
with_locales(Globalize.fallbacks(I18n.locale)).
order("budget_investment_translations.title ASC")
end end
def self.filter_params(params) def self.filter_params(params)
@@ -189,11 +191,10 @@ class Budget
end end
def self.search_by_title_or_id(title_or_id, results) def self.search_by_title_or_id(title_or_id, results)
if title_or_id =~ /^[0-9]+$/ return results.where(id: title_or_id) if title_or_id =~ /^[0-9]+$/
results.where(id: title_or_id)
else results.with_translations(Globalize.fallbacks(I18n.locale)).
results.with_translations(I18n.locale).where("budget_investment_translations.title ILIKE ?", "%#{title_or_id}%") where("budget_investment_translations.title ILIKE ?", "%#{title_or_id}%")
end
end end
def searchable_values def searchable_values

View File

@@ -826,33 +826,43 @@ describe "Admin budget investments" do
end end
before do before do
create(:budget_investment, title: "Some investment", budget: budget) I18n.with_locale(:es) do
Globalize.with_locale(:es) do
create(:budget_investment, title: "Proyecto de inversión", budget: budget)
end
end
end end
scenario "Search investments by title" do scenario "Search investments by title" do
visit admin_budget_budget_investments_path(budget) visit admin_budget_budget_investments_path(budget)
expect(page).to have_content("Some investment") expect(page).to have_content("Proyecto de inversión")
expect(page).to have_content("Some other investment") expect(page).to have_content("Some other investment")
fill_in "title_or_id", with: "Some investment" fill_in "title_or_id", with: "Proyecto de inversión"
click_button "Filter" click_button "Filter"
expect(page).to have_content("Some investment") expect(page).to have_content("Proyecto de inversión")
expect(page).not_to have_content("Some other investment") expect(page).not_to have_content("Some other investment")
fill_in "title_or_id", with: "Some other investment"
click_button "Filter"
expect(page).not_to have_content("Proyecto de inversión")
expect(page).to have_content("Some other investment")
end end
scenario "Search investments by ID" do scenario "Search investments by ID" do
visit admin_budget_budget_investments_path(budget) visit admin_budget_budget_investments_path(budget)
expect(page).to have_content("Some investment") expect(page).to have_content("Proyecto de inversión")
expect(page).to have_content("Some other investment") expect(page).to have_content("Some other investment")
fill_in "title_or_id", with: first_investment.id fill_in "title_or_id", with: first_investment.id
click_button "Filter" click_button "Filter"
expect(page).to have_content("Some other investment") expect(page).to have_content("Some other investment")
expect(page).not_to have_content("Some investment") expect(page).not_to have_content("Proyecto de inversión")
end end
end end

View File

@@ -585,10 +585,30 @@ describe Budget::Investment do
spanish_investment = Globalize.with_locale(:es) do spanish_investment = Globalize.with_locale(:es) do
I18n.with_locale(:es) do I18n.with_locale(:es) do
create(:budget_investment, :selected, title: "Título en español") create(:budget_investment, :selected, title: "Título en español")
describe "search_by_title_or_id" do
before { create(:budget_investment) }
let!(:investment) do
I18n.with_locale(:es) do
Globalize.with_locale(:es) do
create(:budget_investment,
title_es: "Título del proyecto de inversión",
description_es: "Descripción del proyecto de inversión")
end end
end end
end
expect(described_class.sort_by_title).to eq [english_investment, spanish_investment] let(:all_investments) { described_class.all }
it "return investment by given id" do
expect(described_class.search_by_title_or_id(investment.id.to_s, all_investments)).
to eq([investment])
end
it "return investments by given title" do
expect(described_class.search_by_title_or_id("Título del proyecto de inversión", all_investments)).
to eq([investment])
end end
end end
end end