Requested changes

- 2 new methods for reading either current/desired on hand/on demand
  depending on variant state. Goal is to get rid of send method in View
- referring in on_hand/on_demand is in fact irrelevant. In the piece of
  code, only desired on_hand/on_demand can be called as we are only in
  new variant (non persisted) mode
- View does not use send method anymore, replaced by current_or_desired
- refactor of the spec -> 2 examples in one to get more speed.
This commit is contained in:
cyrillefr
2024-06-28 16:37:59 +02:00
parent 1d3906d431
commit 0aecb6873a
3 changed files with 23 additions and 4 deletions

View File

@@ -67,6 +67,18 @@ module VariantStock
end
end
def on_demand_desired_or_current
return on_demand_desired if new_record?
on_demand
end
def on_hand_desired_or_current
return on_hand_desired if new_record?
on_hand
end
# Moving Spree::Stock::Quantifier.can_supply? to the variant enables us
# to override this behaviour for variant overrides
# We can have this responsibility here in the variant because there is

View File

@@ -131,7 +131,11 @@ module Sets
return variant if variant.errors.present?
begin
create_stock_for_variant(variant, on_demand, on_hand)
if on_hand || on_demand
create_stock_for_variant(variant, on_demand, on_hand)
else
create_stock_for_variant_from_desired(variant)
end
rescue StandardError => e
notify_bugsnag(e, product, variant, variant_attributes)
raise e
@@ -156,8 +160,11 @@ module Sets
def create_stock_for_variant(variant, on_demand, on_hand)
variant.on_demand = on_demand if on_demand.present?
variant.on_demand = variant.on_demand_desired if variant.on_demand_desired.present?
variant.on_hand = on_hand.to_i if on_hand.present?
end
def create_stock_for_variant_from_desired(variant)
variant.on_demand = variant.on_demand_desired if variant.on_demand_desired.present?
variant.on_hand = variant.on_hand_desired.to_i if variant.on_hand_desired.present?
end
end

View File

@@ -38,10 +38,10 @@
= error_message_on variant, :price
%td.col-on_hand.field.popout{'data-controller': "popout"}
%button.popout__button{'data-popout-target': "button", 'aria-label': t('admin.products_page.columns.on_hand')}
= variant.send(method_on_demand) ? t(:on_demand) : variant.send(method_on_hand)
= variant.on_demand_desired_or_current ? t(:on_demand) : variant.on_hand_desired_or_current
%div.popout__container{ style: 'display: none;', 'data-controller': 'toggle-control', 'data-popout-target': "dialog" }
.field
= f.number_field method_on_hand, min: 0, 'aria-label': t('admin.products_page.columns.on_hand'), 'data-toggle-control-target': 'control', disabled: f.object.send(method_on_demand)
= f.number_field method_on_hand, min: 0, 'aria-label': t('admin.products_page.columns.on_hand'), 'data-toggle-control-target': 'control', disabled: f.object.on_demand_desired_or_current
= error_message_on variant, method_on_hand
.field.checkbox
= f.label method_on_demand do