Skip to content

Commit

Permalink
SD-1446 use pg_search for full text search of porduct names when pg_s…
Browse files Browse the repository at this point in the history
…earch is available. (#11416)

* SD-1446 use pg_search for full text search of porduct names when pg_search is available

* SD-1446 small adjustment

* SD-1446 CR adjustments
  • Loading branch information
KacperMekarski committed Oct 25, 2021
1 parent fd44b11 commit 1ef4675
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 20 additions & 0 deletions core/app/models/concerns/spree/product_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ def self.for_user(user = nil)
group('spree_products.id').joins(:taxons).where(Taxon.arel_table[:name].eq(name))
end

# .search_by_name
if defined?(PgSearch)
include PgSearch::Model

if defined?(SpreeGlobalize)
pg_search_scope :search_by_name, associated_against: { translations: :name }, using: :tsearch
else
pg_search_scope :search_by_name, against: :name, using: :tsearch
end
else
def self.search_by_name(query)
if defined?(SpreeGlobalize)
joins(:translations).order(:name).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%").distinct
else
where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%")
end
end
end
search_scopes << :search_by_name

def self.price_table_name
Price.quoted_table_name
end
Expand Down
8 changes: 0 additions & 8 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ def self.like_any(fields, values)
where conditions.inject(:or)
end

def self.search_by_name(query)
if defined?(SpreeGlobalize)
joins(:translations).order(:name).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%").distinct
else
where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%")
end
end

# Suitable for displaying only variants that has at least one option value.
# There may be scenarios where an option type is removed and along with it
# all option values. At that point all variants associated with only those
Expand Down

0 comments on commit 1ef4675

Please sign in to comment.