From febda2ce12af1f1f2c30fd9a0f9f116375c6bace Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sun, 3 Dec 2023 14:10:12 +0700 Subject: [PATCH 1/4] add search by fullname with comma --- .../controllers/line_items_controller.js.coffee | 1 + app/models/spree/address.rb | 8 +++++++- spec/models/spree/address_spec.rb | 15 +++++++++++++++ spec/system/admin/bulk_order_management_spec.rb | 12 ++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee index 88d6fd6ab9..1e4af21993 100644 --- a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee +++ b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee @@ -24,6 +24,7 @@ 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", "variant_product_supplier_name", "order_email", "order_number", diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index 703babb4ee..df90e0b160 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -6,7 +6,7 @@ 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_with_comma searchable_associations :country, :state belongs_to :country, class_name: "Spree::Country" @@ -36,6 +36,12 @@ module Spree ) 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 + def self.default new(country: DefaultCountry.country) end diff --git a/spec/models/spree/address_spec.rb b/spec/models/spree/address_spec.rb index 66e07f5983..1d3cd316ab 100644 --- a/spec/models/spree/address_spec.rb +++ b/spec/models/spree/address_spec.rb @@ -203,4 +203,19 @@ 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 end diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index 153e7f8b12..0494357c4a 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -240,6 +240,18 @@ describe ' 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 end context "displaying individual columns" do From 600226cb527bbb3d473c9913175249b2292acbe0 Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sun, 3 Dec 2023 14:15:41 +0700 Subject: [PATCH 2/4] add fullname reversed search --- .../controllers/line_items_controller.js.coffee | 1 + app/models/spree/address.rb | 9 ++++++++- spec/models/spree/address_spec.rb | 15 +++++++++++++++ spec/system/admin/bulk_order_management_spec.rb | 12 ++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee index 1e4af21993..b9939582a5 100644 --- a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee +++ b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee @@ -25,6 +25,7 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, "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", diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index df90e0b160..4735b5a3ce 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -6,7 +6,8 @@ module Spree self.belongs_to_required_by_default = false - searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_with_comma + 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" @@ -42,6 +43,12 @@ 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 + def self.default new(country: DefaultCountry.country) end diff --git a/spec/models/spree/address_spec.rb b/spec/models/spree/address_spec.rb index 1d3cd316ab..36e06de131 100644 --- a/spec/models/spree/address_spec.rb +++ b/spec/models/spree/address_spec.rb @@ -218,4 +218,19 @@ describe Spree::Address do 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 diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index 0494357c4a..88a81c172f 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -252,6 +252,18 @@ describe ' 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 end context "displaying individual columns" do From 6206164c301b1aba4171bee6b6e6ea86e384531f Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sun, 3 Dec 2023 14:28:50 +0700 Subject: [PATCH 3/4] fix rubocop --- app/models/spree/address.rb | 2 +- spec/system/admin/bulk_order_management_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index 4735b5a3ce..bf784450b7 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -7,7 +7,7 @@ module Spree self.belongs_to_required_by_default = false searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_with_comma, - :full_name_reversed + :full_name_reversed searchable_associations :country, :state belongs_to :country, class_name: "Spree::Country" diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index 88a81c172f..eb2b70f53b 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -242,24 +242,24 @@ describe ' end it "by customer fullname with comma" do - fill_in "quick_filter", with: o1.bill_address.firstname + ', ' + o1.bill_address.lastname + 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 + 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 + 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 + 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] From 8a3aababa166b433aeab5e2c3daa8b9055e2dc1e Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Mon, 4 Dec 2023 08:45:19 +0700 Subject: [PATCH 4/4] group specs into one it block, added fullname with comma reversed ransacker --- .../line_items_controller.js.coffee | 3 +- app/models/spree/address.rb | 14 +++++--- spec/models/spree/address_spec.rb | 15 ++++++++ .../admin/bulk_order_management_spec.rb | 36 +++++++++++++++---- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee index b9939582a5..e19c5c7b8b 100644 --- a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee +++ b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee @@ -24,8 +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_with_comma", "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", diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index bf784450b7..59363374f1 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -6,8 +6,8 @@ module Spree self.belongs_to_required_by_default = false - searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_with_comma, - :full_name_reversed + 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" @@ -37,15 +37,21 @@ 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_reversed, formatter: proc { |value| value.to_s } do |parent| + 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)" + "CONCAT(#{parent.table_name}.lastname, ', ', #{parent.table_name}.firstname)" ) end diff --git a/spec/models/spree/address_spec.rb b/spec/models/spree/address_spec.rb index 36e06de131..090a113aa6 100644 --- a/spec/models/spree/address_spec.rb +++ b/spec/models/spree/address_spec.rb @@ -233,4 +233,19 @@ describe Spree::Address do 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 diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index eb2b70f53b..b9b8e2c635 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -230,19 +230,43 @@ 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] - end - it "by customer fullname with comma" do + # 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] @@ -251,15 +275,15 @@ describe ' 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}" + # 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}" + 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]