Sha256: 570cb86ceb0b413dd5581169511be754093dcce0b52fd789d551ec8c33b52af2

Contents?: true

Size: 893 Bytes

Versions: 4

Compression:

Stored size: 893 Bytes

Contents

module Spree
  class Taxonomy < Spree::Base
    include Metadata

    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_save :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_change_to_name?
      return if name.to_s == root.name.to_s

      root.update(name: name)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_core-4.4.1 app/models/spree/taxonomy.rb
spree_core-4.4.0 app/models/spree/taxonomy.rb
spree_core-4.4.0.rc2 app/models/spree/taxonomy.rb
spree_core-4.4.0.rc1 app/models/spree/taxonomy.rb