Sha256: f9b59f41f297a62d7104d44ea3155f39408c169393c477dea45eda1aa8097025

Contents?: true

Size: 1.77 KB

Versions: 41

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Decidim
  # This is the base class to be used by other ParticipatorySpace search services
  # Searchlight documentation: https://github.com/nathanl/searchlight
  class ParticipatorySpaceSearch < Searchlight::Search
    # Initialize the Searchlight::Search base class with the options provided.
    #
    # scope   - The scope used to create the base query
    # options - A hash of options to modify the search. These options will be
    #          converted to methods by SearchLight so they can be used on filter
    #          methods. (Default {})
    def initialize(scope, options = {})
      super(options)
      @scope = scope
    end

    # Creates the SearchLight base query.
    def base_query
      @scope = @scope.where(organization: organization)
      @scope = @scope.published if @scope.respond_to?(:published)
      @scope = @scope.visible_for(current_user) if @scope.respond_to?(:visible_for)
    end

    # Handles the scope_id filter.
    def search_scope_id
      clean_scope_ids = if scope_id.is_a?(Hash)
                          scope_id.values
                        else
                          [scope_id].flatten
                        end
      conditions = []
      conditions << "decidim_scope_id IS NULL" if clean_scope_ids.delete("global")
      conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any?

      query.includes(:scope).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i))
    end

    # Handle the area_id filter
    def search_area_id
      query.includes(:area).where(decidim_area_id: area_id)
    end

    private

    def organization
      options[:organization]
    end

    def current_user
      options[:current_user]
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.9 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.8 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.7 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.5 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.4 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.3 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.2 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.1 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.0 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.0.rc2 app/services/decidim/participatory_space_search.rb
decidim-core-0.26.0.rc1 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.2 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.1 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.0 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.0.rc4 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.0.rc3 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.0.rc2 app/services/decidim/participatory_space_search.rb
decidim-core-0.25.0.rc1 app/services/decidim/participatory_space_search.rb
decidim-core-0.24.3 app/services/decidim/participatory_space_search.rb