Do not show deleted variants on shopfront

This commit is contained in:
Rohan Mitchell
2014-04-10 12:35:49 +10:00
parent 9cb56c61ae
commit 47ac097eb7
5 changed files with 24 additions and 2 deletions

View File

@@ -87,15 +87,16 @@ class OrderCycle < ActiveRecord::Base
end
def variants
self.exchanges.map(&:variants).flatten.uniq
self.exchanges.map(&:variants).flatten.uniq.reject(&:deleted?)
end
def distributed_variants
self.exchanges.outgoing.map(&:variants).flatten.uniq
self.exchanges.outgoing.map(&:variants).flatten.uniq.reject(&:deleted?)
end
def variants_distributed_by(distributor)
Spree::Variant.
not_deleted.
joins(:exchanges).
merge(Exchange.in_order_cycle(self)).
merge(Exchange.outgoing).

View File

@@ -15,6 +15,7 @@ Spree::Variant.class_eval do
before_validation :update_weight_from_unit_value
after_save :update_units
scope :not_deleted, where(deleted_at: nil)
scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true)

View File

@@ -151,11 +151,13 @@ feature "As a consumer I want to shop with a distributor", js: true do
let(:p4) { create(:simple_product, on_demand: false) }
let(:p5) { create(:simple_product, on_demand: false) }
let(:p6) { create(:simple_product, on_demand: false) }
let(:p7) { create(:simple_product, on_demand: false) }
let(:v1) { create(:variant, product: p4, unit_value: 2) }
let(:v2) { create(:variant, product: p4, unit_value: 3, on_demand: false) }
let(:v3) { create(:variant, product: p4, unit_value: 4, on_demand: true) }
let(:v4) { create(:variant, product: p5) }
let(:v5) { create(:variant, product: p5) }
let(:v6) { create(:variant, product: p7) }
before do
p1.master.count_on_hand = 1
@@ -165,11 +167,14 @@ feature "As a consumer I want to shop with a distributor", js: true do
p3.master.update_attribute(:count_on_hand, 0)
p6.master.update_attribute(:count_on_hand, 1)
p6.delete
p7.master.update_attribute(:count_on_hand, 1)
v1.update_attribute(:count_on_hand, 1)
v2.update_attribute(:count_on_hand, 0)
v3.update_attribute(:count_on_hand, 0)
v4.update_attribute(:count_on_hand, 1)
v5.update_attribute(:count_on_hand, 0)
v6.update_attribute(:count_on_hand, 1)
v6.update_attribute(:deleted_at, Time.now)
exchange = Exchange.find(oc.exchanges.to_enterprises(distributor).outgoing.first.id)
exchange.update_attribute :pickup_time, "frogs"
exchange.variants << p1.master
@@ -183,6 +188,7 @@ feature "As a consumer I want to shop with a distributor", js: true do
# v5 is out of stock and in the distribution
# Neither should display, nor should their product, p5
exchange.variants << v5
exchange.variants << v6
visit shop_path
select "frogs", :from => "order_cycle_id"
exchange
@@ -210,6 +216,10 @@ feature "As a consumer I want to shop with a distributor", js: true do
# It does not show deleted products
page.should_not have_content p6.name
# It does not show deleted variants
page.should_not have_content v6.name
page.should_not have_content p7.name
end
end

View File

@@ -191,6 +191,7 @@ describe OrderCycle do
@p0 = create(:simple_product)
@p1 = create(:simple_product)
@p1_v_deleted = create(:variant, product: @p1, deleted_at: Time.now)
@p2 = create(:simple_product)
@p2_v = create(:variant, product: @p2)
@@ -199,6 +200,7 @@ describe OrderCycle do
@e1.variants << @p2.master
@e1.variants << @p2_v
@e2.variants << @p1.master
@e2.variants << @p1_v_deleted
end
it "reports on the variants exchanged" do

View File

@@ -3,6 +3,14 @@ require 'spec_helper'
module Spree
describe Variant do
describe "scopes" do
it "finds non-deleted variants" do
v_not_deleted = create(:variant)
v_deleted = create(:variant, deleted_at: Time.now)
Spree::Variant.not_deleted.should include v_not_deleted
Spree::Variant.not_deleted.should_not include v_deleted
end
describe "finding variants in stock" do
before do
p = create(:product)