Module: Trailblazer::Endpoint::Controller

Defined in:
lib/trailblazer/endpoint/controller.rb

Overview

TODO: test application_controller with and without dsl/api

Defined Under Namespace

Modules: DSL, InstanceMethods, Rails

Class Method Summary collapse

Class Method Details

.advance_endpoint(endpoint:, block_options:, domain_ctx:, endpoint_options:, flow_options:) ⇒ Object

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/trailblazer/endpoint/controller.rb', line 211

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) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/trailblazer/endpoint/controller.rb', line 186

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) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/trailblazer/endpoint/controller.rb', line 201

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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

.module(framework: :rails, api: false, dsl: false, application_controller: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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:) ⇒ Object

Default blocks for the Adapter.



232
233
234
235
236
237
238
# File 'lib/trailblazer/endpoint/controller.rb', line 232

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