OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Document ServiceResult with YARD
Alexander Brandon Coles2b704315762d
diff --git a/app/services/service_result.rb b/app/services/service_result.rbindex c3ce3f46e4c..b1ca981a5ce 100644--- a/app/services/service_result.rb+++ b/app/services/service_result.rb@@ -28,28 +28,75 @@# See COPYRIGHT and LICENSE files for more details.#+++##+# Represents the result of a call to a service.+#+# @example+# result = Projects::UpdateService+# .new(user: current_user, model: @project)+# .call(permitted_params.project)+# result.success? # => true if the service call was successful.+# result.result # => #<Project id: 1011>+# result.errors # => #<ActiveModel::Errors []>+#class ServiceResultSUCCESS = trueFAILURE = false+ private_constant :SUCCESS, :FAILURE++ # @return [Boolean] whether the service call was successful.+ attr_accessor :success++ ##+ # Returns the result of the service call. In typical usage this will be a+ # model, i.e. an {ActiveRecord::Base} descendant.+ #+ # @return [Object, nil] the service call result object. This can also be nil.+ attr_accessor :result- attr_accessor :success,- :result,- :errors,- :dependent_results+ # @return [ActiveModel::Errors] errors resulting from the service call.+ attr_accessor :errors++ # @return [Array<ServiceResult>] all dependent ServiceResults - by virtue of+ # the service calling other services.+ attr_accessor :dependent_resultsattr_writer :message,:state# Creates a successful ServiceResult.+ #+ # @param (see #initialize)+ # @return (see #initialize)def self.success(**)new(**, success: SUCCESS)end+ ### Creates a failed ServiceResult.+ #+ # @param (see #initialize)+ # @return (see #initialize)def self.failure(**)new(**, success: FAILURE)end+ ##+ # @note+ # Prefer using {.success} or {.failure} factory methods to calling+ # `ServiceResult.new(success: true)` or `ServiceResult.new(success: false)`.+ #+ # @param errors [ActiveModel::Errors, nil] errors resulting from the service+ # call.+ # @param message [String, nil] an error message associated with the service+ # call.+ # @param message_type [#to_sym, nil] the type of error message when displayed+ # as a Controller flash message.+ # @param state [Shared::ServiceState, nil] the Service State object.+ # @param dependent_results [Array<ServiceResult>] any dependent+ # ServiceResults.+ # @param result [Object, nil] the result of the service call.+ # @return [ServiceResult] a new, successful instance of ServiceResult.def initialize(success: FAILURE,errors: nil,message: nil,@@ -68,15 +115,24 @@ class ServiceResultself.dependent_results = dependent_resultsend+ # @see failure?+ # @return [Boolean] whether the service call succeeded.alias success? success+ # @see success?+ # @return [Boolean] whether the service call failed.def failure?!success?end##- # Merge another service result into this instance- # allowing optionally to skip updating its service+ # Merges another ServiceResult into this instance, optionally allowing its+ # {#success} to be ignored.+ #+ # @param other [ServiceResult] the other ServiceResult.+ # @param without_success [Boolean] whether to ignore the {#success} of the+ # other ServiceResult.+ # @return [void]def merge!(other, without_success: false)merge_success!(other) unless without_successmerge_errors!(other)@@ -84,26 +140,41 @@ class ServiceResultend##- # Print messages to flash+ # Prints messages to the Controller's flash.+ #+ # @param flash [ActionDispatch::Flash::FlashHash]+ # @return [void]def apply_flash_message!(flash)if messageflash[message_type] = messageendend+ ##+ # Returns all {#result}s, including from dependent ServiceResults.+ #+ # @return [Array<Object, nil>] all results.def all_resultsdependent_results.map(&:result).tap do |results|results.unshift result unless result.nil?endend+ ##+ # Returns all {#errors}, including from dependent ServiceResults.+ #+ # @return [Array<ActiveModel::Errors>] all errors.def all_errors[errors] + dependent_results.map(&:errors)end##- # Test whether the returned errors respond- # to the search key+ # Tests whether the returned errors, including from dependent ServiceResults,+ # include the error key.+ #+ # @param attribute [:base, Symbol] the attribute.+ # @param error_key [Symbol] the type of the error.+ # @return [Boolean] whether the returned errors include the error key.def includes_error?(attribute, error_key)all_errors.any? do |error|error.symbols_for(attribute).include?(error_key)@@ -111,10 +182,11 @@ class ServiceResultend##- # Collect all present errors for the given result- # and dependent results.+ # Returns dependent ServiceResults with errors, and optionally self, if self+ # has errors.#- # Returns a map of the service result to the error object+ # @param include_self [Boolean] whether to include self, if self has errors.+ # @return [Array<ServiceResult>] all ServiceResults with errors.def results_with_errors(include_self: true)results =if include_self@@ -126,10 +198,15 @@ class ServiceResultresults.reject { |call| call.errors.empty? }end+ # @return [Array<ServiceResult>] self and dependent ServiceResults.def self_and_dependent[self] + dependent_resultsend+ ##+ # Adds a dependent ServiceResult.+ #+ # @param [ServiceResult] dependent the dependent ServiceResult to add.def add_dependent!(dependent)merge_success!(dependent)@@ -140,21 +217,47 @@ class ServiceResultself.dependent_results += inner_resultsend+ ##+ # Executes block argument if the service call succeeded.+ #+ # @yield block to be called on success.+ # @return [self]def on_success(&)tap(&) if success?selfend+ ##+ # Executes block argument if the service call failed.+ #+ # @yield block to be called on failure.+ # @return [self]def on_failure(&)tap(&) if failure?selfend+ ##+ # Iterates exactly once, passing the result to the block, if the service call+ # succeeded.+ #+ # @see Enumerable#each+ # @yield block to be called on success.+ # @yieldparam result [Object, nil] the result of the service call.+ # @return [self]def eachyield result if success?selfend+ ##+ # If the service call succeeded, returns a copy of the ServiceResult whose+ # whose {#result} is the return value from the block. Iterates exactly once+ # if the service call succeeded.+ #+ # @yield block to be called on success.+ # @yieldparam result [Object, nil] the result of the service call.+ # @return [ServiceResult] a new ServiceResult with the result.def mapreturn self if failure?@@ -163,6 +266,8 @@ class ServiceResultendend+ # @return [Array<Object, nil>] the {#result} wrapped in an Array if the+ # service call succeeded, or an empty Array if the service call failed.def to_aif success?[result]@@ -171,6 +276,12 @@ class ServiceResultendend+ ##+ # Allows ServiceResult to be used with pattern matching.+ #+ # @param [Array<:success, :failure, :result, :error>] keys the keys to match+ # on.+ # @return [Hash{Symbol=>Object}] the match result.def deconstruct_keys(keys)if keysvalue = {}@@ -189,6 +300,7 @@ class ServiceResultendend+ # @return [String] error message associated with the service call.def messageif @message@message@@ -201,13 +313,19 @@ class ServiceResultendend+ # @return [Shared::ServiceState] the Service State object.def state@state ||= ::Shared::ServiceState.buildend##- # Required as we create an errors object bound to this ServiceResult.- # calling `errors#full_messages` will call `human_attribute_name` here.+ # @api private+ # @note+ # Required as we create an errors object bound to this ServiceResult.+ # Calling `errors#full_messages` will call {.human_attribute_name} here.+ #+ # @see ApplicationRecord.human_attribute_name+ # @return [String] the attribute name in a more human formatdef self.human_attribute_name(*)ApplicationRecord.human_attribute_name(*)enddiff --git a/lib/open_project/patches/active_record_i18n.rb b/lib/open_project/patches/active_record_i18n.rbindex 301ee5a4901..6f1e56b1724 100644--- a/lib/open_project/patches/active_record_i18n.rb+++ b/lib/open_project/patches/active_record_i18n.rb@@ -32,6 +32,7 @@ module ActiveRecordclass Baseinclude Redmine::I18n+ # @return [String] the attribute name in a more human formatdef self.human_attribute_name(attr, options = {})attr = attr.to_s.delete_suffix("_id")