diff --git a/app/models/concerns/stock_settings_override_validation.rb b/app/models/concerns/stock_settings_override_validation.rb new file mode 100644 index 0000000000..b9e3624aec --- /dev/null +++ b/app/models/concerns/stock_settings_override_validation.rb @@ -0,0 +1,41 @@ +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 + disallow_count_on_hand_if_on_demand + 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 disallow_count_on_hand_if_on_demand + return unless on_demand? && count_on_hand.present? + + error_message = I18n.t("count_on_hand.on_demand_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 diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index 5527f29af7..dd3b22a5e0 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -1,5 +1,6 @@ class VariantOverride < ActiveRecord::Base extend Spree::LocalizedNumber + include StockSettingsOverrideValidation acts_as_taggable @@ -10,8 +11,6 @@ class VariantOverride < ActiveRecord::Base # Default stock can be nil, indicating stock should not be reset or zero, meaning reset to zero. Need to ensure this can be set by the user. validates :default_stock, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true - before_validation :require_compatible_on_demand_and_count_on_hand - after_save :refresh_products_cache_from_save after_destroy :refresh_products_cache_from_destroy @@ -106,38 +105,4 @@ class VariantOverride < ActiveRecord::Base def refresh_products_cache_from_destroy OpenFoodNetwork::ProductsCache.variant_override_destroyed self end - - def require_compatible_on_demand_and_count_on_hand - disallow_count_on_hand_if_using_producer_stock_settings - disallow_count_on_hand_if_on_demand - 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("using_producer_stock_settings_but_count_on_hand_set", - scope: [i18n_scope_for_error, "count_on_hand"]) - errors.add(:count_on_hand, error_message) - end - - def disallow_count_on_hand_if_on_demand - return unless on_demand? && count_on_hand.present? - - error_message = I18n.t("on_demand_but_count_on_hand_set", - scope: [i18n_scope_for_error, "count_on_hand"]) - 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("limited_stock_but_no_count_on_hand", - scope: [i18n_scope_for_error, "count_on_hand"]) - errors.add(:count_on_hand, error_message) - end - - def i18n_scope_for_error - "activerecord.errors.models.variant_override" - end end