Sha256: 8fdd8dc3e4c65a2679ad3950c75945e436afa67b8a134d58b0623d7f84aa053f

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module HasJSendResultParamsValidations
        module UsingActiveModelValidations
          class Middleware < MethodChainMiddleware
            intended_for :result, entity: :service

            def next(...)
              return entity.failure(data: errors) if errors.any?

              chain.next(...)
            end

            private

            ##
            # NOTE: errors method (from Active Model Validations) does NOT trigger validations
            # that is why valid? is called here.
            #
            # Check the following link for more info:
            # https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors
            #
            # Quote (this method - errors):
            # > This method is only useful after validations have been run, because it only inspects the errors collection and does not trigger validations itself.
            #
            def errors
              @errors ||= entity.tap(&:valid?).errors.messages.transform_values(&:first)
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
convenient_service-0.13.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb