Sha256: 550b6930d8854cb717396e7203391151bd45f9203e8b1d1fa4978758b8477550

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require_relative './change_context'

module SnFoil
  module Contexts
    module IndexContext
      extend ActiveSupport::Concern

      included do
        include SetupContext
      end

      class_methods do
        attr_reader :i_searcher, :i_setup_index_hooks

        def index(params: {}, user: nil, **options)
          new(user).index(**options, params: params)
        end

        def searcher(klass = nil)
          @i_searcher = klass
        end

        def setup_index(method = nil, **options, &block)
          raise ArgumentError, '#setup_index requires either a method name or a block' if method.nil? && block.nil?

          (@i_setup_index_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
        end
      end

      def searcher
        self.class.i_searcher
      end

      def setup_index_hooks
        self.class.i_setup_index_hooks || []
      end

      def index(**options)
        options[:action] = :index
        options = before_setup_index(**options)
        authorize(nil, :index?, **options)
        options.fetch(:searcher) { searcher }
               .new(scope: scope.resolve)
               .search(options.fetch(:params) { {} })
      end

      def setup_index(**options)
        options
      end

      private

      def before_setup_index(**options)
        options = setup_index(**options)
        options = setup_index_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
        options = setup(**options)
        setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
snfoil-0.4.3 lib/sn_foil/contexts/index_context.rb
snfoil-0.4.2 lib/sn_foil/contexts/index_context.rb