Sha256: 29f6cfadeb094b0aa7d30be744ba050849dab0b90cbdbf6d981a59e81de07b72

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  # A class used to find the ParticipatoryProcesses that the given user has
  # the specific role privilege.
  class ParticipatoryProcessesWithUserRole < Decidim::Query
    # Syntactic sugar to initialize the class and return the queried objects.
    #
    # user - a User that needs to find which processes 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 processes can manage
    # role - (optional) a Symbol to specify the role privilege
    def initialize(user, role = :any)
      @user = user
      @role = role
    end

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

      ParticipatoryProcess.where(id: process_ids)
    end

    private

    attr_reader :user, :role

    def process_ids
      user_roles = ParticipatoryProcessUserRole.where(user: user) if role == :any
      user_roles = ParticipatoryProcessUserRole.where(user: user, role: role) if role != :any
      user_roles.pluck(:decidim_participatory_process_id)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.8 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.7 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.6 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.5 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.4 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.3 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.2 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.1 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.0 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.0.rc2 app/queries/decidim/participatory_processes_with_user_role.rb
decidim-core-0.27.0.rc1 app/queries/decidim/participatory_processes_with_user_role.rb