Sha256: 05cb402f2b11816df723bc2875112e1c918368efad8473ef61d5aa849c132167

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Abilities
      module Admin
        # Defines the abilities related to user able to administer initiative types.
        # Intended to be used with `cancancan`.
        class InitiativeTypeAbility
          include CanCan::Ability

          attr_reader :user, :context

          def initialize(user, context)
            return unless user&.admin?

            @user = user
            @context = context

            can :manage, InitiativesType
            cannot :destroy, InitiativesType
            can :destroy, InitiativesType do |initiative_type|
              result = true

              initiative_type.scopes.each do |s|
                result &&= s.initiatives.empty?
              end

              result
            end

            can :manage, Decidim::InitiativesTypeScope
            cannot :destroy, Decidim::InitiativesTypeScope
            can :destroy, Decidim::InitiativesTypeScope do |scope|
              scope.initiatives.empty?
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-initiatives-0.11.2 app/models/decidim/initiatives/abilities/admin/initiative_type_ability.rb
decidim-initiatives-0.11.1 app/models/decidim/initiatives/abilities/admin/initiative_type_ability.rb
decidim-initiatives-0.11.0.pre1 app/models/decidim/initiatives/abilities/admin/initiative_type_ability.rb
decidim-initiatives-0.9.1 app/models/decidim/initiatives/abilities/admin/initiative_type_ability.rb