Class: Decidim::UserGroups::ManageableUserGroups

Inherits:
Query
  • Object
show all
Defined in:
decidim-core/app/queries/decidim/user_groups/manageable_user_groups.rb

Overview

Use this class to find the user groups the given user can manage. In order to calculate this, we get those groups where the user has a role of creator or admin.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Query

#cached_query, #each, #eager?, #exists?, merge, #none?, #relation?, #|

Constructor Details

#initialize(user) ⇒ ManageableUserGroups

Initializes the class.

user - a User that needs to find which groups can manage



19
20
21
# File 'decidim-core/app/queries/decidim/user_groups/manageable_user_groups.rb', line 19

def initialize(user)
  @user = user
end

Class Method Details

.for(user) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

user - a User that needs to find which groups can manage



12
13
14
# File 'decidim-core/app/queries/decidim/user_groups/manageable_user_groups.rb', line 12

def self.for(user)
  new(user).query
end

Instance Method Details

#queryObject

Finds the UserGroups where the user has a role of ‘:admin` or `:creator`.

Returns an ActiveRecord::Relation.



27
28
29
30
31
32
# File 'decidim-core/app/queries/decidim/user_groups/manageable_user_groups.rb', line 27

def query
  user
    .user_groups
    .includes(:memberships)
    .where(decidim_user_group_memberships: { role: %w(admin creator) })
end