Sha256: 692f4dad118cf91cc0b052a58df7e6a46bf0250bdad3a0d5e8779363b44a5c29

Contents?: true

Size: 784 Bytes

Versions: 25

Compression:

Stored size: 784 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A concern with the components needed when you want a model to have a category.
  module HasCategory
    extend ActiveSupport::Concern

    included do
      has_one :categorization, as: :categorizable
      has_one :category, through: :categorization

      validate :category_belongs_to_organization

      def previous_category
        return if categorization.versions.count <= 1

        Decidim::Category.find_by(id: categorization.versions.last.reify.decidim_category_id)
      end

      private

      def category_belongs_to_organization
        return unless category

        errors.add(:category, :invalid) unless component.categories.exists?(id: category.id)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/has_category.rb
decidim-core-0.26.9 lib/decidim/has_category.rb
decidim-core-0.26.8 lib/decidim/has_category.rb
decidim-core-0.26.7 lib/decidim/has_category.rb
decidim-core-0.26.5 lib/decidim/has_category.rb
decidim-core-0.26.4 lib/decidim/has_category.rb
decidim-core-0.26.3 lib/decidim/has_category.rb
decidim-core-0.26.2 lib/decidim/has_category.rb
decidim-core-0.26.1 lib/decidim/has_category.rb
decidim-core-0.26.0 lib/decidim/has_category.rb
decidim-core-0.26.0.rc2 lib/decidim/has_category.rb
decidim-core-0.26.0.rc1 lib/decidim/has_category.rb
decidim-core-0.25.2 lib/decidim/has_category.rb
decidim-core-0.25.1 lib/decidim/has_category.rb
decidim-core-0.25.0 lib/decidim/has_category.rb
decidim-core-0.25.0.rc4 lib/decidim/has_category.rb
decidim-core-0.25.0.rc3 lib/decidim/has_category.rb
decidim-core-0.25.0.rc2 lib/decidim/has_category.rb
decidim-core-0.25.0.rc1 lib/decidim/has_category.rb
decidim-core-0.24.3 lib/decidim/has_category.rb