Sha256: c302aeedcd209613d56ec7da64c4872eb180233ca90472f4e1b6cf33f79299e7

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Thredded
  class AutocompleteUsersController < Thredded::ApplicationController
    MAX_RESULTS = 20

    def index
      authorize_creating PrivateTopicForm.new(user: thredded_current_user).private_topic
      users = params.key?(:q) ? users_by_prefix : users_by_ids
      render json: {
        results: users.map do |user|
          { id:         user.id,
            name:       user.send(Thredded.user_name_column),
            avatar_url: Thredded.avatar_url.call(user) }
        end
      }
    end

    private

    def users_by_prefix
      query = params[:q].to_s.strip
      if query.length >= Thredded.autocomplete_min_length
        DbTextSearch::CaseInsensitive.new(users_scope, Thredded.user_name_column).prefix(query)
          .where.not(id: thredded_current_user.id)
          .limit(MAX_RESULTS)
      else
        []
      end
    end

    # This method is used by select2 do fetch users by ids, e.g. in case of a browser-prefilled field.
    def users_by_ids
      ids = params[:ids].to_s.split(',')
      if ids.present?
        users_scope.where(id: ids)
      else
        []
      end
    end

    def users_scope
      thredded_current_user.thredded_can_message_users
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thredded-0.12.4 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.12.3 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.12.2 app/controllers/thredded/autocomplete_users_controller.rb