Sha256: 69ac73b05b2bf62c7c845c79eeb841fa9eb254ed8a2ad7f65ac6a24da07bf502

Contents?: true

Size: 1.02 KB

Versions: 25

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Decidim
  module UserGroups
    # 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 ManageableUserGroups < Decidim::Query
      # Syntactic sugar to initialize the class and return the queried objects.
      #
      # user - a User that needs to find which groups can manage
      def self.for(user)
        new(user).query
      end

      # Initializes the class.
      #
      # user - a User that needs to find which groups can manage
      def initialize(user)
        @user = user
      end

      # Finds the UserGroups where the user has a role of `:admin` or
      # `:creator`.
      #
      # Returns an ActiveRecord::Relation.
      def query
        user
          .user_groups
          .includes(:memberships)
          .where(decidim_user_group_memberships: { role: %w(admin creator) })
      end

      private

      attr_reader :user
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.27.2 app/queries/decidim/user_groups/manageable_user_groups.rb
decidim-core-0.27.1 app/queries/decidim/user_groups/manageable_user_groups.rb
decidim-core-0.27.0 app/queries/decidim/user_groups/manageable_user_groups.rb
decidim-core-0.27.0.rc2 app/queries/decidim/user_groups/manageable_user_groups.rb
decidim-core-0.27.0.rc1 app/queries/decidim/user_groups/manageable_user_groups.rb