From 9194a711eab628437ea27171442dde98ccb2afb7 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 17 Mar 2019 23:19:42 +0000 Subject: [PATCH 1/4] De-deface product tabs We do not include stock management from v2 because we will use the variants edit page to manage on_hand and on_demand --- .../add_distributions.html.haml.deface | 5 --- .../add_group_buy.html.haml.deface | 5 --- .../_product_tabs/add_seo.html.haml.deface | 5 --- .../admin/shared/_product_tabs.html.haml | 36 +++++++++++++++++++ 4 files changed, 36 insertions(+), 15 deletions(-) delete mode 100644 app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface delete mode 100644 app/overrides/spree/admin/shared/_product_tabs/add_group_buy.html.haml.deface delete mode 100644 app/overrides/spree/admin/shared/_product_tabs/add_seo.html.haml.deface create mode 100644 app/views/spree/admin/shared/_product_tabs.html.haml diff --git a/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface b/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface deleted file mode 100644 index ef4d2260c1..0000000000 --- a/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface +++ /dev/null @@ -1,5 +0,0 @@ -/ insert_bottom "[data-hook='admin_product_tabs']" - -- klass = current == 'Product Distributions' ? 'active' : '' -%li{:class => klass} - = link_to_with_icon 'icon-tasks', t('admin.products.product_distributions'), product_distributions_admin_product_url(@product) \ No newline at end of file diff --git a/app/overrides/spree/admin/shared/_product_tabs/add_group_buy.html.haml.deface b/app/overrides/spree/admin/shared/_product_tabs/add_group_buy.html.haml.deface deleted file mode 100644 index 5b02383b13..0000000000 --- a/app/overrides/spree/admin/shared/_product_tabs/add_group_buy.html.haml.deface +++ /dev/null @@ -1,5 +0,0 @@ -/ insert_bottom "[data-hook='admin_product_tabs']" - -- klass = current == 'Group Buy Options' ? 'active' : '' -%li{:class => klass} - = link_to_with_icon 'icon-tasks', t('admin.products.group_buy_options'), group_buy_options_admin_product_url(@product) diff --git a/app/overrides/spree/admin/shared/_product_tabs/add_seo.html.haml.deface b/app/overrides/spree/admin/shared/_product_tabs/add_seo.html.haml.deface deleted file mode 100644 index 84ad95e062..0000000000 --- a/app/overrides/spree/admin/shared/_product_tabs/add_seo.html.haml.deface +++ /dev/null @@ -1,5 +0,0 @@ -/ insert_bottom "[data-hook='admin_product_tabs']" - -- klass = current == t(:Search) ? 'active' : '' -%li{:class => klass} - = link_to_with_icon 'icon-tasks', t(:Search), seo_admin_product_url(@product) diff --git a/app/views/spree/admin/shared/_product_tabs.html.haml b/app/views/spree/admin/shared/_product_tabs.html.haml new file mode 100644 index 0000000000..37762df346 --- /dev/null +++ b/app/views/spree/admin/shared/_product_tabs.html.haml @@ -0,0 +1,36 @@ += content_for :page_title do + = Spree.t(:editing_product) + = "\"#{@product.name}\"" + += content_for :sidebar_title do + %span.sku + = @product.sku + += content_for :sidebar do + %nav.menu + %ul + - if can?(:admin, Spree::Product) + - klass = current == 'Product Details' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-edit', Spree.t(:product_details), edit_admin_product_url(@product) + - if can?(:admin, Spree::Image) + - klass = current == 'Images' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-picture', Spree.t(:images), admin_product_images_url(@product) + - if can?(:admin, Spree::Variant) + - klass = current == 'Variants' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-th-large', Spree.t(:variants), admin_product_variants_url(@product) + - if can?(:admin, Spree::ProductProperty) + - klass = current == 'Product Properties' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-tasks', Spree.t(:product_properties), admin_product_product_properties_url(@product) + - klass = current == 'Product Distributions' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-tasks', t('admin.products.product_distributions'), product_distributions_admin_product_url(@product) + - klass = current == 'Group Buy Options' ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-tasks', t('admin.products.group_buy_options'), group_buy_options_admin_product_url(@product) + - klass = current == t(:Search) ? 'active' : '' + %li{:class => klass} + = link_to_with_icon 'icon-tasks', t(:Search), seo_admin_product_url(@product) From 21a10d0206885357718b1f57e66d87e6dd007468 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 17 Mar 2019 23:17:15 +0000 Subject: [PATCH 2/4] Add on_hand and on_demand back to variants form (both variant create and edit pagei), this is where they used to be in spree v1 The admin variants controller create action is adapted (same as in the admin ProductsController). This is necessary because variant.on_hand and on_demand cannot be mass assigned on creation in v2, see VariantStock for more details. For now we are hiding the new stock management page that comes in spree v2 as we don't need the complexity added by the management of stock per stock location (only one stock location in ofn v2 for now) --- .../admin/variants_controller_decorator.rb | 8 ++++++ .../add_stock_management.html.haml.deface | 11 ++++++++ .../_form/on_demand_script.html.haml.deface | 25 +++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface diff --git a/app/controllers/spree/admin/variants_controller_decorator.rb b/app/controllers/spree/admin/variants_controller_decorator.rb index cabc7e5eec..039cb5a849 100644 --- a/app/controllers/spree/admin/variants_controller_decorator.rb +++ b/app/controllers/spree/admin/variants_controller_decorator.rb @@ -3,6 +3,14 @@ require 'open_food_network/scope_variants_for_search' Spree::Admin::VariantsController.class_eval do helper 'spree/products' + def create + on_demand = params[:variant].delete(:on_demand) + on_hand = params[:variant].delete(:on_hand) + super + @object.on_demand = on_demand if @object.present? + @object.on_hand = on_hand.to_i if @object.present? + end + def search scoper = OpenFoodNetwork::ScopeVariantsForSearch.new(params) @variants = scoper.search diff --git a/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface b/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface new file mode 100644 index 0000000000..119a565ebe --- /dev/null +++ b/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface @@ -0,0 +1,11 @@ +/ insert_bottom "[data-hook='admin_variant_form_fields']" + +- if Spree::Config[:track_inventory_levels] + .field.checkbox{ "data-hook" => "on_hand" } + %label + = f.check_box :on_demand + = t(:on_demand) + .field{ "data-hook" => "on_hand" } + = f.label :on_hand, t(:on_hand) + .fullwidth + = f.text_field :on_hand diff --git a/app/overrides/spree/admin/variants/_form/on_demand_script.html.haml.deface b/app/overrides/spree/admin/variants/_form/on_demand_script.html.haml.deface index 6bd45a0d49..fe3448e332 100644 --- a/app/overrides/spree/admin/variants/_form/on_demand_script.html.haml.deface +++ b/app/overrides/spree/admin/variants/_form/on_demand_script.html.haml.deface @@ -6,23 +6,28 @@ var on_demand = $('input#variant_on_demand'); var on_hand = $('input#variant_on_hand'); - on_hand.attr('disabled', on_demand.attr('checked')); + disableOnHandIfOnDemand = function() { + on_demand_checked = on_demand.attr('checked') + if ( on_demand_checked == undefined ) + on_demand_checked = false; + + on_hand.attr('disabled', on_demand_checked); + if(on_demand_checked) { + on_hand.attr('data-stock', on_hand.val()); + on_hand.val("Infinity"); + } + } + + disableOnHandIfOnDemand(); on_demand.change(function(){ - - on_hand.attr('disabled', this.checked); - - if(this.checked) { - on_hand.attr('data-stock', on_hand.val()); - on_hand.val("Infinity"); - } else { + disableOnHandIfOnDemand(); + if(!this.checked) { if(on_hand.attr('data-stock') !== undefined) { on_hand.val(on_hand.attr('data-stock')); } else { on_hand.val("0"); } } - }); - }); From 78ea16c6847d86a90a1765283f599f989827b792 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 22 Mar 2019 21:30:25 +0000 Subject: [PATCH 3/4] Remove unnecessary data-hooks from stock management deface override --- .../variants/_form/add_stock_management.html.haml.deface | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface b/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface index 119a565ebe..e899da406c 100644 --- a/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface +++ b/app/overrides/spree/admin/variants/_form/add_stock_management.html.haml.deface @@ -1,11 +1,11 @@ / insert_bottom "[data-hook='admin_variant_form_fields']" - if Spree::Config[:track_inventory_levels] - .field.checkbox{ "data-hook" => "on_hand" } + .field.checkbox %label = f.check_box :on_demand = t(:on_demand) - .field{ "data-hook" => "on_hand" } + .field = f.label :on_hand, t(:on_hand) .fullwidth = f.text_field :on_hand From a8c0eef3d3adf2ea4d64727c1c746967e6285357 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 22 Mar 2019 23:23:10 +0000 Subject: [PATCH 4/4] Improve admin variants controller create action, it doesn't try to set on_hand and on_demand if there are previous errors in the variant --- .../spree/admin/variants_controller_decorator.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/admin/variants_controller_decorator.rb b/app/controllers/spree/admin/variants_controller_decorator.rb index 039cb5a849..c422ae8b44 100644 --- a/app/controllers/spree/admin/variants_controller_decorator.rb +++ b/app/controllers/spree/admin/variants_controller_decorator.rb @@ -6,9 +6,13 @@ Spree::Admin::VariantsController.class_eval do def create on_demand = params[:variant].delete(:on_demand) on_hand = params[:variant].delete(:on_hand) + super - @object.on_demand = on_demand if @object.present? - @object.on_hand = on_hand.to_i if @object.present? + + if @object.present? && @object.valid? + @object.on_demand = on_demand if on_demand.present? + @object.on_hand = on_hand.to_i if on_hand.present? + end end def search