Class: Decidim::Abilities::ParticipatoryProcessRoleAbility

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
lib/decidim/abilities/participatory_process_role_ability.rb

Overview

Base class used for any participatory process role ability.

Instance Method Summary collapse

Constructor Details

#initialize(user, context) ⇒ ParticipatoryProcessRoleAbility

Returns a new instance of ParticipatoryProcessRoleAbility.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 9

def initialize(user, context)
  @user = user
  @context = context

  # Define abilities if the user is not an admin and has at least one process to manage.
  if not_admin? && has_manageable_processes?
    define_abilities

    if current_participatory_process && can_manage_process?(current_participatory_process)
      define_participatory_process_abilities
    end
  end
end

Instance Method Details

#can_manage_process?(process) ⇒ Boolean

Whether the user can manage the given process or not.

Returns:

  • (Boolean)


53
54
55
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 53

def can_manage_process?(process)
  participatory_processes_with_role_privileges.include? process
end

#current_participatory_processObject



57
58
59
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 57

def current_participatory_process
  @current_participatory_process ||= @context[:current_participatory_process]
end

#define_abilitiesObject

Grant access to admin panel by default.



24
25
26
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 24

def define_abilities
  can :read, :admin_dashboard
end

#define_participatory_process_abilitiesObject



28
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 28

def define_participatory_process_abilities; end

#has_manageable_processes?Boolean

Whether the user has at least one process to manage or not.

Returns:

  • (Boolean)


48
49
50
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 48

def has_manageable_processes?
  participatory_processes_with_role_privileges.any?
end

#not_admin?Boolean

Whether the user is an admin or not.

Returns:

  • (Boolean)


37
38
39
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 37

def not_admin?
  @user && !@user.admin?
end

#participatory_processes_with_role_privilegesObject

Returns a collection of Participatory processes where the given user has the specific role privilege.



43
44
45
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 43

def participatory_processes_with_role_privileges
  @participatory_processes ||= Decidim::ParticipatoryProcessesWithUserRole.for(@user, role)
end

#roleObject

Abstract: A subclass must define this method returning a valid role. See ParticipatoryProcessUserRoles::ROLES for more information.



32
33
34
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 32

def role
  raise "Needs implementation"
end