Merge pull request #11891 from bmd08a1/ft/search-by-customer-fullname-with-comma

Allow searching orders by customers' full name with comma and full name reversed
This commit is contained in:
Konrad
2023-12-06 21:08:28 +01:00
committed by GitHub
4 changed files with 116 additions and 1 deletions

View File

@@ -24,6 +24,9 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
"order_bill_address_firstname",
"order_bill_address_lastname",
"order_bill_address_full_name",
"order_bill_address_full_name_reversed",
"order_bill_address_full_name_with_comma",
"order_bill_address_full_name_with_comma_reversed",
"variant_product_supplier_name",
"order_email",
"order_number",

View File

@@ -6,7 +6,8 @@ module Spree
self.belongs_to_required_by_default = false
searchable_attributes :firstname, :lastname, :phone, :full_name
searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_reversed,
:full_name_with_comma, :full_name_with_comma_reversed
searchable_associations :country, :state
belongs_to :country, class_name: "Spree::Country"
@@ -36,6 +37,24 @@ module Spree
)
end
ransacker :full_name_reversed, formatter: proc { |value| value.to_s } do |parent|
Arel::Nodes::SqlLiteral.new(
"CONCAT(#{parent.table_name}.lastname, ' ', #{parent.table_name}.firstname)"
)
end
ransacker :full_name_with_comma, formatter: proc { |value| value.to_s } do |parent|
Arel::Nodes::SqlLiteral.new(
"CONCAT(#{parent.table_name}.firstname, ', ', #{parent.table_name}.lastname)"
)
end
ransacker :full_name_with_comma_reversed, formatter: proc { |value| value.to_s } do |parent|
Arel::Nodes::SqlLiteral.new(
"CONCAT(#{parent.table_name}.lastname, ', ', #{parent.table_name}.firstname)"
)
end
def self.default
new(country: DefaultCountry.country)
end

View File

@@ -203,4 +203,49 @@ describe Spree::Address do
expect(result2).not_to include(address1)
end
end
describe "ransacker :full_name_with_comma" do
it "searches for records with matching full names with comma" do
address1 = create(:address, firstname: 'John', lastname: 'Doe')
address2 = create(:address, firstname: 'Jane', lastname: 'Smith')
result1 = described_class.ransack(full_name_with_comma_cont: 'John, Doe').result
expect(result1).to include(address1)
expect(result1).not_to include(address2)
result2 = described_class.ransack(full_name_with_comma_cont: 'Jane, Smith').result
expect(result2).to include(address2)
expect(result2).not_to include(address1)
end
end
describe "ransacker :full_name_reversed" do
it "searches for records with matching reversed full names" do
address1 = create(:address, firstname: 'John', lastname: 'Doe')
address2 = create(:address, firstname: 'Jane', lastname: 'Smith')
result1 = described_class.ransack(full_name_reversed_cont: 'Doe John').result
expect(result1).to include(address1)
expect(result1).not_to include(address2)
result2 = described_class.ransack(full_name_reversed_cont: 'Smith Jane').result
expect(result2).to include(address2)
expect(result2).not_to include(address1)
end
end
describe "ransacker :full_name_with_comma_reversed" do
it "searches for records with matching reversed full names" do
address1 = create(:address, firstname: 'John', lastname: 'Doe')
address2 = create(:address, firstname: 'Jane', lastname: 'Smith')
result1 = described_class.ransack(full_name_with_comma_reversed_cont: 'Doe, John').result
expect(result1).to include(address1)
expect(result1).not_to include(address2)
result2 = described_class.ransack(full_name_with_comma_reversed_cont: 'Smith, Jane').result
expect(result2).to include(address2)
expect(result2).not_to include(address1)
end
end
end

View File

@@ -230,15 +230,63 @@ describe '
end
it "by customer name" do
# by firstname
fill_in "quick_filter", with: o1.bill_address.firstname
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
# by lastname
fill_in "quick_filter", with: o1.bill_address.lastname
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
# by fullname
fill_in "quick_filter", with: "#{o1.bill_address.firstname} #{o1.bill_address.lastname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
fill_in "quick_filter", with: "#{o2.bill_address.firstname} #{o2.bill_address.lastname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li2, li3], [li1]
# by fullname reversed
fill_in "quick_filter", with: "#{o1.bill_address.lastname} #{o1.bill_address.firstname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
fill_in "quick_filter", with: "#{o2.bill_address.lastname} #{o2.bill_address.firstname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li2, li3], [li1]
# by fullname with comma
fill_in "quick_filter", with: "#{o1.bill_address.firstname}, #{o1.bill_address.lastname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
fill_in "quick_filter", with: "#{o2.bill_address.firstname}, #{o2.bill_address.lastname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li2, li3], [li1]
# by fullname with comma reversed
fill_in "quick_filter", with: "#{o1.bill_address.lastname}, #{o1.bill_address.firstname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li1], [li2, li3]
fill_in "quick_filter", with: "#{o2.bill_address.lastname}, #{o2.bill_address.firstname}"
page.find('.filter-actions .button.icon-search').click
expect_line_items_results [li2, li3], [li1]
end
end