Files
openfoodnetwork/app/models/concerns/stock_settings_override_validation.rb
2018-12-09 23:29:36 +08:00

42 lines
1.5 KiB
Ruby

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