Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve user fuzzy search

Damian Legawieca0291e5a6519
diff --git a/spree/core/app/models/concerns/spree/searchable.rb b/spree/core/app/models/concerns/spree/searchable.rb
index 3b886f4e22..253493772f 100644
--- a/spree/core/app/models/concerns/spree/searchable.rb
+++ b/spree/core/app/models/concerns/spree/searchable.rb
@@ -13,7 +13,11 @@ module Spree
if encrypted_attributes.include?(attribute.to_sym)
model_class.arel_table[attribute.to_sym].eq(query)
else
- model_class.arel_table[attribute.to_sym].lower.matches("%#{query}%")
+ # `sanitize_query_for_search` escapes LIKE wildcards (`_`, `%`) with a
+ # backslash. Pass `\` as the ESCAPE character so SQLite/MySQL honor the
+ # escaping instead of treating the backslash as a literal — otherwise a
+ # query like `[email protected]` never matches its own row.
+ model_class.arel_table[attribute.to_sym].lower.matches("%#{query}%", '\\')
end
end
diff --git a/spree/core/app/models/concerns/spree/user_methods.rb b/spree/core/app/models/concerns/spree/user_methods.rb
index 0fe93db6b2..818c41bbcb 100644
--- a/spree/core/app/models/concerns/spree/user_methods.rb
+++ b/spree/core/app/models/concerns/spree/user_methods.rb
@@ -105,22 +105,26 @@ module Spree
end
def self.search(query)
- sanitized_query = sanitize_query_for_search(query)
return none if query.blank?
- name_conditions = []
+ sanitized_query = sanitize_query_for_search(query)
- name_conditions << search_condition(self, :first_name, sanitized_query)
- name_conditions << search_condition(self, :last_name, sanitized_query)
+ conditions = []
+ # Email may be stored encrypted (equality lookups only), so we pass the
+ # raw query — escaped LIKE wildcards would break encrypted comparisons.
+ # Name columns are plain text and use sanitized input for LIKE safety.
+ conditions << search_condition(self, :email, query.strip)
+ conditions << search_condition(self, :first_name, sanitized_query)
+ conditions << search_condition(self, :last_name, sanitized_query)
full_name = NameOfPerson::PersonName.full(sanitized_query)
if full_name.first.present? && full_name.last.present?
- name_conditions << search_condition(self, :first_name, full_name.first)
- name_conditions << search_condition(self, :last_name, full_name.last)
+ conditions << search_condition(self, :first_name, full_name.first)
+ conditions << search_condition(self, :last_name, full_name.last)
end
- where(arel_table[:email].lower.eq(query.downcase)).or(where(name_conditions.reduce(:or)))
+ where(conditions.reduce(:or))
end
# Backward compatibility alias — remove in Spree 6.0
diff --git a/spree/core/spec/models/spree/concerns/user_methods_spec.rb b/spree/core/spec/models/spree/concerns/user_methods_spec.rb
index c7c338b0e8..3d5183d60a 100644
--- a/spree/core/spec/models/spree/concerns/user_methods_spec.rb
+++ b/spree/core/spec/models/spree/concerns/user_methods_spec.rb
@@ -122,12 +122,28 @@ describe Spree::UserMethods do
let!(:user_4) { create(:user, email: '[email protected]', first_name: 'Ayn', last_name: 'Rand') }
let!(:user_5) { create(:user, email: '[email protected]', first_name: 'John', last_name: 'Doe') }
- it 'returns users based on an email' do
+ it 'returns users based on a full email' do
expect(Spree.user_class.search('[email protected]')).to eq([user_1])
expect(Spree.user_class.search('[email protected]')).to eq([user_2])
expect(Spree.user_class.search('[email protected]')).to eq([user_4])
expect(Spree.user_class.search('[email protected]')).to eq([user_5])
- expect(Spree.user_class.search('mary.moe@')).to eq([])
+ end
+
+ it 'matches a partial email substring (admin pickers feed it a few chars at a time)' do
+ expect(Spree.user_class.search('mary.moe@')).to eq([user_3])
+ expect(Spree.user_class.search('john.doe')).to eq([user_1])
+ end
+
+ it 'is case-insensitive across email and name' do
+ mixed = create(:user, email: '[email protected]', first_name: 'Greg', last_name: 'Smith')
+
+ expect(Spree.user_class.search('[email protected]')).to eq([mixed])
+ expect(Spree.user_class.search('[email protected]')).to eq([mixed])
+ expect(Spree.user_class.search('gReG.SmItH')).to eq([mixed])
+ expect(Spree.user_class.search('GREG')).to eq([mixed])
+ expect(Spree.user_class.search('SMITH')).to eq([mixed])
+ expect(Spree.user_class.search('greg smith')).to eq([mixed])
+ expect(Spree.user_class.search('GREG SMITH')).to eq([mixed])
end
it 'returns users based on the first name' do