From e7828e107b3d078afb20e7cd9a28a1331f865d5a Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 1 Jun 2020 14:07:34 +0100 Subject: [PATCH 1/2] Delete dead methods from product_decorator --- app/models/spree/product_decorator.rb | 12 ------- spec/models/spree/product_spec.rb | 51 --------------------------- 2 files changed, 63 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index fe20fe5194..4b99aff8f2 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -158,18 +158,6 @@ Spree::Product.class_eval do self.class.in_order_cycle(order_cycle).include? self end - # overriding to check self.on_demand as well - def has_stock? - has_variants? ? variants.any?(&:in_stock?) : (on_demand || master.in_stock?) - end - - def has_stock_for_distribution?(order_cycle, distributor) - # This product has stock for a distribution if it is available on-demand - # or if one of its variants in the distribution is in stock - (!has_variants? && on_demand) || - variants_distributed_by(order_cycle, distributor).any?(&:in_stock?) - end - def variants_distributed_by(order_cycle, distributor) order_cycle.variants_distributed_by(distributor).where(product_id: self) end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index a8c6368a70..15d79596ee 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -579,57 +579,6 @@ module Spree end end - describe "stock filtering" do - it "considers products that are on_demand as being in stock" do - product = create(:simple_product, on_demand: true) - product.master.update_attribute(:on_hand, 0) - expect(product.has_stock?).to eq(true) - end - - describe "finding products in stock for a particular distribution" do - it "returns on-demand products" do - p = create(:simple_product, on_demand: true) - p.variants.first.update_attributes!(on_hand: 0, on_demand: true) - d = create(:distributor_enterprise) - oc = create(:simple_order_cycle, distributors: [d]) - oc.exchanges.outgoing.first.variants << p.variants.first - - expect(p).to have_stock_for_distribution(oc, d) - end - - it "returns products with in-stock variants" do - p = create(:simple_product) - v = create(:variant, product: p) - v.update_attribute(:on_hand, 1) - d = create(:distributor_enterprise) - oc = create(:simple_order_cycle, distributors: [d]) - oc.exchanges.outgoing.first.variants << v - - expect(p).to have_stock_for_distribution(oc, d) - end - - it "returns products with on-demand variants" do - p = create(:simple_product) - v = create(:variant, product: p, on_demand: true) - v.update_attribute(:on_hand, 0) - d = create(:distributor_enterprise) - oc = create(:simple_order_cycle, distributors: [d]) - oc.exchanges.outgoing.first.variants << v - - expect(p).to have_stock_for_distribution(oc, d) - end - - it "does not return products that have stock not in the distribution" do - p = create(:simple_product) - p.master.update_attribute(:on_hand, 1) - d = create(:distributor_enterprise) - oc = create(:simple_order_cycle, distributors: [d]) - - expect(p).not_to have_stock_for_distribution(oc, d) - end - end - end - describe "taxons" do let(:taxon1) { create(:taxon) } let(:taxon2) { create(:taxon) } From 072da7316e6a8ad1210088dc91e8737a9d60bc89 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 1 Jun 2020 14:09:14 +0100 Subject: [PATCH 2/2] Remove views code that tests for the existence of product variants A product always has the standard variant, the only case where a product will not have variants is immediatly after creation and before being saved --- app/views/spree/admin/images/index.html.haml | 9 +-- .../spree/admin/products/_form.html.haml | 31 ---------- .../spree/admin/variants/index.html.haml | 58 ++++++++----------- 3 files changed, 28 insertions(+), 70 deletions(-) diff --git a/app/views/spree/admin/images/index.html.haml b/app/views/spree/admin/images/index.html.haml index 89124383e2..37cc53c70b 100644 --- a/app/views/spree/admin/images/index.html.haml +++ b/app/views/spree/admin/images/index.html.haml @@ -15,15 +15,13 @@ %colgroup %col{ style: "width: 5%" }/ %col{ style: "width: 10%" }/ - - if @product.has_variants? - %col{ style: "width: 25%" }/ + %col{ style: "width: 25%" }/ %col{ style: "width: 45%" }/ %col{ style: "width: 15%" }/ %thead %tr %th{:colspan => "2"}= t('spree.thumbnail') - - if @product.has_variants? - %th= Spree::Variant.model_name.human + %th= Spree::Variant.model_name.human %th= t('spree.alt_text') %th.actions %tbody @@ -35,8 +33,7 @@ %span.handle %td = link_to image_tag(image.attachment.url(:mini)), image.attachment.url(:product) - - if @product.has_variants? - %td= options_text_for(image) + %td= options_text_for(image) %td= image.alt %td.actions = link_to_with_icon 'icon-edit', t('spree.edit'), edit_admin_product_image_url(@product, image), no_text: true, data: { action: 'edit'} diff --git a/app/views/spree/admin/products/_form.html.haml b/app/views/spree/admin/products/_form.html.haml index 616f7977f3..08386e10dd 100644 --- a/app/views/spree/admin/products/_form.html.haml +++ b/app/views/spree/admin/products/_form.html.haml @@ -43,37 +43,6 @@ .clear - - unless @product.has_variants? - = f.field_container :sku do - = f.label :sku, t(:sku) - = f.text_field :sku, :size => 16 - - .alpha.two.columns - = f.field_container :on_hand do - = f.label :on_hand, t(:on_hand) - = f.number_field :on_hand, :min => 0 - .omega.two.columns - = f.field_container :on_demand, :class => ['checkbox'] do - %label - = f.check_box :on_demand - = t(:on_demand) - - .clear - - %ul#shipping_specs - %li#shipping_specs_weight_field.field.alpha.two.columns - = f.label :weight, t(:weight) - = f.text_field :weight, :size => 4 - %li#shipping_specs_height_field.field.omega.two.columns - = f.label :height, t(:height) - = f.text_field :height, :size => 4 - %li#shipping_specs_width_field.field.alpha.two.columns - = f.label :width, t(:width) - = f.text_field :width, :size => 4 - %li#shipping_specs_depth_field.field.omega.two.columns - = f.label :depth, t(:depth) - = f.text_field :depth, :size => 4 - = f.field_container :shipping_categories do = f.label :shipping_category_id, t(:shipping_categories) = f.collection_select(:shipping_category_id, @shipping_categories, :id, :name, { :include_blank => 'None' }, { :class => 'select2' }) diff --git a/app/views/spree/admin/variants/index.html.haml b/app/views/spree/admin/variants/index.html.haml index 199bcc0501..ce9c1edd86 100644 --- a/app/views/spree/admin/variants/index.html.haml +++ b/app/views/spree/admin/variants/index.html.haml @@ -3,40 +3,32 @@ = render partial: 'spree/admin/shared/product_tabs', locals: {current: 'Variants'} #new_variant -- if @variants.any? - %table.index.sortable{"data-sortable-link" => update_positions_admin_product_variants_path(@product)} - %colgroup - %col{style: "width: 5%"}/ - %col{style: "width: 25%"}/ - %col{style: "width: 20%"}/ - %col{style: "width: 20%"}/ - %col{style: "width: 15%"}/ - %col{style: "width: 15%"}/ - %thead - %tr - %th{colspan: "2"}= t('.options') - %th= t('.price') - %th= t('.sku') - %th.actions - %tbody - - @variants.each do |variant| - %tr{id: spree_dom_id(variant), class: cycle('odd', 'even'), style: "#{"color:red;" if variant.deleted? }" } - %td.no-border - %span.handle - %td= variant.full_name - %td.align-center= variant.display_price.to_html - %td.align-center= variant.sku - %td.actions - = link_to_edit(variant, no_text: true) unless variant.deleted? - = link_to_delete(variant, no_text: true) unless variant.deleted? - - unless @product.has_variants? - %tr - %td{colspan: "5"}= t(:none) -- else - .alpha.twelve.columns.no-objects-found - = t('.no_results') - \. +%table.index.sortable{"data-sortable-link" => update_positions_admin_product_variants_path(@product)} + %colgroup + %col{style: "width: 5%"}/ + %col{style: "width: 25%"}/ + %col{style: "width: 20%"}/ + %col{style: "width: 20%"}/ + %col{style: "width: 15%"}/ + %col{style: "width: 15%"}/ + %thead + %tr + %th{colspan: "2"}= t('.options') + %th= t('.price') + %th= t('.sku') + %th.actions + %tbody + - @variants.each do |variant| + %tr{id: spree_dom_id(variant), class: cycle('odd', 'even'), style: "#{"color:red;" if variant.deleted? }" } + %td.no-border + %span.handle + %td= variant.full_name + %td.align-center= variant.display_price.to_html + %td.align-center= variant.sku + %td.actions + = link_to_edit(variant, no_text: true) unless variant.deleted? + = link_to_delete(variant, no_text: true) unless variant.deleted? - if @product.empty_option_values? %p.first_add_option_types.no-objects-found