1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- class Service::Translation::Search < Service::Base
- attr_reader :locale, :query, :limit, :collector_mode
- def initialize(locale:, query:, limit: 150)
- super()
- @locale = locale
- @query = query
- @limit = limit
- @collector_mode = query.blank? || query.strip.empty? ? :list : :search
- end
- def execute
- items = []
- total_count = 0
- Service::Translation::Search::Collector.collector_suggestions.each do |collector_module|
- collector = collector_module.new(locale:, query:, limit:, mode: collector_mode)
-
- suggestions = collector.result.reject { |suggestion| items.pluck(:source).include?(suggestion[:source]) }
- items.concat(suggestions)
- total_count += suggestions.length
- end
-
- items.concat(translations[:items])
- {
- items: items.take(limit),
- total_count: total_count + translations[:total_count]
- }
- end
- private
- def translations
- @translations ||= begin
- items = []
- total_count = 0
- Service::Translation::Search::Collector.collector_translations.each do |collector_module|
- collector = collector_module.new(locale:, query:, limit:, mode: collector_mode)
- items.concat collector.result
- total_count += collector.count
- end
- {
- items: items,
- total_count: total_count
- }
- end
- end
- end
|