diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9905760a71..d8a8fcb5bd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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' diff --git a/app/models/distributed_valid_products.rb b/app/models/distributed_valid_products.rb index 2151320b91..af411e0748 100644 --- a/app/models/distributed_valid_products.rb +++ b/app/models/distributed_valid_products.rb @@ -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] + 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) && diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 7122d9cee7..59547ee086 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -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 diff --git a/lib/open_food_network/products_renderer.rb b/lib/open_food_network/products_renderer.rb index 6dd0d5f5ed..9ccee412da 100644 --- a/lib/open_food_network/products_renderer.rb +++ b/lib/open_food_network/products_renderer.rb @@ -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 diff --git a/spec/models/distributed_valid_products_spec.rb b/spec/models/distributed_valid_products_spec.rb index b2c50ef022..b5ce260393 100644 --- a/spec/models/distributed_valid_products_spec.rb +++ b/spec/models/distributed_valid_products_spec.rb @@ -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