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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix customers search (#12426)
Łukasz Adamczyk7d79ff963eb2
diff --git a/admin/app/views/spree/admin/users/_filters.html.erb b/admin/app/views/spree/admin/users/_filters.html.erbindex 835f0b04b1..7b02b0dd01 100644--- a/admin/app/views/spree/admin/users/_filters.html.erb+++ b/admin/app/views/spree/admin/users/_filters.html.erb@@ -1,6 +1,6 @@<%= search_form_for [:admin, @search], url: spree.admin_users_url, class: "filter-wrap border-bottom", data: {controller: "filters reveal", reveal_hidden_class: "d-none"} do |f| %><div class="d-flex flex-column flex-lg-row gap-2">- <%= render 'spree/admin/shared/filters_search_bar', param: :email_eq, placeholder: [Spree.t(:search), Spree.t(:customers).downcase].join(' ') %>+ <%= render 'spree/admin/shared/filters_search_bar', param: :multi_search, placeholder: Spree.t('admin.users.filters.search_placeholder') %><%= render 'spree/admin/shared/filters_button' %></div>diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.ymlindex b0339f6702..65d45987b8 100644--- a/admin/config/locales/en.yml+++ b/admin/config/locales/en.yml@@ -403,3 +403,6 @@ en:avg_order_value: Avg. Order Valuenum_orders: No. of Ordersmember_since: Member since+ users:+ filters:+ search_placeholder: Search customers by email or namediff --git a/admin/spec/controllers/spree/admin/users_controller_spec.rb b/admin/spec/controllers/spree/admin/users_controller_spec.rbindex 05e7bd5d8e..3cd1263d57 100644--- a/admin/spec/controllers/spree/admin/users_controller_spec.rb+++ b/admin/spec/controllers/spree/admin/users_controller_spec.rb@@ -7,7 +7,7 @@ RSpec.describe Spree::Admin::UsersController, type: :controller dolet(:store) { Spree::Store.default }describe 'GET #index' do- let!(:user_1) { create(:user) }+ let!(:user_1) { create(:user, first_name: 'John', last_name: 'Doe', email: '[email protected]') }let!(:user_2) { create(:user, tag_list: ['some tag']) }let!(:user_3) { create(:user, accepts_email_marketing: true) }@@ -41,6 +41,54 @@ RSpec.describe Spree::Admin::UsersController, type: :controller doexpect(assigns(:collection).to_a).to eq([user_2])end+ describe 'multi_search' do+ subject { get :index, params: { q: { multi_search: multi_search_param } } }++ context 'when searching by email' do+ let(:multi_search_param) { '[email protected]' }++ it 'returns users based on an email' do+ subject+ expect(assigns(:collection).to_a).to eq([user_1])+ end+ end++ context 'when searching by the first name' do+ let(:multi_search_param) { 'john' }++ it 'returns users based on the first name' do+ subject+ expect(assigns(:collection).to_a).to eq([user_1])+ end+ end++ context 'when searching by the last name' do+ let(:multi_search_param) { 'doe' }++ it 'returns users based on the last name' do+ subject+ expect(assigns(:collection).to_a).to eq([user_1])+ end+ end++ context 'when searching by the full name' do+ let(:multi_search_param) { 'joh do' }++ it 'returns users based on the full name' do+ subject+ expect(assigns(:collection).to_a).to eq([user_1])+ end+ end++ context 'when the user is not found' do+ let(:multisearch_param) { 'mary' }++ it 'returns an empty list' do+ expect(assigns(:collection).to_a).to eq([])+ end+ end+ end+context 'csv' dolet!(:country) { create(:country) }let!(:state) { create(:state, country: country) }diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rbindex afebb98973..2159e86b89 100644--- a/core/app/models/concerns/spree/user_methods.rb+++ b/core/app/models/concerns/spree/user_methods.rb@@ -43,8 +43,30 @@ module Spree#attr_accessor :confirm_email, :terms_of_service+ scope :multi_search, lambda { |query|+ sanitized_query = ActiveRecord::Base.sanitize_sql_like(query.to_s.downcase.strip)+ return none if query.blank?++ query_pattern = "%#{sanitized_query}%"++ name_conditions = []++ name_conditions << arel_table[:first_name].lower.matches(query_pattern)+ name_conditions << arel_table[:last_name].lower.matches(query_pattern)++ full_name = NameOfPerson::PersonName.full(sanitized_query)++ if full_name.first.present? && full_name.last.present?+ name_conditions << arel_table[:first_name].lower.matches("%#{full_name.first}%")+ name_conditions << arel_table[:last_name].lower.matches("%#{full_name.last}%")+ end++ where(email: sanitized_query).or(where(name_conditions.reduce(:or)))+ }+self.whitelisted_ransackable_associations = %w[bill_address ship_address addresses tags]self.whitelisted_ransackable_attributes = %w[id email first_name last_name accepts_email_marketing]+ self.whitelisted_ransackable_scopes = %w[multi_search]def self.with_email(query)where("#{table_name}.email LIKE ?", "%#{query}%")diff --git a/core/spec/models/spree/concerns/user_methods_spec.rb b/core/spec/models/spree/concerns/user_methods_spec.rbindex 87143ce003..e93b9f04c6 100644--- a/core/spec/models/spree/concerns/user_methods_spec.rb+++ b/core/spec/models/spree/concerns/user_methods_spec.rb@@ -171,4 +171,36 @@ describe Spree::UserMethods doexpect(test_user.last_name).to eq('User')endend++ describe '.multi_search' do+ let!(:user_1) { create(:user, email: '[email protected]', first_name: 'John', last_name: 'Doe') }+ let!(:user_2) { create(:user, email: '[email protected]', first_name: 'Jane', last_name: 'Gone') }+ let!(:user_3) { create(:user, email: '[email protected]', first_name: 'Mary', last_name: 'Moe') }++ it 'returns users based on an email' do+ expect(Spree.user_class.multi_search('[email protected]')).to eq([user_1])+ expect(Spree.user_class.multi_search('[email protected]')).to eq([user_2])+ expect(Spree.user_class.multi_search('mary.moe@')).to eq([])+ end++ it 'returns users based on the first name' do+ expect(Spree.user_class.multi_search('joh')).to eq([user_1])+ expect(Spree.user_class.multi_search('jan')).to eq([user_2])+ expect(Spree.user_class.multi_search('greg')).to eq([])+ end++ it 'returns users based on the last name' do+ expect(Spree.user_class.multi_search('do')).to eq([user_1])+ expect(Spree.user_class.multi_search('moe')).to eq([user_3])+ expect(Spree.user_class.multi_search('smith')).to eq([])+ end++ it 'returns users based on the full name' do+ expect(Spree.user_class.multi_search('joh do')).to eq([user_1])+ expect(Spree.user_class.multi_search('ane gon')).to eq([user_2])+ expect(Spree.user_class.multi_search('mary moe')).to eq([user_3])+ expect(Spree.user_class.multi_search('jane moe')).to eq([user_2, user_3])+ expect(Spree.user_class.multi_search('greg smith')).to eq([])+ end+ endend