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
# 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
    define_participatory_process_abilities if current_participatory_process && can_manage_process?(current_participatory_process)
  end
end

Instance Method Details

#can_manage_process?(process) ⇒ Boolean

Whether the user can manage the given process or not.

Returns:

  • (Boolean)


50
51
52
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 50

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

#current_participatory_processObject



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

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

#define_abilitiesObject

Grant access to admin panel by default.



21
22
23
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 21

def define_abilities
  can :read, :admin_dashboard
end

#define_participatory_process_abilitiesObject



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

def define_participatory_process_abilities; end

#has_manageable_processes?Boolean

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 45

def has_manageable_processes?
  participatory_processes_with_role_privileges.any?
end

#not_admin?Boolean

Whether the user is an admin or not.

Returns:

  • (Boolean)


34
35
36
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 34

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.



40
41
42
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 40

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.



29
30
31
# File 'lib/decidim/abilities/participatory_process_role_ability.rb', line 29

def role
  raise "Needs implementation"
end