Sha256: a773adced892e5d25ef8f46f019f543a3108bea6ce07a3cac189e0abfaf9bbd3

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

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

            ##
            # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
            #
            def next(...)
              return entity.public_send(status, data: errors, message: errors.first.to_a.map(&:to_s).join(" ")) if errors.any?

              chain.next(...)
            end

            private

            ##
            # @internal
            #   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

            ##
            # @return [Symbol]
            #
            def status
              middleware_arguments.kwargs.fetch(:status) { :error }
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.19.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.18.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.17.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.16.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.15.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.14.0 lib/convenient_service/service/plugins/has_j_send_result_params_validations/using_active_model_validations/middleware.rb