mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Fix incorrect count of enterprises (due to bug in rails issue #5554)
This commit is contained in:
@@ -13,9 +13,9 @@ class ApplicationController < ActionController::Base
|
||||
sidebar_distributors_limit = 5 #set false to disable TODO: move to app config
|
||||
sidebar_suppliers_limit = 5
|
||||
@sidebar_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.limit(sidebar_distributors_limit)
|
||||
@total_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.count(:distinct => true)
|
||||
@total_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.distinct_count
|
||||
@sidebar_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.limit(sidebar_suppliers_limit)
|
||||
@total_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.count(:distinct => true)
|
||||
@total_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.distinct_count
|
||||
end
|
||||
|
||||
# All render calls within the block will be performed with the specified format
|
||||
|
||||
@@ -18,10 +18,14 @@ class Enterprise < ActiveRecord::Base
|
||||
scope :is_primary_producer, where(:is_primary_producer => true)
|
||||
scope :is_distributor, where(:is_distributor => true)
|
||||
scope :with_supplied_active_products_on_hand, lambda {
|
||||
joins(:supplied_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') }
|
||||
joins(:supplied_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).uniq }
|
||||
scope :with_distributed_active_products_on_hand, lambda {
|
||||
joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') }
|
||||
joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).uniq }
|
||||
|
||||
# Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554
|
||||
def self.distinct_count
|
||||
count(distinct: true)
|
||||
end
|
||||
|
||||
def has_supplied_products_on_hand?
|
||||
self.supplied_products.where('count_on_hand > 0').present?
|
||||
|
||||
@@ -29,6 +29,7 @@ describe Enterprise do
|
||||
create(:product, :distributors => [d3], :on_hand => 0)
|
||||
|
||||
Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2]
|
||||
Enterprise.with_distributed_active_products_on_hand.distinct_count.should == 2
|
||||
end
|
||||
|
||||
it "returns suppliers with products in stock" do
|
||||
@@ -37,10 +38,12 @@ describe Enterprise do
|
||||
d3 = create(:supplier_enterprise)
|
||||
d4 = create(:supplier_enterprise)
|
||||
create(:product, :supplier => d1, :on_hand => 5)
|
||||
create(:product, :supplier => d1, :on_hand => 5)
|
||||
create(:product, :supplier => d2, :on_hand => 5, :available_on => 1.week.from_now)
|
||||
create(:product, :supplier => d3, :on_hand => 0)
|
||||
# supplier with no products, supplier with product out of stock, supplier with product thats unavailable, supplier with active product on hand
|
||||
Enterprise.with_supplied_active_products_on_hand.sort.should == [d1]
|
||||
Enterprise.with_supplied_active_products_on_hand.distinct_count.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user