Sha256: 9072485f6d60ed18ef9100dd0409a0c359f54d3e26762fb8d5b127f2ef218585

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module SolidusAdmin::ControllerHelpers::Search
  extend ActiveSupport::Concern

  module ClassMethods
    def search_scope(name, default: false, &block)
      search_scopes << SearchScope.new(
        name: name.to_s,
        block: block,
        default: default,
      )
    end

    def search_scopes
      @search_scopes ||= []
    end
  end

  private

  def apply_search_to(relation, param:)
    relation = apply_scopes_to(relation, param: param)
    apply_ransack_search_to(relation, param: param)
  end

  def apply_ransack_search_to(relation, param:)
    relation
      .ransack(params[param]&.except(:scope))
      .result(distinct: true)
  end

  def apply_scopes_to(relation, param:)
    current_scope_name = params.dig(param, :scope)

    search_block = (
      self.class.search_scopes.find { _1.name == current_scope_name } ||
      self.class.search_scopes.find { _1.default }
    )&.block

    # Run the search if a block is present, fall back to the relation even if the
    # block is present but returns nil.
    (search_block && instance_exec(relation, &search_block)) || relation
  end

  SearchScope = Struct.new(:name, :block, :default, keyword_init: true)
  private_constant :SearchScope
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_admin-0.3.1 app/controllers/solidus_admin/controller_helpers/search.rb
solidus_admin-0.3.0 app/controllers/solidus_admin/controller_helpers/search.rb
solidus_admin-0.2.0 app/controllers/solidus_admin/controller_helpers/search.rb
solidus_admin-0.1.0 app/controllers/solidus_admin/controller_helpers/search.rb