Sha256: bd8b6049436cb5f4ce516802c4a532e383099c07349a09a665a0eb9e87890613

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Composable
  module Core
    module RecordInvalid
      extend ActiveSupport::Concern
      include ActiveSupport::Rescuable

      class ErrorWithRecord < Exception
        attr_reader :record

        def initialize(record)
          @record = record
        end
      end

      included do
        prepend ComposableDSLExt

        rescue_from(ErrorWithRecord) do |exception|
          exception.record.errors.delete(:base)&.each { |m| errors.add(:base, m) }
          exception.record.errors.each do |error|
            if error.attribute.to_sym.in?(attributes)
              errors.add(error.attribute, error.message)
            else
              errors.add(:base, exception.record.errors.full_message(error.attribute, error.message))
            end
          end
        end
      end

      module ComposableDSLExt
        def save_composable_records
          super
        rescue ActiveRecord::RecordInvalid => exception
          raise ErrorWithRecord.new(exception.record)
        end
      end
    end
  end
end

Composable::Core::ComposableDSL.include Composable::Core::RecordInvalid

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
composable-core-0.0.12 lib/composable/core/record_invalid.rb
composable-core-0.0.11 lib/composable/core/record_invalid.rb
composable-core-0.0.10 lib/composable/core/record_invalid.rb
composable-core-0.0.9 lib/composable/core/record_invalid.rb
composable-core-0.0.8 lib/composable/core/record_invalid.rb
composable-core-0.0.7 lib/composable/core/record_invalid.rb
composable-core-0.0.6 lib/composable/core/record_invalid.rb
composable-core-0.0.5 lib/composable/core/record_invalid.rb