<commit_before>module Admin
  class ResourceController < Admin::BaseController
    respond_to :html

    before_action :load_collection, only: :index
    before_action :load_object, unless: -> { skip_preload.include? action_name.to_sym }

    def create
      @object.update_attributes(permitted_params)
      respond_with :admin, admin_responder
    end
    alias_method :update, :create

    def destroy
      flash[:notice] = "Successfully deleted resource" if @object.destroy

      redirect_to :back
    end

    def duplicatable?
      respond_to?(:duplicate)
    end
    helper_method :duplicatable?

    def update_positions
      unless [:id, :position].all? { |i| params[i].present? }
        return render json: { errors: "Model ID and new position are both required" }, status: :invalid_entity
      end

      model_class.find(params[:id]).set_list_position(params[:position])

      render json: "Success"
    end

    protected

    def admin_responder
      @object
    end

    def model_name
      self.class.to_s.demodulize.gsub(/Controller\Z/, "").singularize.underscore.humanize
    end
    helper_method :model_name

    def model_path
      model_class.name.underscore.parameterize(separator: "_")
    end
    helper_method :model_path

    def model_class
      class_name = self.class.to_s.match(/^Admin::(.*)Controller$/)[1].singularize
      class_name.constantize
    end

    def collection_scope
      model_class
    end

    def id_param
      params["#{model_path.underscore}_id"] || params[:id]
    end

    def search_params
      params[:q] || {}
    end

    def load_collection
      @q = collection_scope.ransack(search_params)
      @collection = @q.result(distinct: true).page(params[:page]).per(100)
      instance_variable_set("@#{model_path.pluralize.underscore}", @collection)
    end

    def load_object
      @object ||= if %w(new create).include? action_name
                    model_class.new
                  else
                    model_class.find id_param
                  end

      instance_variable_set("@#{model_path.underscore}", @object)
    end

    def collection_table_fields
      fields = Hash[model_class.attribute_names.map { |n| [n, n.humanize] }]
      fields["id"] = "ID"<commit_msg>Fall back to searching on friendly_id<commit_after>module Admin
  class ResourceController < Admin::BaseController
    respond_to :html

    before_action :load_collection, only: :index
    before_action :load_object, unless: -> { skip_preload.include? action_name.to_sym }

    def create
      @object.update_attributes(permitted_params)
      respond_with :admin, admin_responder
    end
    alias_method :update, :create

    def destroy
      flash[:notice] = "Successfully deleted resource" if @object.destroy

      redirect_to :back
    end

    def duplicatable?
      respond_to?(:duplicate)
    end
    helper_method :duplicatable?

    def update_positions
      unless [:id, :position].all? { |i| params[i].present? }
        return render json: { errors: "Model ID and new position are both required" }, status: :invalid_entity
      end

      model_class.find(params[:id]).set_list_position(params[:position])

      render json: "Success"
    end

    protected

    def admin_responder
      @object
    end

    def model_name
      self.class.to_s.demodulize.gsub(/Controller\Z/, "").singularize.underscore.humanize
    end
    helper_method :model_name

    def model_path
      model_class.name.underscore.parameterize(separator: "_")
    end
    helper_method :model_path

    def model_class
      class_name = self.class.to_s.match(/^Admin::(.*)Controller$/)[1].singularize
      class_name.constantize
    end

    def collection_scope
      model_class
    end

    def id_param
      params["#{model_path.underscore}_id"] || params[:id]
    end

    def search_params
      params[:q] || {}
    end

    def load_collection
      @q = collection_scope.ransack(search_params)
      @collection = @q.result(distinct: true).page(params[:page]).per(100)
      instance_variable_set("@#{model_path.pluralize.underscore}", @collection)
    end

    def load_object
      @object ||= if %w(new create).include? action_name
                    model_class.new
                  else
                    obj = model_class.find_by id: id_param
                    obj = model_class.friendly.find id_param if obj.blank?
                  end

      instance_variable_set("@#{model_path.underscore}", @object)
    end

    def collection_table_fields
      fields = Hash[model_class.attribute_names.map { |n| [n, n.humanize] }]
      fields["id"] = "ID"
{'fraction_non_alphanumeric': 0.09922178988326848, 'fraction_numerical': 0.0017293558149589277, 'mean_word_length': 2.767915309446254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3510391', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1364, 'n_words': 293}