From bc6110c178b1183eeafa7d9b9130d5acb20b5bcb Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 17 Dec 2015 12:42:28 +1100 Subject: [PATCH] Re-defining in_distributor scope as method to allow chaining --- app/models/spree/variant_decorator.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index c3b6b86c02..33e7f5bffd 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -29,12 +29,6 @@ Spree::Variant.class_eval do scope :not_deleted, where(deleted_at: nil) scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true) - scope :in_distributor, lambda { |distributor| - where(id: ExchangeVariant.select(:variant_id). - joins(:exchange). - where('exchanges.incoming = ? AND exchanges.receiver_id = ?', false, distributor) - ) - } scope :in_order_cycle, lambda { |order_cycle| with_order_cycles_inner. merge(Exchange.outgoing). @@ -46,6 +40,14 @@ Spree::Variant.class_eval do where('spree_variants.id IN (?)', order_cycle.variants_distributed_by(distributor)) } + # Define sope as class method to allow chaining with other scopes filtering id. + # In Rails 3, merging two scopes on the same column will consider only the last scope. + def self.in_distributor(distributor) + where(id: ExchangeVariant.select(:variant_id). + joins(:exchange). + where('exchanges.incoming = ? AND exchanges.receiver_id = ?', false, distributor) + ) + end def self.indexed Hash[