Sha256: 939e07c5f0c03d53b6308e1d198c00080d287dedbf89da46a280578d5839c4f4

Contents?: true

Size: 1.74 KB

Versions: 20

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Decidim
  # Scope types allows to use different types of scopes in participatory process
  # (municipalities, provinces, states, countries, etc.)
  class ScopeType < ApplicationRecord
    include Decidim::TranslatableResource
    include Traceable

    translatable_fields :name, :plural
    belongs_to :organization,
               foreign_key: "decidim_organization_id",
               class_name: "Decidim::Organization",
               inverse_of: :scope_types

    has_many :scopes, class_name: "Decidim::Scope", inverse_of: :scope_type, dependent: :nullify

    validates :name, presence: true

    before_destroy :detach_dynamic_associations

    def self.log_presenter_class_for(_log)
      Decidim::AdminLog::ScopeTypePresenter
    end

    private

    # This method detaches all records that may have association with the scope
    # type. This cannot be done directly using the `dependent` option in the
    # `has_many` relation in order to avoid tight coupling between the modules.
    #
    # This logic does not have to be applied to any classes that have been
    # defined as `has_many` associations within this model already as they are
    # already handled by the `dependent` option.
    def detach_dynamic_associations
      ActiveRecord::Base.descendants.each do |cls|
        next if cls.abstract_class? || !cls.name&.match?(/^Decidim::/)
        next if cls == self.class || cls == Decidim::Scope

        cls.reflect_on_all_associations(:belongs_to).each do |ref|
          next unless ref.options[:class_name] == self.class.name

          cls.where(ref.options[:foreign_key] => id).update_all(ref.options[:foreign_key] => nil) # rubocop:disable Rails/SkipsModelValidations
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/models/decidim/scope_type.rb
decidim-core-0.28.4 app/models/decidim/scope_type.rb
decidim-core-0.27.9 app/models/decidim/scope_type.rb
decidim-core-0.29.0 app/models/decidim/scope_type.rb
decidim-core-0.28.3 app/models/decidim/scope_type.rb
decidim-core-0.27.8 app/models/decidim/scope_type.rb
decidim-core-0.29.0.rc4 app/models/decidim/scope_type.rb
decidim-core-0.29.0.rc3 app/models/decidim/scope_type.rb
decidim-core-0.29.0.rc2 app/models/decidim/scope_type.rb
decidim-core-0.29.0.rc1 app/models/decidim/scope_type.rb
decidim-core-0.28.2 app/models/decidim/scope_type.rb
decidim-core-0.27.7 app/models/decidim/scope_type.rb
decidim-core-0.28.1 app/models/decidim/scope_type.rb
decidim-core-0.27.6 app/models/decidim/scope_type.rb
decidim-core-0.28.0 app/models/decidim/scope_type.rb
decidim-core-0.27.5 app/models/decidim/scope_type.rb
decidim-core-0.28.0.rc5 app/models/decidim/scope_type.rb
decidim-core-0.28.0.rc4 app/models/decidim/scope_type.rb
decidim-core-0.27.4 app/models/decidim/scope_type.rb
decidim-core-0.27.3 app/models/decidim/scope_type.rb