Sha256: e6870fa926becf7e236e2e9bd48bd2b75838f87f90323c70f6b090acdbff1cd6

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require_relative '../../shared/messages'

module Dsu
  module Views
    module EditedEntries
      module Shared
        class Errors
          def initialize(edited_entries:, options: {})
            raise ArgumentError, 'edited_entries is nil' if edited_entries.nil?
            raise ArgumentError, 'edited_entries is the wrong object type' unless edited_entries.is_a?(Array)
            unless edited_entries.all?(Models::EditedEntry)
              raise ArgumentError, 'edited_entries elements are the wrong object type'
            end
            raise ArgumentError, 'options is nil' if options.nil?
            raise ArgumentError, 'options is the wrong object type' unless options.is_a?(Hash)

            @edited_entries = edited_entries
            @options = options || {}
            @header = options[:header] || 'The following ERRORS were encountered; these changes were not saved:'
          end

          def render
            return if edited_entries.empty?
            return if edited_entries.all?(&:valid?)

            messages = edited_entries.map { |edited_entry| edited_entry.errors.full_messages }.flatten
            Views::Shared::Messages.new(messages: messages, message_type: :error, options: { header: header }).render
          end

          private

          attr_reader :edited_entries, :header, :options
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dsu-1.2.1 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.2.0 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.1.2 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.1.1 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.1.0.alpha.2 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.1.0.alpha.1 lib/dsu/views/edited_entries/shared/errors.rb
dsu-1.0.0 lib/dsu/views/edited_entries/shared/errors.rb