Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow searching orders by customers' full name with comma and full name reversed #11891

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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_with_comma",
"order_bill_address_full_name_reversed",
"variant_product_supplier_name",
"order_email",
"order_number",
Expand Down
15 changes: 14 additions & 1 deletion app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Address < ApplicationRecord

self.belongs_to_required_by_default = false

searchable_attributes :firstname, :lastname, :phone, :full_name
searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_with_comma,
:full_name_reversed
searchable_associations :country, :state

belongs_to :country, class_name: "Spree::Country"
Expand Down Expand Up @@ -36,6 +37,18 @@ class Address < ApplicationRecord
)
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_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
Expand Down
30 changes: 30 additions & 0 deletions spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,34 @@
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
end
24 changes: 24 additions & 0 deletions spec/system/admin/bulk_order_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@

expect_line_items_results [li1], [li2, li3]
end

it "by customer fullname with comma" do
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]
end

it "by customer fullname reversed" do
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
bmd08a1 marked this conversation as resolved.
Show resolved Hide resolved
end

context "displaying individual columns" do
Expand Down
Loading