<commit_before>require 'spec_helper'
require 'paper_trail/frameworks/rspec' if defined?(PaperTrail)

describe 'RailsAdmin PaperTrail history', active_record: true do
  before(:each) do
    skip 'Requires Ruby >= 2.3' if Rails::VERSION::STRING >= '5.2' && RUBY_VERSION =~ /^2\.2/
    RailsAdmin.config do |config|
      config.audit_with :paper_trail, 'User', 'PaperTrail::Version'
    end
  end

  after(:each) do
    # if #user_for_paper_trail is left unused, PaperTrail complains about it
    RailsAdmin::ApplicationController.class_eval do
      undef user_for_paper_trail
    end
  end

  describe 'on object creation', type: :request do
    subject { page }
    before do
      @user = FactoryBot.create :user
      RailsAdmin::Config.authenticate_with { warden.authenticate! scope: :user }
      RailsAdmin::Config.current_user_method(&:current_user)
      login_as @user
      with_versioning do
        visit new_path(model_name: 'paper_trail_test')
        fill_in 'paper_trail_test[name]', with: '<NAME>'
        click_button 'Save'
        @object = RailsAdmin::AbstractModel.new('PaperTrailTest').first
      end
    end

    it 'records a version' do
      expect(@object.versions.size).to eq 1
      expect(@object.versions.first.whodunnit).to eq @user.id.to_s
    end
  end

  describe 'model history fetch' do
    before(:each) do
      PaperTrail::Version.delete_all
      @model = RailsAdmin::AbstractModel.new('PaperTrailTest')
      @user = FactoryBot.create :user
      @paper_trail_test = FactoryBot.create :paper_trail_test
      with_versioning do
        PaperTrail.whodunnit = @user.id
        30.times do |i|
          @paper_trail_test.update!(name: "updated name #{i}")
        end
      end
    end

    it 'creates versions' do<commit_msg>Fix deprecation warning re: PaperTrail.whodunnit<commit_after>require 'spec_helper'
require 'paper_trail/frameworks/rspec' if defined?(PaperTrail)

describe 'RailsAdmin PaperTrail history', active_record: true do
  before(:each) do
    skip 'Requires Ruby >= 2.3' if Rails::VERSION::STRING >= '5.2' && RUBY_VERSION =~ /^2\.2/
    RailsAdmin.config do |config|
      config.audit_with :paper_trail, 'User', 'PaperTrail::Version'
    end
  end

  after(:each) do
    # if #user_for_paper_trail is left unused, PaperTrail complains about it
    RailsAdmin::ApplicationController.class_eval do
      undef user_for_paper_trail
    end
  end

  describe 'on object creation', type: :request do
    subject { page }
    before do
      @user = FactoryBot.create :user
      RailsAdmin::Config.authenticate_with { warden.authenticate! scope: :user }
      RailsAdmin::Config.current_user_method(&:current_user)
      login_as @user
      with_versioning do
        visit new_path(model_name: 'paper_trail_test')
        fill_in 'paper_trail_test[name]', with: '<NAME>'
        click_button 'Save'
        @object = RailsAdmin::AbstractModel.new('PaperTrailTest').first
      end
    end

    it 'records a version' do
      expect(@object.versions.size).to eq 1
      expect(@object.versions.first.whodunnit).to eq @user.id.to_s
    end
  end

  describe 'model history fetch' do
    before(:each) do
      PaperTrail::Version.delete_all
      @model = RailsAdmin::AbstractModel.new('PaperTrailTest')
      @user = FactoryBot.create :user
      @paper_trail_test = FactoryBot.create :paper_trail_test
      with_versioning do
        # `PaperTrail.whodunnit` deprecated in PT 9, will be removed in 10.
        # Use `PaperTrail.request.whodunnit` instead.
        if PaperTrail.respond_to?(:request)
          PaperTrail.request.whodunnit = @user.id
        else
          PaperTrail.whodunnit = @user.id
        end
        30.times do |i|
          @paper_trail_test.update!(name: "updated name #{i}")
        end
      end
    end

    it 'creates versions' do
{'fraction_non_alphanumeric': 0.10141139571353894, 'fraction_numerical': 0.005488761108207005, 'mean_word_length': 3.128371089536138, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4820439', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1218, 'n_words': 288}