<commit_before>module RestfullyManageable
  extend ActiveSupport::Concern

  module ClassMethods
    # Build standard RESTful actions to manage records of a model
    def manage_restfully(defaults = {})
      name = controller_name
      path = controller_path
      options = defaults.extract!(:t3e, :creation_t3e, :redirect_to, :xhr, :destroy_to, :subclass_inheritance, :partial, :multipart, :except, :only, :cancel_url)
      after_save_url    = options[:redirect_to]
      after_destroy_url = options[:destroy_to] || :index
      actions  = [:index, :show, :new, :create, :edit, :update, :destroy]
      actions &= [options[:only]].flatten   if options[:only]
      actions -= [options[:except]].flatten if options[:except]

      record_name = name.to_s.singularize
      model_name  = name.to_s.classify
      model = model_name.constantize

      if after_save_url.blank?
        if instance_methods(true).include?(:show) || actions.include?(:show)
          after_save_url = :show
        elsif instance_methods(true).include?(:index) || actions.include?(:index)
          after_save_url = :index
        end
      end

      if after_save_url == :show
        after_save_url = "{action: :show, id: 'id'.c}".c
      elsif after_save_url == :index
        after_save_url = '{action: :index}'.c
      elsif after_save_url.is_a?(CodeString)
        after_save_url.gsub!(/RECORD/, "@#{record_name}")
      elsif after_save_url.is_a?(Hash)
        after_save_url = after_save_url.inspect.gsub(/RECORD/, "@#{record_name}")
      end

      render_form_options = []
      render_form_options << "partial: '#{options[:partial]}'" if options[:partial]
      render_form_options << 'multipart: true' if options[:multipart]
      options[:cancel_url] ||= if actions.include?(:index)
                                 { action: :index }
                               else
                                 :back
                               end
      render_form_options << "locals: {cancel_url: #{options[:cancel_url].inspect}}"
      render_form = 'render(' + render_form_options.join(', ') + ')'

      after_save_url ||= options[:cancel_url].inspect

      t3e_code = "t3e(@#{record_name}.attributes"
      if t3e = options[:t3e]
        t3e_code << '.merge(' + t3e.collect do |k, v|
          "#{k}: (" + (v.is_a?(Symbol) ? "@#{record_name}.#{v}" : v.inspect.gsub(/RECORD/, '@' + record_name)) + ')'
        end.join(', ') + ')'
      end
      t3e_code << ')'

      creation_t3e = options[:creation_t3e].is_a?(TrueClass)

      code = ''

      code << "respond_to :html, :xml, :json\n"
      # code << "respond_to :pdf, :odt, :ods, :csv, :docx, :xlsx, :only => [:show, :index]\n"

      if actions.include?(:index)
        code << "def index\n"
        code << "  respond_to do |format|\n"
        code << "    format.html\n"
        code << "    format.xml  { render xml:  resource_model.all }\n"
        code << "    format.json { render json: resource_model.all }\n"
        code << "  end\n"
        code << "end\n"
      end

      if actions.include?(:show)
        code << "def show\n"
        code << "  return unless @#{record_name} = find_and_check\n"
        parents = [self]
        while parents.last.superclass < ActionController::Base
          parents << parents.last.superclass
        end
        lookup = Rails.root.join('app', 'views', "{#{parents.map(&:controller_path).join(',')}}")
        if Dir.glob(lookup.join('show.*')).any?
          if options[:subclass_inheritance]
            code << "  if @#{record_name}.type and @#{record_name}.type != '#{model_name}'\n"
            code << "    redirect_to controller: @#{record_name}.type.tableize, action: :show, id: @#{record_name}.id\n"
            code << "    return\n"
            code << "  end\n"
          end
          code << "  respond_to do |format|\n"
          code << "    format.html { #{t3e_code} }\n"
          code << "    format.xml  { render xml:  @#{record_name} }\n"
          code << "    format.json\n" #  { render json: @#{record_name} }
          code << "  end\n"<commit_msg>Refuses redirect on AJAX call on STI controllers. Fixes #832.<commit_after>module RestfullyManageable
  extend ActiveSupport::Concern

  module ClassMethods
    # Build standard RESTful actions to manage records of a model
    def manage_restfully(defaults = {})
      name = controller_name
      path = controller_path
      options = defaults.extract!(:t3e, :creation_t3e, :redirect_to, :xhr, :destroy_to, :subclass_inheritance, :partial, :multipart, :except, :only, :cancel_url)
      after_save_url    = options[:redirect_to]
      after_destroy_url = options[:destroy_to] || :index
      actions  = [:index, :show, :new, :create, :edit, :update, :destroy]
      actions &= [options[:only]].flatten   if options[:only]
      actions -= [options[:except]].flatten if options[:except]

      record_name = name.to_s.singularize
      model_name  = name.to_s.classify
      model = model_name.constantize

      if after_save_url.blank?
        if instance_methods(true).include?(:show) || actions.include?(:show)
          after_save_url = :show
        elsif instance_methods(true).include?(:index) || actions.include?(:index)
          after_save_url = :index
        end
      end

      if after_save_url == :show
        after_save_url = "{action: :show, id: 'id'.c}".c
      elsif after_save_url == :index
        after_save_url = '{action: :index}'.c
      elsif after_save_url.is_a?(CodeString)
        after_save_url.gsub!(/RECORD/, "@#{record_name}")
      elsif after_save_url.is_a?(Hash)
        after_save_url = after_save_url.inspect.gsub(/RECORD/, "@#{record_name}")
      end

      render_form_options = []
      render_form_options << "partial: '#{options[:partial]}'" if options[:partial]
      render_form_options << 'multipart: true' if options[:multipart]
      options[:cancel_url] ||= if actions.include?(:index)
                                 { action: :index }
                               else
                                 :back
                               end
      render_form_options << "locals: {cancel_url: #{options[:cancel_url].inspect}}"
      render_form = 'render(' + render_form_options.join(', ') + ')'

      after_save_url ||= options[:cancel_url].inspect

      t3e_code = "t3e(@#{record_name}.attributes"
      if t3e = options[:t3e]
        t3e_code << '.merge(' + t3e.collect do |k, v|
          "#{k}: (" + (v.is_a?(Symbol) ? "@#{record_name}.#{v}" : v.inspect.gsub(/RECORD/, '@' + record_name)) + ')'
        end.join(', ') + ')'
      end
      t3e_code << ')'

      creation_t3e = options[:creation_t3e].is_a?(TrueClass)

      code = ''

      code << "respond_to :html, :xml, :json\n"
      # code << "respond_to :pdf, :odt, :ods, :csv, :docx, :xlsx, :only => [:show, :index]\n"

      if actions.include?(:index)
        code << "def index\n"
        code << "  respond_to do |format|\n"
        code << "    format.html\n"
        code << "    format.xml  { render xml:  resource_model.all }\n"
        code << "    format.json { render json: resource_model.all }\n"
        code << "  end\n"
        code << "end\n"
      end

      if actions.include?(:show)
        code << "def show\n"
        code << "  return unless @#{record_name} = find_and_check\n"
        parents = [self]
        while parents.last.superclass < ActionController::Base
          parents << parents.last.superclass
        end
        lookup = Rails.root.join('app', 'views', "{#{parents.map(&:controller_path).join(',')}}")
        if Dir.glob(lookup.join('show.*')).any?
          if options[:subclass_inheritance]
            code << "  if @#{record_name}.type and @#{record_name}.type != '#{model_name}' && !request.xhr?\n"
            code << "    redirect_to controller: @#{record_name}.type.tableize, action: :show, id: @#{record_name}.id, format: params[:format]\n"
            code << "    return\n"
            code << "  end\n"
          end
          code << "  respond_to do |format|\n"
          code << "    format.html { #{t3e_code} }\n"
          code << "    format.xml  { render xml:  @#{record_name} }\n"
          code << "    format.json\n" #  { render json: @#{record_name} }
          code << "  end\n"
{'fraction_non_alphanumeric': 0.15475036354823074, 'fraction_numerical': 0.003271934076587494, 'mean_word_length': 2.3948992184286304, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306613', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2682, 'n_words': 529}