mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
Otherwise we would try to take stock from the producer stock level without respecting their on-demand settings. So from now on: If stock level or on_demand are set on the override then it's not using producer stock levels.
53 lines
2.2 KiB
Ruby
53 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Validates the combination of on_demand and count_on_hand values.
|
|
#
|
|
# `on_demand` can have three values: true, false or nil
|
|
# `count_on_hand` can either be: nil or a number
|
|
#
|
|
# This means that a variant override can be in six different stock states
|
|
# but only four of them are valid.
|
|
#
|
|
# | on_demand | count_on_hand | stock_overridden? | use_producer_stock_settings? | valid? |
|
|
# |-----------|---------------|-------------------|------------------------------|--------|
|
|
# | 1 | nil | true | false | true |
|
|
# | 0 | x | true | false | true |
|
|
# | nil | nil | false | true | true |
|
|
# | 1 | x | true | false | true |
|
|
# | 0 | nil | ? | ? | false |
|
|
# | nil | x | ? | ? | false |
|
|
#
|
|
# This module has one method for each invalid case.
|
|
module StockSettingsOverrideValidation
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
before_validation :require_compatible_on_demand_and_count_on_hand
|
|
end
|
|
|
|
def require_compatible_on_demand_and_count_on_hand
|
|
disallow_count_on_hand_if_using_producer_stock_settings
|
|
require_count_on_hand_if_limited_stock
|
|
end
|
|
|
|
def disallow_count_on_hand_if_using_producer_stock_settings
|
|
return unless on_demand.nil? && count_on_hand.present?
|
|
|
|
error_message = I18n.t("count_on_hand.using_producer_stock_settings_but_count_on_hand_set",
|
|
scope: i18n_scope_for_stock_settings_override_validation_error)
|
|
errors.add(:count_on_hand, error_message)
|
|
end
|
|
|
|
def require_count_on_hand_if_limited_stock
|
|
return unless on_demand == false && count_on_hand.blank?
|
|
|
|
error_message = I18n.t("count_on_hand.limited_stock_but_no_count_on_hand",
|
|
scope: i18n_scope_for_stock_settings_override_validation_error)
|
|
errors.add(:count_on_hand, error_message)
|
|
end
|
|
|
|
def i18n_scope_for_stock_settings_override_validation_error
|
|
"activerecord.errors.models.#{self.class.name.underscore}"
|
|
end
|
|
end
|