From d32a8b56d08fbb56010df17fea3077e6042b551f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 9 Sep 2013 10:40:15 +1000 Subject: [PATCH] When order cycle is selected, only show variants that are in it --- app/models/order_cycle.rb | 4 +++ ...ent_distribution_variants.html.haml.deface | 16 ++++++++++++ .../features/consumer/browse_products_spec.rb | 25 +++++++++++++++++++ spec/models/order_cycle_spec.rb | 9 +++++++ 4 files changed, 54 insertions(+) create mode 100644 app/overrides/spree/products/_cart_form/only_show_current_distribution_variants.html.haml.deface diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 2f48de84aa..dc8e9dd77e 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -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) diff --git a/app/overrides/spree/products/_cart_form/only_show_current_distribution_variants.html.haml.deface b/app/overrides/spree/products/_cart_form/only_show_current_distribution_variants.html.haml.deface new file mode 100644 index 0000000000..adfe5af84f --- /dev/null +++ b/app/overrides/spree/products/_cart_form/only_show_current_distribution_variants.html.haml.deface @@ -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 diff --git a/spec/features/consumer/browse_products_spec.rb b/spec/features/consumer/browse_products_spec.rb index 99c51048cc..acb40278d8 100644 --- a/spec/features/consumer/browse_products_spec.rb +++ b/spec/features/consumer/browse_products_spec.rb @@ -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 diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 8933e6a58c..0dac0a839c 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -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)