<commit_before>module RailsAdmin
  module Extensions
    module PaperTrail
      class VersionProxy
        def initialize(version, user_class = User)
          @version = version
          @user_class = user_class
        end

        def message
          @message = @version.event
          @version.respond_to?(:changeset) && @version.changeset.present? ? @message + ' [' + @version.changeset.to_a.collect { |c| c[0] + ' = ' + c[1][1].to_s }.join(', ') + ']' : @message
        end

        def created_at
          @version.created_at
        end

        def table
          @version.item_type
        end

        def username
          @user_class.find(@version.whodunnit).try(:email) rescue nil || @version.whodunnit
        end

        def item
          @version.item_id
        end
      end

      class AuditingAdapter
        COLUMN_MAPPING = {
          table: :item_type,
          username: :whodunnit,
          item: :item_id,
          created_at: :created_at,
          message: :event,
        }.freeze

        def self.setup
          raise('PaperTrail not found') unless defined?(::PaperTrail)
          RailsAdmin::ApplicationController.class_eval do
            def user_for_paper_trail
              _current_user.try(:id) || _current_user
            end
          end
        end

        def initialize(controller, user_class = 'User', version_class = '::Version')
          @controller = controller
          @controller.send(:set_paper_trail_whodunnit) if @controller
          begin
            @user_class = user_class.to_s.constantize
          rescue NameError
            raise "Please set up Papertrail's user model explicitly. Ex: config.audit_with :paper_trail, 'User'"
          end

          begin
            @version_class = version_class.to_s.constantize
          rescue NameError
            raise "Please set up Papertrail's version model explicitly. Ex: config.audit_with :paper_trail, 'User', 'PaperTrail::Version'"
          end
        end

        def latest
          @version_class.order('id DESC').limit(100).collect { |version| VersionProxy.new(version, @user_class) }
        end

        def delete_object(_object, _model, _user)
          # do nothing
        end

        def update_object(_object, _model, _user, _changes)
          # do nothing
        end

        def create_object(_object, _abstract_model, _user)
          # do nothing
        end

        def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
          listing_for_model_or_object(model, nil, query, sort, sort_reverse, all, page, per_page)
        end

        def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
          listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
        end

      protected

        def listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
          if sort.present?
            sort = COLUMN_MAPPING[sort.to_sym]<commit_msg>Eager load paper_trail item association to avoid N+1 queries<commit_after>module RailsAdmin
  module Extensions
    module PaperTrail
      class VersionProxy
        def initialize(version, user_class = User)
          @version = version
          @user_class = user_class
        end

        def message
          @message = @version.event
          @version.respond_to?(:changeset) && @version.changeset.present? ? @message + ' [' + @version.changeset.to_a.collect { |c| c[0] + ' = ' + c[1][1].to_s }.join(', ') + ']' : @message
        end

        def created_at
          @version.created_at
        end

        def table
          @version.item_type
        end

        def username
          @user_class.find(@version.whodunnit).try(:email) rescue nil || @version.whodunnit
        end

        def item
          @version.item_id
        end
      end

      class AuditingAdapter
        COLUMN_MAPPING = {
          table: :item_type,
          username: :whodunnit,
          item: :item_id,
          created_at: :created_at,
          message: :event,
        }.freeze

        def self.setup
          raise('PaperTrail not found') unless defined?(::PaperTrail)
          RailsAdmin::ApplicationController.class_eval do
            def user_for_paper_trail
              _current_user.try(:id) || _current_user
            end
          end
        end

        def initialize(controller, user_class = 'User', version_class = '::Version')
          @controller = controller
          @controller.send(:set_paper_trail_whodunnit) if @controller
          begin
            @user_class = user_class.to_s.constantize
          rescue NameError
            raise "Please set up Papertrail's user model explicitly. Ex: config.audit_with :paper_trail, 'User'"
          end

          begin
            @version_class = version_class.to_s.constantize
          rescue NameError
            raise "Please set up Papertrail's version model explicitly. Ex: config.audit_with :paper_trail, 'User', 'PaperTrail::Version'"
          end
        end

        def latest
          @version_class
            .order(id: :desc).includes(:item).limit(100)
            .collect { |version| VersionProxy.new(version, @user_class) }
        end

        def delete_object(_object, _model, _user)
          # do nothing
        end

        def update_object(_object, _model, _user, _changes)
          # do nothing
        end

        def create_object(_object, _abstract_model, _user)
          # do nothing
        end

        def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
          listing_for_model_or_object(model, nil, query, sort, sort_reverse, all, page, per_page)
        end

        def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
          listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
        end

      protected

        def listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
          if sort.present?
            sort = COLUMN_MAPPING[sort.to_sym]
{'fraction_non_alphanumeric': 0.08827694728560188, 'fraction_numerical': 0.0033044846577498033, 'mean_word_length': 2.1496531219028743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3869371', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1813, 'n_words': 435}