Sha256: cbd8ad541f4af53c8abf71319bdadcaecec73d273842caf053f59f7648e9c45e

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A class used to filter users by whitelisted scope or searches on their
    # name
    class UserFilter < Rectify::Query
      WHITELISTED_STATE_SCOPES = %w(
        officialized
        not_officialized
        managed
        not_managed
      ).freeze

      # Syntactic sugar to initialize the class and return the queried objects.
      #
      # scope - the ActiveRecord::Relation of users to be filtered
      # name_query - query to filter user group names
      # state - evaluation state to be used as a filter
      def self.for(scope, name_query = nil, state = nil)
        new(scope, name_query, state).query
      end

      # Initializes the class.
      #
      # scope - the ActiveRecord::Relation of users to be filtered
      # name_query - query to filter user group names
      # state - users state, must be defined as a scope in the user model
      def initialize(scope, name_query = nil, state = nil)
        @scope = scope
        @name_query = name_query
        @state = state
      end

      # List the User groups by the diferents filters.
      def query
        users = scope
        users = filter_by_search(users)
        filter_by_state(users)
      end

      private

      attr_reader :name_query, :state, :scope

      def filter_by_search(users)
        return users if name_query.blank?

        users.where("LOWER(name) LIKE LOWER(?)", "%#{name_query}%")
      end

      def filter_by_state(users)
        return users unless WHITELISTED_STATE_SCOPES.include?(state)

        users.public_send(state)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-admin-0.26.10 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.9 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.8 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.7 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.5 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.4 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.3 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.2 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.1 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.0 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.0.rc2 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.26.0.rc1 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.2 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.1 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.0 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.0.rc4 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.0.rc3 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.0.rc2 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.25.0.rc1 app/queries/decidim/admin/user_filter.rb
decidim-admin-0.24.3 app/queries/decidim/admin/user_filter.rb