Sha256: be8db1c61740c313ac29a820e9421476bdbd2df983326a1c4b3458ded2932bcc

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 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:,
        default:,
      )
    end

    def search_scopes
      @search_scopes ||= []
    end
  end

  private

  def apply_search_to(relation, param:)
    relation = apply_scopes_to(relation, param:)
    apply_ransack_search_to(relation, 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

1 entries across 1 versions & 1 rubygems

Version Path
solidus_admin-0.3.2 app/controllers/solidus_admin/controller_helpers/search.rb