123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
- class SearchController < ApplicationController
- prepend_before_action :authentication_check
- # GET|POST /api/v1/search
- # GET|POST /api/v1/search/:objects
- def search_generic
- # get params
- query = params[:query]
- if query.respond_to?(:permit!)
- query = query.permit!.to_h
- end
- limit = params[:limit] || 10
- # convert objects string into array of class names
- # e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
- objects = if params[:objects]
- params[:objects].split('-').map(&:camelize)
- else
- Setting.get('models_searchable')
- end
- assets = {}
- result = []
- execute_service(
- SearchService,
- term: query,
- objects: objects.map(&:constantize),
- options: { limit: limit, ids: params[:ids] },
- ).each do |item|
- assets = item.assets(assets)
- result << {
- type: item.class.to_app_model.to_s,
- id: item[:id],
- }
- end
- render json: {
- assets: assets,
- result: result,
- }
- end
- end
|