Sha256: e97821c7b146bf66ee0c524f96d747d2b9112e8fa613eaef8208aa381e578c70

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

module Workarea
  module GlobalE
    class CountryException
      include ApplicationDocument

      field :country, type: Country
      field :restricted, type: Boolean, default: false
      field :vat_rate, type: Float

      embedded_in :product, class_name: "Workarea::Catalog::Product"

      validates_presence_of :country
      validate :unique_country
      validate :restricted_or_vat_rate_presence

      private

        def unique_country
          return unless product.present? && country.present?

          country_exists = product.country_exceptions.any? do |country_exception|
            country_exception != self && country_exception.country == country
          end

          if country_exists
            errors.add(:base, I18n.t('workarea.country_exception.errors.country_must_be_unique'))
          end
        end

        def restricted_or_vat_rate_presence
          return if restricted.present? || vat_rate.present?

          errors.add(:base, I18n.t('workarea.country_exception.errors.restricted_or_vat_rate_required'))
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-global_e-1.3.0 app/models/workarea/global_e/country_exception.rb
workarea-global_e-1.2.1 app/models/workarea/global_e/country_exception.rb