Sha256: a2ac0cb437f7cad64e30a2721711e3f5dc01a7030e7ca0cba8a3b623e0209fd0

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

module Spree
  class Taxonomy < Spree::Base
    include Spree::TranslatableResource
    include Spree::Metadata
    if defined?(Spree::Webhooks::HasWebhooks)
      include Spree::Webhooks::HasWebhooks
    end

    TRANSLATABLE_FIELDS = %i[name].freeze
    translates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)

    acts_as_list

    validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true, scope: :store_id }
    validates :store, presence: true

    has_many :taxons, inverse_of: :taxonomy
    has_one :root, -> { where parent_id: nil }, class_name: 'Spree::Taxon', dependent: :destroy
    belongs_to :store, class_name: 'Spree::Store'

    after_create :set_root
    after_update :set_root_taxon_name

    default_scope { order("#{table_name}.position, #{table_name}.created_at") }

    self.whitelisted_ransackable_associations = %w[root]

    private

    def set_root
      self.root ||= Taxon.create!(taxonomy_id: id, name: name)
    end

    def set_root_taxon_name
      return unless saved_changes.key?(:name)
      return if name.to_s == root.name.to_s

      root.update(name: name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_core-4.9.0 app/models/spree/taxonomy.rb
spree_core-4.8.3 app/models/spree/taxonomy.rb
spree_core-4.8.2 app/models/spree/taxonomy.rb