Add Spree::Variant.in_order_cycle

This commit is contained in:
Rohan Mitchell
2014-07-11 12:48:12 +10:00
parent 0ecfee79d4
commit b7a08bcb9b
2 changed files with 29 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ Spree::Variant.class_eval do
before_validation :update_weight_from_unit_value
after_save :update_units
scope :with_order_cycles_inner, joins(exchanges: :order_cycle)
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = spree_variants.id)').
joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)')
@@ -30,6 +31,13 @@ Spree::Variant.class_eval do
select('DISTINCT spree_variants.*')
}
scope :in_order_cycle, lambda { |order_cycle|
with_order_cycles_inner.
merge(Exchange.outgoing).
where('order_cycles.id = ?', order_cycle).
select('DISTINCT spree_variants.*')
}
def price_with_fees(distributor, order_cycle)
price + fees_for(distributor, order_cycle)

View File

@@ -46,6 +46,27 @@ module Spree
Variant.in_distributor(d1).should == [p1.master]
end
end
describe "finding variants in an order cycle" do
let!(:d1) { create(:distributor_enterprise) }
let!(:d2) { create(:distributor_enterprise) }
let!(:p1) { create(:product) }
let!(:p2) { create(:product) }
let!(:oc1) { create(:simple_order_cycle, distributors: [d1], variants: [p1.master]) }
let!(:oc2) { create(:simple_order_cycle, distributors: [d2], variants: [p2.master]) }
it "shows variants in an order cycle" do
Variant.in_order_cycle(oc1).should == [p1.master]
end
it "doesn't show duplicates" do
ex = create(:exchange, order_cycle: oc1, sender: oc1.coordinator, receiver: d2)
ex.variants << p1.master
Variant.in_order_cycle(oc1).should == [p1.master]
end
end
end
describe "calculating the price with enterprise fees" do