From f572d5b579f58dc0a2877db21eab1c3e9e0d14e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Sat, 12 Jan 2019 14:56:06 +0100 Subject: [PATCH] Add translations to proposal pg_search_scope Some Proposal attributes are now translatable so we need to include all existing translations on pg_search scope. --- app/models/concerns/globalizable.rb | 14 ++++++------- app/models/proposal.rb | 18 ++++++++++------- spec/models/proposal_spec.rb | 31 ++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index 6501041d0..a225c9236 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -23,15 +23,15 @@ module Globalizable private - def searchable_globalized_values - values = {} - translations.each do |translation| - Globalize.with_locale(translation.locale) do - values.merge! searchable_translations_definitions + def searchable_globalized_values + values = {} + translations.each do |translation| + Globalize.with_locale(translation.locale) do + values.merge! searchable_translations_definitions + end end + values end - values - end end class_methods do diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 4961b237d..497d8181e 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -125,14 +125,18 @@ class Proposal < ApplicationRecord "#{id}-#{title}".parameterize end + def searchable_translations_definitions + { title => "A", + summary => "C", + description => "D" } + end + def searchable_values - { title => "A", - author.username => "B", - tag_list.join(" ") => "B", - geozone.try(:name) => "B", - summary => "C", - description => "D" - } + { + author.username => "B", + tag_list.join(" ") => "B", + geozone.try(:name) => "B" + }.merge!(searchable_globalized_values) end def self.search(terms) diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 881063f26..9c1d0971c 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -486,24 +486,49 @@ describe Proposal do context "attributes" do + let(:attributes) { { title: "save the world", + summary: "basically", + description: "in order to save the world one must think about...", + title_es: "para salvar el mundo uno debe pensar en...", + summary_es: "basicamente", + description_es: "uno debe pensar" } } + it "searches by title" do - proposal = create(:proposal, title: "save the world") + proposal = create(:proposal, attributes) results = described_class.search("save the world") expect(results).to eq([proposal]) end + it "searches by title across all languages translations" do + proposal = create(:proposal, attributes) + results = described_class.search("salvar el mundo") + expect(results).to eq([proposal]) + end + it "searches by summary" do - proposal = create(:proposal, summary: "basically...") + proposal = create(:proposal, attributes) results = described_class.search("basically") expect(results).to eq([proposal]) end + it "searches by summary across all languages translations" do + proposal = create(:proposal, attributes) + results = described_class.search("basicamente") + expect(results).to eq([proposal]) + end + it "searches by description" do - proposal = create(:proposal, description: "in order to save the world one must think about...") + proposal = create(:proposal, attributes) results = described_class.search("one must think") expect(results).to eq([proposal]) end + it "searches by description across all languages translations" do + proposal = create(:proposal, attributes) + results = described_class.search("uno debe pensar") + expect(results).to eq([proposal]) + end + it "searches by author name" do author = create(:user, username: "Danny Trejo") proposal = create(:proposal, author: author)