From b8fcfbd72b3c7f08bc6a021370991aad03feb369 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 1 Nov 2013 14:48:23 +1100 Subject: [PATCH] Test and implementation: dropping inclusion of product_distributions from active Distributors on front page - BugHerd#125 --- app/helpers/temp_landing_page_helper.rb | 2 +- app/models/enterprise.rb | 8 +++ .../consumer/temp_landing_page_spec.rb | 4 +- spec/models/enterprises_spec.rb | 66 ++++++++++++------- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/app/helpers/temp_landing_page_helper.rb b/app/helpers/temp_landing_page_helper.rb index a187c5098f..02277e7322 100644 --- a/app/helpers/temp_landing_page_helper.rb +++ b/app/helpers/temp_landing_page_helper.rb @@ -1,7 +1,7 @@ module TempLandingPageHelper def temp_landing_page_distributor_link_class(distributor) cart = current_order(true) - @active_distributors ||= Enterprise.active_distributors + @active_distributors ||= Enterprise.distributors_with_active_order_cycles klass = "shop-distributor" klass += " empties-cart" unless cart.line_items.empty? || cart.distributor == distributor diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index ebc24fc13b..83b599b04c 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -47,6 +47,7 @@ class Enterprise < ActiveRecord::Base scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id)'). joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') + scope :with_order_cycles_and_exchange_variants_outer, with_order_cycles_outer. joins('LEFT OUTER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)'). @@ -57,6 +58,13 @@ class Enterprise < ActiveRecord::Base where('(product_distributions.product_id IS NOT NULL AND spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0) OR (order_cycles.id IS NOT NULL AND order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?)', Time.now, Time.now, Time.now). select('DISTINCT enterprises.*') } + + scope :distributors_with_active_order_cycles, lambda { + with_order_cycles_outer. + merge(OrderCycle.active). + select('DISTINCT enterprises.*') + } + scope :distributing_product, lambda { |product| with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer. where('product_distributions.product_id = ? OR spree_variants.product_id = ?', product, product). diff --git a/spec/features/consumer/temp_landing_page_spec.rb b/spec/features/consumer/temp_landing_page_spec.rb index 52433605bb..7275d86c73 100644 --- a/spec/features/consumer/temp_landing_page_spec.rb +++ b/spec/features/consumer/temp_landing_page_spec.rb @@ -58,7 +58,7 @@ feature %q{ page.should have_link "PepperTree Place" end - it "should grey out hubs that have no products available for distribution and are not in an order cycle" do + it "should grey out hubs that are not in an order cycle" do create(:simple_order_cycle, distributors: [d1, d3]) create(:simple_product, distributors: [d1, d2]) @@ -66,7 +66,7 @@ feature %q{ visit root_path page.should have_selector 'a.shop-distributor.active', text: 'Murandaka' - page.should have_selector 'a.shop-distributor.active', text: 'Ballantyne' + page.should have_selector 'a.shop-distributor.inactive', text: 'Ballantyne' page.should have_selector 'a.shop-distributor.active', text: "O'Hea Street" page.should have_selector 'a.shop-distributor.inactive', text: 'PepperTree Place' end diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 3c6278b300..98e7a90f7b 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -47,6 +47,25 @@ describe Enterprise do end describe "scopes" do + + describe "distributors_with_active_order_cycles" do + it "finds active distributors by order cycles" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + p = create(:product) + create(:simple_order_cycle, suppliers: [s], distributors: [d], variants: [p.master]) + Enterprise.distributors_with_active_order_cycles.should == [d] + end + + it "should not find inactive distributors by order cycles" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + p = create(:product) + create(:simple_order_cycle, :orders_open_at => 10.days.from_now, suppliers: [s], distributors: [d], variants: [p.master]) + Enterprise.distributors_with_active_order_cycles.should_not include d + end + end + describe "active_distributors" do it "finds active distributors by product distributions" do d = create(:distributor_enterprise) @@ -88,28 +107,6 @@ describe Enterprise do Enterprise.active_distributors.should be_empty end - it "shows only enterprises for given user" do - user = create(:user) - user.spree_roles = [] - e1 = create(:enterprise) - e2 = create(:enterprise) - e1.enterprise_roles.build(user: user).save - - enterprises = Enterprise.managed_by user - enterprises.count.should == 1 - enterprises.should include e1 - end - - it "shows all enterprises for admin user" do - user = create(:admin_user) - e1 = create(:enterprise) - e2 = create(:enterprise) - - enterprises = Enterprise.managed_by user - enterprises.count.should == 2 - enterprises.should include e1 - enterprises.should include e2 - end end describe "with_distributed_active_products_on_hand" do @@ -199,6 +196,31 @@ describe Enterprise do Enterprise.distributing_any_product_of([p1, p2]).should == [d] end end + + describe "managed_by" do + it "shows only enterprises for given user" do + user = create(:user) + user.spree_roles = [] + e1 = create(:enterprise) + e2 = create(:enterprise) + e1.enterprise_roles.build(user: user).save + + enterprises = Enterprise.managed_by user + enterprises.count.should == 1 + enterprises.should include e1 + end + + it "shows all enterprises for admin user" do + user = create(:admin_user) + e1 = create(:enterprise) + e2 = create(:enterprise) + + enterprises = Enterprise.managed_by user + enterprises.count.should == 2 + enterprises.should include e1 + enterprises.should include e2 + end + end end describe "has_supplied_products_on_hand?" do