Sha256: f89543368e7df483b2ada3ca6b4b9ebb27fe576963133e4203b47925fac3b13b

Contents?: true

Size: 1.62 KB

Versions: 24

Compression:

Stored size: 1.62 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)
        users = filter_by_state(users)
        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

24 entries across 24 versions & 1 rubygems

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