Sha256: 3747337e70e77b6549e56edafa7d1b46f2240ec67d00555cbbcc435a9fd95050

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Decidim
  #
  # Utilities for models that can act as participatory spaces
  #
  module Participable
    extend ActiveSupport::Concern

    # rubocop:disable Metrics/BlockLength
    included do
      def demodulized_name
        self.class.name.demodulize
      end

      delegate :foreign_key, to: :demodulized_name

      def module_name
        "Decidim::#{demodulized_name.pluralize}"
      end

      def admin_module_name
        "#{module_name}::Admin"
      end

      def underscored_name
        demodulized_name.underscore
      end

      def mounted_engine
        "decidim_#{underscored_name.pluralize}"
      end

      def mounted_admin_engine
        "decidim_admin_#{underscored_name.pluralize}"
      end

      def mounted_params
        {
          host: organization.host,
          "#{underscored_name}_slug".to_sym => slug
        }
      end

      def admin_extension_module
        "#{admin_module_name}::#{demodulized_name}Context".constantize
      end

      def admins_query
        "#{admin_module_name}::AdminUsers".constantize
      end

      def admins
        admins_query.for(self)
      end

      def allows_steps?
        respond_to?(:steps)
      end

      def has_steps?
        allows_steps? && steps.any?
      end

      def manifest
        self.class.participatory_space_manifest
      end
    end
    # rubocop:enable Metrics/BlockLength

    class_methods do
      def slug_format
        /\A[a-zA-Z]+[a-zA-Z0-9\-]+\z/
      end

      def participatory_space_manifest
        Decidim.find_participatory_space_manifest(name.demodulize.underscore.pluralize)
      end

      # Public: Adds a sane default way to retrieve public spaces. Please, overwrite
      # this from your model class in case this is not correct for your model.
      #
      # Returns an `ActiveRecord::Association`.
      def public_spaces
        published
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.9.3 lib/decidim/participable.rb
decidim-core-0.9.2 lib/decidim/participable.rb
decidim-core-0.9.1 lib/decidim/participable.rb
decidim-core-0.9.0 lib/decidim/participable.rb