Class: MuchRails::Action::UnprocessableEntityResult

Inherits:
BaseResult
  • Object
show all
Defined in:
lib/much-rails/action/unprocessable_entity_result.rb

Overview

MuchRails::Action::UnprocessableEntityResult is a result returned by calling a view action that is not valid. It returns JSON with the validation details.

This result is only returned if, after validating the action, there are errors. Most commonly this occurs when validating form submission params.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors) ⇒ UnprocessableEntityResult

Returns a new instance of UnprocessableEntityResult.



18
19
20
# File 'lib/much-rails/action/unprocessable_entity_result.rb', line 18

def initialize(errors)
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/much-rails/action/unprocessable_entity_result.rb', line 16

def errors
  @errors
end

Instance Method Details

#execute_blockObject

This block is called using ‘instance_exec` in the scope of the controller



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/much-rails/action/unprocessable_entity_result.rb', line 23

def execute_block
  ->(result){
    errors =
      Array
        .wrap(much_rails_action_class&.params_root)
        .reduce(result.errors) do |acc, root|
          acc.merge(
            acc.transform_keys{ |error_key| "#{root}[#{error_key}]" },
          )
        end

    render(json: errors, status: :unprocessable_entity)
  }
end