mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
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
25 lines
656 B
Ruby
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
|