module Trailblazer::Endpoint::Controller

Public Class Methods

advance_endpoint(endpoint:, block_options:, domain_ctx:, endpoint_options:, flow_options:) click to toggle source

Ultimate low-level entry point. Remember that you don't have to use Endpoint.with_or_etc to invoke an endpoint.

# File lib/trailblazer/endpoint/controller.rb, line 192
def self.advance_endpoint(endpoint:, block_options:, domain_ctx:, endpoint_options:, flow_options:)

  # build Context(ctx),
  args, _ = Trailblazer::Endpoint.arguments_for(
    domain_ctx:   domain_ctx,
    flow_options: flow_options,
    **endpoint_options,
  )

  signal, (ctx, _ ) = Trailblazer::Endpoint.with_or_etc(
    endpoint,
    args, # [ctx, flow_options]

    **block_options,
    # success_block: success_block,
    # failure_block: failure_block,
    # protocol_failure_block: protocol_failure_block,
  )
end
advance_endpoint_for_controller(endpoint:, block_options:, **action_options) click to toggle source
# File lib/trailblazer/endpoint/controller.rb, line 167
def self.advance_endpoint_for_controller(endpoint:, block_options:, **action_options)
  domain_ctx, endpoint_options, flow_options = compile_options_for_controller(**action_options) # controller-specific, get from directives.

  endpoint_options = endpoint_options.merge(action_options) # DISCUSS

  Endpoint::Controller.advance_endpoint(
    endpoint:      endpoint,
    block_options: block_options,

    domain_ctx:       domain_ctx,
    endpoint_options: endpoint_options,
    flow_options:     flow_options,
  )
end
compile_options_for_controller(options_for_domain_ctx: nil, config_source:, **action_options) click to toggle source
# File lib/trailblazer/endpoint/controller.rb, line 182
def self.compile_options_for_controller(options_for_domain_ctx: nil, config_source:, **action_options)
  flow_options     = config_source.options_for(:options_for_flow_options, **action_options)
  endpoint_options = config_source.options_for(:options_for_endpoint, **action_options) # "class level"
  domain_ctx       = options_for_domain_ctx || config_source.options_for(:options_for_domain_ctx, **action_options)

  return domain_ctx, endpoint_options, flow_options
end
extended(extended) click to toggle source
# File lib/trailblazer/endpoint/controller.rb, line 4
def self.extended(extended)
  extended.extend Trailblazer::Endpoint::Options::DSL           # ::directive
  extended.extend Trailblazer::Endpoint::Options::DSL::Inherit
  extended.extend Trailblazer::Endpoint::Options                # ::options_for
  extended.extend DSL::Endpoint

  extended.include InstanceMethods # {#endpoint_for}

  # DISCUSS: hmm
  extended.directive :generic_options,          ->(*) { Hash.new } # for Controller::endpoint
  extended.directive :options_for_flow_options, ->(*) { Hash.new }
  extended.directive :options_for_endpoint,     ->(*) { Hash.new }
  extended.directive :options_for_domain_ctx,   ->(*) { Hash.new }
end
included(includer) click to toggle source
# File lib/trailblazer/endpoint/controller.rb, line 25
def self.included(includer)
  includer.extend(Controller) # only ::directive and friends.
end
module(framework: :rails, api: false, dsl: false, application_controller: false) click to toggle source

@experimental TODO: test application_controller with and without dsl/api

# File lib/trailblazer/endpoint/controller.rb, line 22
def self.module(framework: :rails, api: false, dsl: false, application_controller: false)
  if application_controller && !api && !dsl # FIXME: not tested! this is useful for an actual AppController with block_options or flow_options settings, "globally"
    Module.new do
      def self.included(includer)
        includer.extend(Controller) # only ::directive and friends.
      end
    end
  elsif api
    Module.new do
      @application_controller = application_controller
      def self.included(includer)
        if @application_controller
          includer.extend Controller
        end
        includer.include(InstanceMethods::API)
      end
    end
  elsif dsl
    Module.new do
      @application_controller = application_controller
      def self.included(includer)
        if @application_controller
          includer.extend Controller
        end
        includer.include Trailblazer::Endpoint::Controller::InstanceMethods::DSL
        includer.include Trailblazer::Endpoint::Controller::Rails
        includer.extend Trailblazer::Endpoint::Controller::Rails::DefaultBlocks
        includer.extend Trailblazer::Endpoint::Controller::Rails::DefaultParams
        includer.include Trailblazer::Endpoint::Controller::Rails::Process
      end
    end # Module
  else
    raise
  end
end
options_for_block_options(ctx, controller:, **) click to toggle source

Default blocks for the {Adapter}.

# File lib/trailblazer/endpoint/controller.rb, line 213
def self.options_for_block_options(ctx, controller:, **)
  {
    success_block:          ->(ctx, endpoint_ctx:, **) { controller.head 200 },
    failure_block:          ->(ctx, **) { controller.head 422 },
    protocol_failure_block: ->(ctx, endpoint_ctx:, **) { controller.head endpoint_ctx[:status] }
  }
end