Sha256: ca747b820de8aa57442f0723f374135ea530c048732b1b48103455adee75e3d8

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # Service that encapsulates all logic related to filtering initiatives.
    class InitiativeSearch < Searchlight::Search
      include CurrentLocale

      # Public: Initializes the service.
      # page        - The page number to paginate the results.
      # per_page    - The number of proposals to return per page.
      def initialize(options = {})
        super(options)
      end

      def base_query
        Decidim::Initiative
          .includes(scoped_type: [:scope])
          .where(organization: options[:organization])
      end

      # Handle the search_text filter
      def search_search_text
        query
          .where("title->>'#{current_locale}' ILIKE ?", "%#{search_text}%")
          .or(
            query.where(
              "description->>'#{current_locale}' ILIKE ?",
              "%#{search_text}%"
            )
          )
      end

      # Handle the state filter
      def search_state
        case state
        when "closed"
          query.closed
        else # Assume open
          query.open
        end
      end

      def search_type
        return query if type == "all"

        types = InitiativesTypeScope.where(decidim_initiatives_types_id: type).pluck(:id)

        query.where(scoped_type: types)
      end

      def search_author
        if author == "myself" && options[:current_user]
          query.where(decidim_author_id: options[:current_user].id)
        else
          query
        end
      end

      def search_scope_id
        return if scope_id.nil?

        query
          .joins(:scoped_type)
          .where(
            "decidim_initiatives_type_scopes.decidim_scopes_id": scope_id
          )
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-initiatives-0.21.0 app/services/decidim/initiatives/initiative_search.rb
decidim-initiatives-0.20.1 app/services/decidim/initiatives/initiative_search.rb
decidim-initiatives-0.20.0 app/services/decidim/initiatives/initiative_search.rb
decidim-initiatives-0.19.1 app/services/decidim/initiatives/initiative_search.rb
decidim-initiatives-0.19.0 app/services/decidim/initiatives/initiative_search.rb