From 23017acfbad519ff88a68e7c1416373ff890e79e Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Thu, 7 Mar 2019 23:29:47 +0100 Subject: [PATCH] Enable partial word searches in pg_search Doc: https://github.com/Casecommons/pg_search#prefix-postgresql-84-and-newer-only --- app/models/post.rb | 7 +++++-- spec/controllers/offers_controller_spec.rb | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 0dddb7727..7bee67030 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,8 +3,11 @@ class Post < ActiveRecord::Base include PgSearch pg_search_scope :search_by_query, - :against => [:title, :description, :tags], - :ignoring => :accents + against: [:title, :description, :tags], + ignoring: :accents, + using: { + tsearch: { prefix: true } + } attr_reader :member_id diff --git a/spec/controllers/offers_controller_spec.rb b/spec/controllers/offers_controller_spec.rb index 3757f1fa7..cca4c262c 100644 --- a/spec/controllers/offers_controller_spec.rb +++ b/spec/controllers/offers_controller_spec.rb @@ -36,15 +36,21 @@ end describe "GET #index (search)" do - it "populates an array of offers" do - login(another_member.user) + before { login(another_member.user) } + it "populates an array of offers" do get "index", q: offer.title.split(/\s/).first expect(assigns(:offers).size).to eq 1 expect(assigns(:offers)[0]).to eq offer expect(assigns(:offers).to_a).to eq([offer]) end + + it "allows to search by partial word" do + get :index, q: offer.title.split(/\s/).first[0..-2] + + expect(assigns(:offers)).to include offer + end end describe 'GET #show' do