Sha256: dd0e85b918e2a0a2d35dd3915ea7c07eb34dde197e7452501765f9adb4f682c1

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Lite
  module Form
    module Helpers
      module Errors

        module ClassMethods

          def perform(action, params = {})
            klass = %w[create update].include?(action.to_s) ? send(action, params) : send(action)

            if klass.success?
              yield(klass.result, Lite::Form::Success, Lite::Form::Failure)
            else
              yield(klass.result, Lite::Form::Failure, Lite::Form::Success)
            end
          end

        end

        class << self

          def included(klass)
            klass.extend(ClassMethods)
          end

        end

        def errored?
          !success?
        end

        def merge_errors!(klass, direction: :from)
          case direction
          when :from then errors.merge!(klass.errors)
          when :to then klass.errors.merge!(errors)
          end

          nil
        end

        def merge_exception!(exception, key: :internal)
          errors.add(key, "#{exception.class} - #{exception.message}")

          nil
        end

        def success?
          errors.empty?
        end

        def raise_transaction_rollback(exception)
          run_callbacks(:rollback)
          raise exception
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lite-form-1.1.2 lib/lite/form/helpers/errors.rb
lite-form-1.1.1 lib/lite/form/helpers/errors.rb
lite-form-1.1.0 lib/lite/form/helpers/errors.rb
lite-form-1.0.5 lib/lite/form/helpers/errors.rb