When order cycle is selected, only show variants that are in it

This commit is contained in:
Rohan Mitchell
2013-09-09 10:40:15 +10:00
parent 57906e6f8b
commit d32a8b56d0
4 changed files with 54 additions and 0 deletions

View File

@@ -68,6 +68,10 @@ class OrderCycle < ActiveRecord::Base
self.distributors.include? distributor
end
def has_variant?(variant)
self.variants.include? variant
end
# -- Fees
def create_adjustments_for(line_item)

View File

@@ -0,0 +1,16 @@
/ replace_contents '#product-variants'
%h6.product-section-title= t(:variants)
%ul
- has_checked = false
- @product.variants.active(current_currency).each_with_index do |v,index|
- next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
- next if current_order_cycle.present? && !current_order_cycle.has_variant?(v) # All copied from spree apart from this line
- checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
- has_checked = true if checked
%li
= radio_button_tag "products[#{@product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders], 'data-price' => v.price_in(current_currency).display_price
%label{:for => ['products', @product.id, v.id].join('_')}
%span.variant-description= variant_options v
- if variant_price v
%span.price.diff= variant_price v

View File

@@ -65,6 +65,31 @@ feature %q{
end
end
describe "variant listing" do
it "shows only variants that are in the distributor and order cycle", js: true do
# Given a product with two variants
s = create(:supplier_enterprise)
d = create(:distributor_enterprise, name: 'Green Grass')
p = create(:simple_product, supplier: s)
v1 = create(:variant, product: p, is_master: false)
v2 = create(:variant, product: p, is_master: false)
# And only one of those is distributed by an order cycle
oc = create(:simple_order_cycle, suppliers: [s], distributors: [d], variants: [v1])
# When I am in that order cycle
visit root_path
click_link d.name
select '7 days', from: 'order_order_cycle_id'
# And I view the product
click_link p.name
# Then I should see only the relevant variant
page.all('#product-variants li input').count.should == 1
end
end
context "viewing a product, it provides a choice of distributor when adding to cart" do
it "works when no distributor is chosen" do
# Given a distributor and a product under it

View File

@@ -98,6 +98,15 @@ describe OrderCycle do
oc.should_not have_distributor(d2)
end
it "checks for variants" do
p1 = create(:simple_product)
p2 = create(:simple_product)
oc = create(:simple_order_cycle, suppliers: [p1.supplier], variants: [p1.master])
oc.should have_variant(p1.master)
oc.should_not have_variant(p2.master)
end
describe "product exchanges" do
before(:each) do
@oc = create(:simple_order_cycle)