Sha256: 3e17f76980dfac8d7f48da88df1e50f7c132789b467171b4203a5ea2eb487e1a

Contents?: true

Size: 1.54 KB

Versions: 69

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  module Assemblies
    # A class used to find the Assemblies that the given user has
    # the specific role privilege.
    class AssembliesWithUserRole < Rectify::Query
      # Syntactic sugar to initialize the class and return the queried objects.
      #
      # user - a User that needs to find which assemblies can manage
      # role - (optional) a Symbol to specify the role privilege
      def self.for(user, role = :any)
        new(user, role).query
      end

      # Initializes the class.
      #
      # user - a User that needs to find which assemblies can manage
      # role - (optional) a Symbol to specify the role privilege
      def initialize(user, role = :any)
        @user = user
        @role = role
      end

      # Finds the Assemblies that the given user has role privileges.
      # If the special role ':any' is provided it returns all assemblies where
      # the user has some kind of role privilege.
      #
      # Returns an ActiveRecord::Relation.
      def query
        # Admin users have all role privileges for all organization assemblies
        return Assemblies::OrganizationAssemblies.new(user.organization).query if user.admin?

        Assembly.where(id: assembly_ids)
      end

      private

      attr_reader :user, :role

      def assembly_ids
        user_roles = AssemblyUserRole.where(user: user) if role == :any
        user_roles = AssemblyUserRole.where(user: user, role: role) if role != :any
        user_roles.pluck(:decidim_assembly_id)
      end
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
decidim-assemblies-0.17.2 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.18.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.17.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.16.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.17.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.16.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.15.2 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.15.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.15.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.14.4 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.14.3 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.14.2 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.14.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.13.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.12.2 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.13.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.12.1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.13.0.pre1 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.12.0 app/queries/decidim/assemblies/assemblies_with_user_role.rb
decidim-assemblies-0.11.2 app/queries/decidim/assemblies/assemblies_with_user_role.rb