Sha256: eb21cfe3bfa5428a986963d0432ad76afdbe391108a02823488f01f5001a52bb

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module ROM
  module Model
    class Form
      # Proxy for form errors
      #
      # This simple proxy forwards most messages to a wrapped
      # ActiveModel::Errors object
      #
      # @api private
      class ErrorProxy < SimpleDelegator
        # @api private
        def initialize
          super ActiveModel::Errors.new([])
        end

        # update the current errors
        #
        # @param error [ActiveModel::Errors, ROM::Model::ValidatonError, object]
        #
        # When the argument is an AM Error object, or our wrapper around one,
        # replace the wrapped object.  Otherwise, add an error to the current
        # messages
        #
        # @return [self]
        #
        # @api private
        def set(error)
          case error
          when ActiveModel::Errors
            __setobj__ error
          when ROM::Model::ValidationError
            __setobj__ error.errors
          when nil
            # do nothing
          else
            add(:base, "a database error prevented saving this form")
          end

          self
        end

        # Has the command succeeded?
        #
        # @return [Boolean]
        #
        # @api public
        def success?
          !present?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-rails-0.9.0 lib/rom/rails/model/form/error_proxy.rb
rom-rails-0.9.0.beta1 lib/rom/rails/model/form/error_proxy.rb
rom-rails-0.8.0 lib/rom/rails/model/form/error_proxy.rb
rom-rails-0.7.0 lib/rom/rails/model/form/error_proxy.rb