Replace #valid_products_distributed_by by class

This commit is contained in:
Pau Perez
2019-03-06 13:49:53 +01:00
parent adb40d6c73
commit 347aa3c4ae
5 changed files with 24 additions and 27 deletions

View File

@@ -151,7 +151,6 @@ Layout/EmptyLines:
- 'app/models/exchange.rb'
- 'app/models/exchange_fee.rb'
- 'app/models/inventory_item.rb'
- 'app/models/order_cycle.rb'
- 'app/models/producer_property.rb'
- 'app/models/product_distribution.rb'
- 'app/models/spree/calculator_decorator.rb'

View File

@@ -1,11 +1,20 @@
# Finds valid products distributed by a particular distributor in an order cycle
#
# If a product without variants is added to an order cycle, and then some
# variants are added to that product, but not the order cycle, then the master
# variant should not available for customers to purchase. This class filters
# out such products so that the customer cannot purchase them.
class DistributedValidProducts
def initialize(order_cycle, distributor)
@order_cycle = order_cycle
@distributor = distributor
end
def all
# Returns an ActiveRecord relation without invalid products. Check
# #valid_products_distributed_by for details
#
# @return [ActiveRecord::Relation<Spree::Product>]
def relation
variants = order_cycle.variants_distributed_by(distributor)
products = variants.map(&:product).uniq
@@ -24,9 +33,6 @@ class DistributedValidProducts
# If a product without variants is added to an order cycle, and then some variants are added
# to that product, but not the order cycle, then the master variant should not available for
# customers to purchase.
#
# This method is used by #valid_products_distributed_by to filter out such products so that
# the customer cannot purchase them.
def product_has_only_obsolete_master_in_distribution?(product, distributed_variants)
product.has_variants? &&
distributed_variants.include?(product.master) &&

View File

@@ -50,7 +50,6 @@ class OrderCycle < ActiveRecord::Base
joins(:exchanges).merge(Exchange.outgoing).merge(Exchange.to_enterprise(distributor))
}
scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')
scoped
@@ -174,14 +173,6 @@ class OrderCycle < ActiveRecord::Base
variants_distributed_by(distributor).map(&:product).uniq
end
# If a product without variants is added to an order cycle, and then some variants are added
# to that product, but not the order cycle, then the master variant should not available for customers
# to purchase.
# This method filters out such products so that the customer cannot purchase them.
def valid_products_distributed_by(distributor)
DistributedValidProducts.new(self, distributor).all
end
def products
self.variants.map(&:product).uniq
end

View File

@@ -32,15 +32,16 @@ module OpenFoodNetwork
private
def load_products
if @order_cycle
scoper = ScopeProductToHub.new(@distributor)
return unless @order_cycle
scoper = ScopeProductToHub.new(@distributor)
@order_cycle.
valid_products_distributed_by(@distributor).
order(taxon_order).
each { |p| scoper.scope(p) }.
select { |p| !p.deleted? && p.has_stock_for_distribution?(@order_cycle, @distributor) }
end
DistributedValidProducts.new(@order_cycle, @distributor).
relation.
order(taxon_order).
each { |product| scoper.scope(product) }.
select do |product|
!product.deleted? && product.has_stock_for_distribution?(@order_cycle, @distributor)
end
end
def taxon_order

View File

@@ -18,7 +18,7 @@ describe DistributedValidProducts do
distributed_valid_products = described_class.new(order_cycle, distributor)
expect(distributed_valid_products.all).to eq([valid_product])
expect(distributed_valid_products.relation).to eq([valid_product])
end
context 'when the product has only an obsolete master variant in a distribution' do
@@ -34,7 +34,7 @@ describe DistributedValidProducts do
it 'does not return the obsolete product' do
distributed_valid_products = described_class.new(order_cycle, distributor)
expect(distributed_valid_products.all).to eq([unassociated_variant.product])
expect(distributed_valid_products.relation).to eq([unassociated_variant.product])
end
end
@@ -51,7 +51,7 @@ describe DistributedValidProducts do
it 'returns the product' do
distributed_valid_products = described_class.new(order_cycle, distributor)
expect(distributed_valid_products.all).to eq([product])
expect(distributed_valid_products.relation).to eq([product])
end
end
@@ -69,7 +69,7 @@ describe DistributedValidProducts do
it 'returns the product' do
distributed_valid_products = described_class.new(order_cycle, distributor)
expect(distributed_valid_products.all).to eq([product])
expect(distributed_valid_products.relation).to eq([product])
end
end
@@ -87,7 +87,7 @@ describe DistributedValidProducts do
it 'returns the product' do
distributed_valid_products = described_class.new(order_cycle, distributor)
expect(distributed_valid_products.all).to eq([product])
expect(distributed_valid_products.relation).to eq([product])
end
end
end