Sha256: 98ffc6a7183dfc2756cac7dd43f1cb3d17cb5cf870fd120e4def1cc8fae218b8

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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

    def index
      authorize_creating Thredded::PrivateTopicForm.new(user: thredded_current_user).private_topic
      users = params.key?(:q) ? users_by_prefix : users_by_ids
      render json: {
        results: users.map { |user| user_to_autocomplete_result(user) }
      }
    end

    protected

    def user_to_autocomplete_result(user)
      {
        id: user.id,
        name: user.send(Thredded.user_name_column),
        display_name: user.send(Thredded.user_display_name_method),
        avatar_url: Thredded.avatar_url.call(user)
      }
    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

    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

10 entries across 10 versions & 2 rubygems

Version Path
thredded-0.16.0 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.15.5 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.15.4 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.15.3 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.15.2 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.15.1 app/controllers/thredded/autocomplete_users_controller.rb
threddedDANIEL-0.14.5 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.14.4 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.14.3 app/controllers/thredded/autocomplete_users_controller.rb
thredded-0.14.2 app/controllers/thredded/autocomplete_users_controller.rb