Sha256: 7a47c98d2fc7f13b232b7437c91582f47dfdfffe6e0674e187661c8631dbfeee

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Trailblazer
  class Finder
    module Activities
      class ProcessAdapters < Trailblazer::Activity::Railway
        def set_adapter((ctx, _flow_options), **)
          adapter = ctx[:adapter]
          ctx[:orm] = {}
          ctx[:orm][:adapter] = adapter
          ctx[:orm][:predicates] = "Trailblazer::Finder::Adapters::#{adapter}::Predicates"
          ctx[:orm][:paging] = "Trailblazer::Finder::Adapters::#{adapter}::Paging"
          ctx[:orm][:sorting] = "Trailblazer::Finder::Adapters::#{adapter}::Sorting"
          true
        end

        def set_paginator(ctx, **)
          paginator = ctx.dig(:config, :paginator)
          return true unless paginator
          return false unless ORM_ADAPTERS.include?(ctx[:orm][:adapter])
          return false unless PAGING_ADAPTERS.include?(paginator)

          ctx[:orm][:paging] = "Trailblazer::Finder::Adapters::#{paginator}::Paging"
          true
        end

        def invalid_paginator_error(ctx, **)
          (ctx[:errors] ||= []) << {
            paginator: "Can't use paginator #{ctx.dig(
              :config,
              :paginator
            )} without using an ORM like ActiveRecord or Sequel"
          }
        end

        step :set_adapter, fast_track: true
        step :set_paginator
        fail :invalid_paginator_error
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trailblazer-finder-0.90.0 lib/trailblazer/finder/activities/process_adapters.rb
trailblazer-finder-0.80.1 lib/trailblazer/finder/activities/process_adapters.rb
trailblazer-finder-0.70.0 lib/trailblazer/finder/activities/process_adapters.rb