Sha256: 4dcf9003afcaa47cd31223a6d14c418cd3b445a6dfdc0897b632cd8d69672801

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Pragma
  module Operation
    # Finds all records of the requested resource, authorizes them, paginates them and decorates
    # them.
    class Index < Pragma::Operation::Base
      step Macro::Classes()
      step :retrieve!, name: 'retrieve'
      step :scope!, name: 'scope'
      step Macro::Filtering()
      step Macro::Ordering()
      step Macro::Pagination()
      step Macro::Decorator(name: :collection)
      step :include!, name: 'include'
      step :respond!, name: 'respond'

      def retrieve!(options)
        options['model'] = options['model.class'].all
      end

      def include!(options)
        options['model'] = AssociationIncluder
          .load_adaptor(options['model'])
          .include_associations(options['params'][:expand] || [])
      end

      # TODO: Turn this into a macro.
      def scope!(options, model:, **)
        options['model'] = options['policy.default.scope.class'].new(
          options['policy.context'] || options['current_user'],
          model
        ).resolve
      end

      def respond!(options, **)
        options['result.response'] = Response::Ok.new(
          entity: options['result.decorator.collection']
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pragma-2.5.0 lib/pragma/operation/index.rb