Module: MuchRails::SaveService::ValidationErrors

Defined in:
lib/much-rails/save_service.rb

Class Method Summary collapse

Class Method Details

.add(exception_class, &block) ⇒ Object



30
31
32
# File 'lib/much-rails/save_service.rb', line 30

def self.add(exception_class, &block)
  service_validation_errors.add(exception_class, &block)
end

.exception_classesObject



34
35
36
# File 'lib/much-rails/save_service.rb', line 34

def self.exception_classes
  service_validation_errors.exception_classes
end

.result_for(ex) ⇒ Object



38
39
40
# File 'lib/much-rails/save_service.rb', line 38

def self.result_for(ex)
  service_validation_errors.result_for(ex)
end

.service_validation_errorsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/much-rails/save_service.rb', line 42

def self.service_validation_errors
  @service_validation_errors ||=
    MuchRails::ServiceValidationErrors
      .new
      .tap do |e|
        e.add(ActiveRecord::RecordInvalid) do |ex|
          MuchRails::SaveService::FailureResult.new(
            record: ex.record,
            exception: ex,
            validation_errors: ex.record&.errors.to_h,
            validation_error_messages:
              ex.record&.errors&.full_messages.to_a,
          )
        end
        e.add(MuchRails::InvalidError) do |ex|
          MuchRails::SaveService::FailureResult.new(
            exception: ex,
            validation_errors: ex.errors,
            validation_error_messages: ex.error_messages,
          )
        end
      end
end