lib/action_controller/responder.rb in responders-3.0.1 vs lib/action_controller/responder.rb in responders-3.1.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true require "active_support/json" -module ActionController #:nodoc: +module ActionController # :nodoc: # Responsible for exposing a resource to different mime requests, # usually depending on the HTTP verb. The responder is triggered when # <code>respond_with</code> is called. The simplest case to study is a GET request: # # class PeopleController < ApplicationController @@ -47,11 +47,11 @@ # if @user.save # flash[:notice] = 'User was successfully created.' # format.html { redirect_to(@user) } # format.xml { render xml: @user, status: :created, location: @user } # else - # format.html { render action: "new" } + # format.html { render action: "new", status: :unprocessable_entity } # format.xml { render xml: @user.errors, status: :unprocessable_entity } # end # end # end # @@ -111,17 +111,20 @@ # @task = @project.tasks.build(params[:task]) # respond_with(@project, @task, status: 201) do |format| # if @task.save # flash[:notice] = 'Task was successfully created.' # else - # format.html { render "some_special_template" } + # format.html { render "some_special_template", status: :unprocessable_entity } # end # end # end # # Using <code>respond_with</code> with a block follows the same syntax as <code>respond_to</code>. class Responder + class_attribute :error_status, default: :ok, instance_writer: false, instance_predicate: false + class_attribute :redirect_status, default: :found, instance_writer: false, instance_predicate: false + attr_reader :controller, :request, :format, :resource, :resources, :options DEFAULT_ACTIONS_FOR_VERBS = { post: :new, patch: :edit, @@ -200,13 +203,13 @@ # This is the common behavior for formats associated with browsing, like :html, :iphone and so forth. def navigation_behavior(error) if get? raise error elsif has_errors? && default_action - render rendering_options + render error_rendering_options else - redirect_to navigation_location + redirect_to navigation_location, status: redirect_status end end # This is the common behavior for formats associated with APIs, such as :xml and :json. def api_behavior @@ -234,10 +237,12 @@ # controller. # def default_render if @default_response @default_response.call(options) + elsif !get? && has_errors? + controller.render({ status: error_status }.merge!(options)) else controller.render(options) end end @@ -261,10 +266,12 @@ def display(resource, given_options = {}) controller.render given_options.merge!(options).merge!(format => resource) end def display_errors + # TODO: use `error_status` once we switch the default to be `unprocessable_entity`, + # otherwise we'd be changing this behavior here now. controller.render format => resource_errors, :status => :unprocessable_entity end # Check whether the resource has errors. # @@ -298,14 +305,14 @@ def response_overridden? @default_response.present? end - def rendering_options + def error_rendering_options if options[:render] options[:render] else - { action: default_action } + { action: default_action, status: error_status } end end end end