Files
openfoodnetwork/app/models/spree/tax_category.rb
Maikel Linke 1364b878fe Add ApplicationRecord for customisations
Rails 5 introduced this new class to confine application-specific monkey
patches to our models only, and not leak into other libraries using
ActiveRecord::Base.

https://bigbinary.com/blog/application-record-in-rails-5
2021-04-15 15:59:03 +10:00

25 lines
656 B
Ruby

# frozen_string_literal: true
module Spree
class TaxCategory < ApplicationRecord
acts_as_paranoid
validates :name, presence: true, uniqueness: { scope: :deleted_at }
has_many :tax_rates, dependent: :destroy, inverse_of: :tax_category
before_save :set_default_category
def set_default_category
# set existing default tax category to false if this one has been marked as default
return unless is_default && tax_category = self.class.find_by(is_default: true)
return if tax_category == self
tax_category.update_columns(
is_default: false,
updated_at: Time.zone.now
)
end
end
end