<commit_before>module TypicalSituation

  # Rails MIME responses.
  module Responses
    
    # Return the collection as HTML or JSON
    #
    def respond_with_resources
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_collection_instance
          render
        end
        format.json do
          render :json => include_root? ? { plural_model_type => @resources.as_json } : @resources.as_json
        end
      end
    end
    
    # Return the resource as HTML or JSON
    #
    # A provided block is passed the #respond_to format to further define responses.
    #
    def respond_with_resource
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          render
        end
        format.json do
          render :json => @resource.as_json(root: include_root?)
        end
      end
    end
    
    def respond_as_changed
      if has_errors?
        respond_as_error
      else
        respond_to do |format|
          yield(format) if block_given?
          format.html do
            set_single_instance
            changed_so_redirect or render
          end
          format.json do
            render :json => @resource.as_json(root: include_root?)
          end
        end
      end
    end
    
    def respond_as_created
      if has_errors?
        respond_as_error
      else
        respond_to do |format|
          yield(format) if block_given?
          format.html do
            set_single_instance
            changed_so_redirect or render
          end
          format.json do
            render :json => @resource.as_json(root: include_root?), 
              :location => @resource.respond_to?(:to_url) ? 
                @resource.to_url : polymorphic_url(@resource), 
              :status => :created
          end
        end
      end
    end
    
    def respond_as_error
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          render action: (@resource.new_record? ? :new : :edit), :status => :unprocessable_entity
        end
        format.json do
          render :json => @resource.as_json(root: include_root?, methods: [:errors]), :status => :unprocessable_entity
        end
      end
    end
    
    def respond_as_gone
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          gone_so_redirect or render
        end
        format.json do
          render :text => '', :status => :no_content
        end
      end
    end
    
    def respond_as_not_found
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          render :file => not_found_template, :status => :not_found
        end
        format.json do
          render :text => '', :status => :not_found
        end
      end
    end
    
    def respond_as_not_acceptable
      render :text => '', :status => :not_acceptable
    end
    
    # HTML response when @resource saved or updated.
    def changed_so_redirect
      redirect_to @resource
      true # return true when redirecting
    end
    
    # HTML response when @resource deleted.
    def gone_so_redirect
      redirect_to action: :index
      true # return true when redirecting
    end<commit_msg>Avoid jumping out of nested resource path.<commit_after>module TypicalSituation

  # Rails MIME responses.
  module Responses
    
    # Return the collection as HTML or JSON
    #
    def respond_with_resources
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_collection_instance
          render
        end
        format.json do
          render :json => include_root? ? { plural_model_type => @resources.as_json } : @resources.as_json
        end
      end
    end
    
    # Return the resource as HTML or JSON
    #
    # A provided block is passed the #respond_to format to further define responses.
    #
    def respond_with_resource
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          render
        end
        format.json do
          render :json => @resource.as_json(root: include_root?)
        end
      end
    end
    
    def respond_as_changed
      if has_errors?
        respond_as_error
      else
        respond_to do |format|
          yield(format) if block_given?
          format.html do
            set_single_instance
            changed_so_redirect or render
          end
          format.json do
            render :json => @resource.as_json(root: include_root?)
          end
        end
      end
    end
    
    def respond_as_created
      if has_errors?
        respond_as_error
      else
        respond_to do |format|
          yield(format) if block_given?
          format.html do
            set_single_instance
            changed_so_redirect or render
          end
          format.json do
            render :json => @resource.as_json(root: include_root?), 
              :location => @resource.respond_to?(:to_url) ? 
                @resource.to_url : polymorphic_url(@resource), 
              :status => :created
          end
        end
      end
    end
    
    def respond_as_error
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          render action: (@resource.new_record? ? :new : :edit), :status => :unprocessable_entity
        end
        format.json do
          render :json => @resource.as_json(root: include_root?, methods: [:errors]), :status => :unprocessable_entity
        end
      end
    end
    
    def respond_as_gone
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          set_single_instance
          gone_so_redirect or render
        end
        format.json do
          render :text => '', :status => :no_content
        end
      end
    end
    
    def respond_as_not_found
      respond_to do |format|
        yield(format) if block_given?
        format.html do
          render :file => not_found_template, :status => :not_found
        end
        format.json do
          render :text => '', :status => :not_found
        end
      end
    end
    
    def respond_as_not_acceptable
      render :text => '', :status => :not_acceptable
    end
    
    # HTML response when @resource saved or updated.
    def changed_so_redirect
      redirect_to action: :show, id: @resource.to_param
      true # return true when redirecting
    end
    
    # HTML response when @resource deleted.
    def gone_so_redirect
      redirect_to action: :index
      true # return true when redirecting
    end
{'fraction_non_alphanumeric': 0.06247250329960405, 'fraction_numerical': 0.0, 'mean_word_length': 1.7588996763754046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5430628', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1854, 'n_words': 536}