Adapt exchanges.with_any_variant scope to rails 4

This commit is contained in:
luisramos0
2019-04-07 11:31:31 +01:00
parent 0d07bf2a3b
commit ba91abd20e
5 changed files with 15 additions and 15 deletions

View File

@@ -29,7 +29,7 @@ class Exchange < ActiveRecord::Base
scope :involving, lambda { |enterprises| where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises).select('DISTINCT exchanges.*') }
scope :supplying_to, lambda { |distributor| where('exchanges.incoming OR exchanges.receiver_id = ?', distributor) }
scope :with_variant, lambda { |variant| joins(:exchange_variants).where('exchange_variants.variant_id = ?', variant) }
scope :with_any_variant, lambda { |variants| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', variants).select('DISTINCT exchanges.*') }
scope :with_any_variant, lambda { |variant_ids| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', variant_ids).select('DISTINCT exchanges.*') }
scope :with_product, lambda { |product| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', product.variants_including_master) }
scope :by_enterprise_name, -> {
joins('INNER JOIN enterprises AS sender ON (sender.id = exchanges.sender_id)').

View File

@@ -52,7 +52,7 @@ class OrderCycle < ActiveRecord::Base
if user.has_spree_role?('admin')
scoped
else
where('coordinator_id IN (?)', user.enterprises)
where('coordinator_id IN (?)', user.enterprises.map(&:id))
end
}
@@ -217,7 +217,7 @@ class OrderCycle < ActiveRecord::Base
end
def exchanges_supplying(order)
exchanges.supplying_to(order.distributor).with_any_variant(order.variants)
exchanges.supplying_to(order.distributor).with_any_variant(order.variants.map(&:id))
end
def coordinated_by?(user)

View File

@@ -55,7 +55,7 @@ module OpenFoodNetwork
# TODO: Remove this when all P-OC are sorted out
# Any hubs that currently have outgoing exchanges distributing variants of producers I manage
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer)
active_exchanges = @order_cycle.exchanges.outgoing.with_any_variant(variants)
active_exchanges = @order_cycle.exchanges.outgoing.with_any_variant(variants.select("spree_variants.id"))
hubs_active = active_exchanges.map(&:receiver_id)
# TODO: Remove this when all P-OC are sorted out
@@ -249,7 +249,7 @@ module OpenFoodNetwork
# outgoing exchange to those where the producer had granted P-OC to the distributor
# For any of my managed producers, any outgoing exchanges with their variants
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
active_exchanges = @order_cycle.exchanges.outgoing.with_any_variant(variants).pluck :id
active_exchanges = @order_cycle.exchanges.outgoing.with_any_variant(variants.select("spree_variants.id")).pluck :id
permitted_exchanges | active_exchanges
end

View File

@@ -5,13 +5,13 @@ require 'open_food_network/products_cache_refreshment'
module OpenFoodNetwork
class ProductsCache
def self.variant_changed(variant)
exchanges_featuring_variants(variant).each do |exchange|
exchanges_featuring_variants(variant.id).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
def self.variant_destroyed(variant, &block)
exchanges = exchanges_featuring_variants(variant).to_a
exchanges = exchanges_featuring_variants(variant.id).to_a
block.call
@@ -21,13 +21,13 @@ module OpenFoodNetwork
end
def self.product_changed(product)
exchanges_featuring_variants(product.variants).each do |exchange|
exchanges_featuring_variants(product.variants.map(&:id)).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
def self.product_deleted(product, &block)
exchanges = exchanges_featuring_variants(product.reload.variants).to_a
exchanges = exchanges_featuring_variants(product.reload.variants.map(&:id)).to_a
block.call
@@ -37,7 +37,7 @@ module OpenFoodNetwork
end
def self.variant_override_changed(variant_override)
exchanges_featuring_variants(variant_override.variant, distributor: variant_override.hub).each do |exchange|
exchanges_featuring_variants(variant_override.variant.id, distributor: variant_override.hub).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
@@ -52,7 +52,7 @@ module OpenFoodNetwork
where(is_master: false, deleted_at: nil).
where(product_id: products)
exchanges_featuring_variants(variants).each do |exchange|
exchanges_featuring_variants(variants.select(:id)).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
@@ -94,17 +94,17 @@ module OpenFoodNetwork
end
def self.inventory_item_changed(inventory_item)
exchanges_featuring_variants(inventory_item.variant, distributor: inventory_item.enterprise).each do |exchange|
exchanges_featuring_variants(inventory_item.variant.id, distributor: inventory_item.enterprise).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
private
def self.exchanges_featuring_variants(variants, distributor: nil)
def self.exchanges_featuring_variants(variant_ids, distributor: nil)
exchanges = Exchange.
outgoing.
with_any_variant(variants).
with_any_variant(variant_ids).
joins(:order_cycle).
merge(OrderCycle.dated).
merge(OrderCycle.not_closed)

View File

@@ -240,7 +240,7 @@ describe Exchange do
ex.variants << v1
ex.variants << v2
expect(Exchange.with_any_variant([v1, v2, v3])).to eq([ex])
expect(Exchange.with_any_variant([v1.id, v2.id, v3.id])).to eq([ex])
end
it "finds exchanges with a particular product's master variant" do