Sha256: 26c7d436f8e894e2719811f959502802a161384e11553104a868c6d062214f73

Contents?: true

Size: 1.78 KB

Versions: 54

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Decidim
  module Budgets
    # This class handles search and filtering of projects. Needs a
    # `current_feature` param with a `Decidim::Feature` in order to
    # find the projects.
    class ProjectSearch < ResourceSearch
      # Public: Initializes the service.
      # feature     - A Decidim::Feature to get the projects from.
      def initialize(options = {})
        super(Project.all, options)
        @random_seed = options[:random_seed].to_f
      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

      # Returns the random projects for the current page.
      def results
        @projects ||= Project.transaction do
          Project.connection.execute("SELECT setseed(#{Project.connection.quote(random_seed)})")
          super.reorder("RANDOM()").load
        end
      end

      # Returns the random seed used to randomize the proposals.
      def random_seed
        @random_seed = (rand * 2 - 1) if @random_seed == 0.0 || @random_seed > 1 || @random_seed < -1
        @random_seed
      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

54 entries across 54 versions & 2 rubygems

Version Path
decidim-budgets-0.9.3 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.9.2 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.9.1 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.9.0 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.8.4 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.8.3 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.8.2 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.8.1 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.8.0 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.7.4 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.7.3 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.7.2 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.7.1 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.7.0 app/services/decidim/budgets/project_search.rb
decidim-budgets-0.6.8 app/services/decidim/budgets/project_search.rb
decidim-0.6.8 decidim-budgets/app/services/decidim/budgets/project_search.rb
decidim-budgets-0.6.7 app/services/decidim/budgets/project_search.rb
decidim-0.6.7 decidim-budgets/app/services/decidim/budgets/project_search.rb
decidim-budgets-0.6.6 app/services/decidim/budgets/project_search.rb
decidim-0.6.6 decidim-budgets/app/services/decidim/budgets/project_search.rb