Sha256: 9d77c00ab702616532f3f77f586b8e338a4bdf7ce1873c7f0c90b967410ccadf

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  module Accountability
    # This class handles search and filtering of results. Needs a
    # `current_feature` param with a `Decidim::Feature` in order to
    # find the results.
    class ResultSearch < ResourceSearch
      # Public: Initializes the service.
      # feature     - A Decidim::Feature to get the results from.
      # page        - The page number to paginate the results.
      # per_page    - The number of proposals to return per page.
      def initialize(options = {})
        super(Result.all, options)
      end

      # Handle the search_text filter
      def search_search_text
        query
          .where(localized_search_text_in(:title), text: "%#{search_text}%")
          .or(query.where(localized_search_text_in(:description), text: "%#{search_text}%"))
      end

      private

      # Internal: builds the needed query to search for a text in the organization's
      # available locales. Note that it is intended to be used as follows:
      #
      # Example:
      #   Resource.where(localized_search_text_for(:title, text: "my_query"))
      #
      # The Hash with the `:text` key is required or it won't work.
      def localized_search_text_in(field)
        options[:organization].available_locales.map do |l|
          "#{field} ->> '#{l}' ILIKE :text"
        end.join(" OR ")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-accountability-0.1.2 app/services/decidim/accountability/result_search.rb
decidim-accountability-0.1.1 app/services/decidim/accountability/result_search.rb
decidim-accountability-0.1.0 app/services/decidim/accountability/result_search.rb