selectors_controller.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SelectorsController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. before_action :ensure_object_klass_has_selector!
  5. # POST /api/v1/:object/selector
  6. # POST /api/v1/tickets/selector
  7. # POST /api/v1/users/selector
  8. # POST /api/v1/organizations/selector
  9. def preview
  10. object_count, objects = object_klass.selectors(params[:condition], limit: 6, execution_time: true)
  11. assets = {}
  12. object_ids = []
  13. objects&.each do |object|
  14. object_ids.push object.id
  15. assets = object.assets(assets)
  16. end
  17. # return result
  18. render json: {
  19. object_ids: object_ids,
  20. object_count: object_count || 0,
  21. assets: assets,
  22. }
  23. end
  24. private
  25. def object_klass
  26. @object_klass ||= case params[:object]
  27. when 'organizations'
  28. Organization
  29. when 'tickets'
  30. Ticket
  31. when 'users'
  32. User
  33. end
  34. end
  35. def ensure_object_klass_has_selector!
  36. return if object_klass.present?
  37. raise Exceptions::UnprocessableEntity, __('Given object does not support selector')
  38. end
  39. end