collector.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Translation::Search::Collector
  3. include Mixin::RequiredSubPaths
  4. attr_reader :locale, :query, :like_query, :like_operator, :limit, :mode
  5. def self.collector_suggestions
  6. @collector_suggestions ||= descendants.select { |collector| collector.type == :suggestion }
  7. end
  8. def self.collector_translations
  9. @collector_translations ||= descendants.select { |collector| collector.type == :translation }
  10. end
  11. def self.type
  12. :suggestion
  13. end
  14. def initialize(locale:, query:, limit:, mode:)
  15. super()
  16. @locale = locale
  17. @query = query
  18. @limit = limit
  19. @mode = mode
  20. return if mode == :list
  21. @like_query = "%#{SqlHelper.quote_like(query)}%"
  22. @like_operator = Rails.application.config.db_like
  23. end
  24. def result
  25. @result ||= begin
  26. mode == :list ? list : search
  27. end
  28. end
  29. def list
  30. raise NotImplementedError if self.class.type != :suggestion
  31. suggestions(list_sources)
  32. end
  33. def search
  34. raise NotImplementedError if self.class.type != :suggestion
  35. suggestions(search_sources)
  36. end
  37. def count
  38. raise NotImplementedError
  39. end
  40. private
  41. def list_sources
  42. raise NotImplementedError
  43. end
  44. def search_sources
  45. raise NotImplementedError
  46. end
  47. def suggestions(sources)
  48. result = []
  49. sources.each do |source|
  50. next if Translation.find_source(locale, source)
  51. result.push({
  52. source:,
  53. target: '',
  54. target_initial: '',
  55. id: SecureRandom.uuid,
  56. })
  57. end
  58. result
  59. end
  60. end