Sha256: 2ecc82df0f42da120c439d5be69cdf49accad5d86d925bcdb481e044931a1e97

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  module Core
    # A GraphQL resolver to handle `user` queries
    class UserResolver
      #
      # - organization: Decidim::Organization scoping
      # - filters: hash of attr - value to filter results
      #
      def initialize(organization, filters = {})
        @organization = organization
        if filters.include? :wildcard
          filters.delete(:name)
          filters.delete(:nickname)
        end
        @filters = filters
      end

      def users
        resolve
      end

      private

      def resolve
        return @records if @records

        scope
        global_filter
        filter
        @records.limit(50)
      end

      def scope
        @records = Decidim::User
                   .where(organization: organization)
                   .confirmed
      end

      # Only key name attributes in Decidim::User will be applied
      def filter
        @filters.each do |key, value|
          next unless Decidim::User.column_names.include? key.to_s

          @records = @records.where("#{key} ilike ?", "%#{value}%")
        end
      end

      # Special search key ":wildcard"
      def global_filter
        return unless @filters.include? :wildcard

        term = "%#{@filters[:wildcard]}%"
        @records = @records.where("name ilike ? or nickname ilike ?", term, term)
      end

      attr_reader :organization, :filters
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/resolvers/decidim/core/user_resolver.rb
decidim-core-0.20.1 app/resolvers/decidim/core/user_resolver.rb
decidim-core-0.20.0 app/resolvers/decidim/core/user_resolver.rb