Skip to main content

My plugin is added to the config, but its middleware is never called

Some plugins are order-dependent.

For example, CanHaveConnectedSteps has a middleware for the instance result method in the Standard config.

middlewares :result do
# ...
use Plugins::Service::CanHaveConnectedSteps::Middleware
# ...
end

If your plugin enhances the instance result method as well, you may be affected by CanHaveConnectedSteps.

How? Let's check its source.

module ConvenientService
module Service
module Plugins
module CanHaveConnectedSteps
class Middleware < Core::MethodChainMiddleware
def next(...)
return chain.next(...) if entity.steps.none?
# ...
entity.steps.result
end
# ...
end
end
end
end
end

It works as a proxy middleware.

When a service has no steps, CanHaveConnectedSteps just calls what is next in the middleware chain, by chain.next(...).

Otherwise, it returns the last completed step result.

Let's place, e.g. HasJSendResultParamsValidations::UsingActiveModelValidations plugin after CanHaveConnectedSteps in your application service config.

module ApplicationService
module Config
include ConvenientService::Concern

included do
include ConvenientService::Standard::Config
# ...
middlewares :result do
use ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingActiveModelValidations::Middleware
end
end
end
end

This way HasJSendResultParamsValidations::UsingActiveModelValidations is never called for services with steps.

To fix that, locate it before CanHaveConnectedSteps like so:

module ApplicationService
module Config
include ConvenientService::Concern

included do |service_class|
service_class.class_exec do
include ConvenientService::Standard::Config
# ...
middlewares :result do
insert_before \
ConvenientService::Plugins::Service::CanHaveConnectedSteps::Middleware,
ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingActiveModelValidations::Middleware
end
end
end
end
end

Apart from insert_before, insert_after, insert_after_each, insert_before_each, replace, and delete are also available.