diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 1a3516199..cf25dc47d 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -119,7 +119,9 @@ class Budget end def self.sort_by_title - all.sort_by(&:title) + includes(:translations). + with_locales(Globalize.fallbacks(I18n.locale)). + order("budget_investment_translations.title ASC") end def self.filter_params(params) @@ -189,11 +191,10 @@ class Budget end def self.search_by_title_or_id(title_or_id, results) - if title_or_id =~ /^[0-9]+$/ - results.where(id: title_or_id) - else - results.with_translations(I18n.locale).where("budget_investment_translations.title ILIKE ?", "%#{title_or_id}%") - end + return results.where(id: title_or_id) if title_or_id =~ /^[0-9]+$/ + + results.with_translations(Globalize.fallbacks(I18n.locale)). + where("budget_investment_translations.title ILIKE ?", "%#{title_or_id}%") end def searchable_values diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index 1c8b14073..829adbff1 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -826,33 +826,43 @@ describe "Admin budget investments" do end 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 scenario "Search investments by title" do 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") - fill_in "title_or_id", with: "Some investment" + fill_in "title_or_id", with: "Proyecto de inversión" 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") + + 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 scenario "Search investments by ID" do 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") fill_in "title_or_id", with: first_investment.id click_button "Filter" 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 diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index e021f8f04..579476b55 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -585,10 +585,30 @@ describe Budget::Investment do spanish_investment = Globalize.with_locale(:es) do I18n.with_locale(:es) do 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 - 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