Sha256: 59fb3dd268b043210de9980a267fa6bb9c2a508433e9a20ea667485027f8c971

Contents?: true

Size: 1.39 KB

Versions: 18

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Decidim
  # Categories serve as a taxonomy for components to use for while in the
  # context of a participatory process.
  class Category < ApplicationRecord
    belongs_to :participatory_space, foreign_key: "decidim_participatory_space_id", foreign_type: "decidim_participatory_space_type", polymorphic: true
    has_many :subcategories, foreign_key: "parent_id", class_name: "Decidim::Category", dependent: :destroy, inverse_of: :parent
    belongs_to :parent, class_name: "Decidim::Category", foreign_key: "parent_id", inverse_of: :subcategories, optional: true
    has_many :categorizations, foreign_key: "decidim_category_id", class_name: "Decidim::Categorization", dependent: :destroy

    validate :forbid_deep_nesting
    before_validation :subcategories_have_same_participatory_space

    # Scope to return only the first-class categories, that is, those that are
    # not subcategories.
    #
    # Returns an ActiveRecord::Relation.
    def self.first_class
      where(parent_id: nil)
    end

    def unused?
      categorizations.empty?
    end

    private

    def forbid_deep_nesting
      return unless parent
      return unless parent.parent.present?

      errors.add(:parent_id, :nesting_too_deep)
    end

    def subcategories_have_same_participatory_space
      return unless parent
      self.participatory_space = parent.participatory_space
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
decidim-core-0.6.8 app/models/decidim/category.rb
decidim-0.6.8 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.7 app/models/decidim/category.rb
decidim-0.6.7 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.6 app/models/decidim/category.rb
decidim-0.6.6 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.5 app/models/decidim/category.rb
decidim-0.6.5 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.4 app/models/decidim/category.rb
decidim-0.6.4 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.3 app/models/decidim/category.rb
decidim-0.6.3 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.2 app/models/decidim/category.rb
decidim-0.6.2 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.1 app/models/decidim/category.rb
decidim-0.6.1 decidim-core/app/models/decidim/category.rb
decidim-core-0.6.0 app/models/decidim/category.rb
decidim-0.6.0 decidim-core/app/models/decidim/category.rb