From 93ad772fda3438d4845493b8ad5513df52bdf54d Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Tue, 6 Dec 2016 14:19:29 -0800 Subject: [PATCH] Clean up Ransack query for promotion code This commit fixes up the ransack search when attempting to find Orders via a promotion code, by using one fewer join in the resulting query and going directly through the spree_orders_promotions table. This commit exists upstream as a WIP from @jhawthorn: https://github.com/solidusio/solidus/commit/22ebde78b0daffae7e9bf933e1fa5169747360fa --- backend/app/views/spree/admin/orders/index.html.erb | 2 +- backend/spec/features/admin/orders/listing_spec.rb | 2 +- core/app/models/spree/order.rb | 2 +- core/app/models/spree/order_promotion.rb | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/app/views/spree/admin/orders/index.html.erb b/backend/app/views/spree/admin/orders/index.html.erb index ca9c1c3a82d..eecf8269dfd 100644 --- a/backend/app/views/spree/admin/orders/index.html.erb +++ b/backend/app/views/spree/admin/orders/index.html.erb @@ -36,7 +36,7 @@
<%= label_tag nil, Spree.t(:promotion) %> - <%= f.text_field :promotions_codes_value_cont, :size => 25 %> + <%= f.text_field :order_promotions_promotion_code_value_cont, :size => 25 %>
diff --git a/backend/spec/features/admin/orders/listing_spec.rb b/backend/spec/features/admin/orders/listing_spec.rb index ab050d7a0a2..7be0d93796e 100644 --- a/backend/spec/features/admin/orders/listing_spec.rb +++ b/backend/spec/features/admin/orders/listing_spec.rb @@ -120,7 +120,7 @@ it "only shows the orders with the selected promotion" do click_on 'Filter' - fill_in "q_promotions_codes_value_cont", with: promotion.codes.first.value + fill_in "q_order_promotions_promotion_code_value_cont", with: promotion.codes.first.value click_on 'Filter Results' within_row(1) { expect(page).to have_content("R100") } within("table#listing_orders") { expect(page).not_to have_content("R200") } diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4724915818d..b3c209e1e5e 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -27,7 +27,7 @@ class CannotRebuildShipments < StandardError; end go_to_state :confirm end - self.whitelisted_ransackable_associations = %w[shipments user promotions bill_address ship_address line_items] + self.whitelisted_ransackable_associations = %w[shipments user order_promotions promotions bill_address ship_address line_items] self.whitelisted_ransackable_attributes = %w[completed_at created_at email number state payment_state shipment_state total] attr_reader :coupon_code diff --git a/core/app/models/spree/order_promotion.rb b/core/app/models/spree/order_promotion.rb index 3e675e990b0..800134ef191 100644 --- a/core/app/models/spree/order_promotion.rb +++ b/core/app/models/spree/order_promotion.rb @@ -14,6 +14,8 @@ class OrderPromotion < Spree::Base validates :promotion, presence: true validates :promotion_code, presence: true, if: :require_promotion_code? + self.whitelisted_ransackable_associations = %w[promotion_code] + private def require_promotion_code?