Sha256: d115aedc31f79f3b5b48d567f56013597d19210823630d97e2108e25144c6fcd

Contents?: true

Size: 737 Bytes

Versions: 7

Compression:

Stored size: 737 Bytes

Contents

# frozen_string_literal: true

module Spree
  class Taxonomy < Spree::Base
    acts_as_list

    validates :name, presence: true
    validates :name, uniqueness: true, if: -> { Spree::Config.extra_taxonomy_validations }

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

    after_save :set_name

    default_scope -> { order(position: :asc) }

    self.allowed_ransackable_attributes = %w[name]

    private

    def set_name
      if root
        root.update_columns(
          name: name,
          updated_at: Time.current
        )
      else
        self.root = Spree::Taxon.create!(taxonomy_id: id, name: name)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
solidus_core-3.4.6 app/models/spree/taxonomy.rb
solidus_core-3.4.5 app/models/spree/taxonomy.rb
solidus_core-3.4.4 app/models/spree/taxonomy.rb
solidus_core-3.4.3 app/models/spree/taxonomy.rb
solidus_core-3.4.2 app/models/spree/taxonomy.rb
solidus_core-3.4.1 app/models/spree/taxonomy.rb
solidus_core-3.4.0 app/models/spree/taxonomy.rb