<commit_before># frozen_string_literal: true

module Fustrate
  module Rails
    module Services
      module LogEdit
        IGNORE_COLUMNS = %w[id created_at updated_at].freeze
        RENAME_COLUMNS = {}.freeze

        def call(subject, force: false, note: nil)
          # Make sure any before_validation callbacks are run
          subject.validate

          @subject = subject
          @force = force
          @note = note
          @data = subject.changes.except(*self.class::IGNORE_COLUMNS)

          process_changes

          self.class::RENAME_COLUMNS.each do |from, to|
            @data[to] = @data.delete from if @data[from]
          end

          record_edit
        end

        protected

        def record_edit
          # Get rid of changes from nil to '' and whatnot
          @data.delete_if do |_, values|
            values.all?(&:blank?) || values[0] == values[1]
          end

          return if @data.empty? && !@force

          log_edit_on_relation.new(
            type: 'Edited',
            user: Current.user,
            data: { changes: @data, raw_changes: raw_changes },
            note: @note
          )
        end

        # Allow edit data to be recorded on a model that isn't @subject
        def log_edit_on
          @subject
        end
        
        def log_edit_on_relation
          log_edit_on.events
        end

        def raw_changes
          @subject.changes
        end

        def process_changes
          process_datetime_columns
          process_date_columns
          process_boolean_columns
          process_relations
        end

        def process_datetime_columns
          Array(columns[:date]).select { |col| @data[col] }.each do |col|
            @data[col] = format_timestamps(@data[col], time: false)
          end
        end

        def process_date_columns
          Array(columns[:datetime]).select { |col| @data[col] }.each do |col|
            @data[col] = format_timestamps(@data[col], time: true)
          end
        end
<commit_msg>allow additional data in log_edit event<commit_after># frozen_string_literal: true

module Fustrate
  module Rails
    module Services
      module LogEdit
        IGNORE_COLUMNS = %w[id created_at updated_at].freeze
        RENAME_COLUMNS = {}.freeze

        def call(subject, force: false, note: nil)
          # Make sure any before_validation callbacks are run
          subject.validate

          @subject = subject
          @force = force
          @note = note
          @data = subject.changes.except(*self.class::IGNORE_COLUMNS)

          process_changes

          self.class::RENAME_COLUMNS.each do |from, to|
            @data[to] = @data.delete from if @data[from]
          end

          record_edit
        end

        protected

        def record_edit
          # Get rid of changes from nil to '' and whatnot
          @data.delete_if do |_, values|
            values.all?(&:blank?) || values[0] == values[1]
          end

          return if @data.empty? && !@force

          log_edit_on_relation.new(
            type: 'Edited',
            user: Current.user,
            data: { changes: @data, raw_changes: raw_changes }.merge(additional_data),
            note: @note
          )
        end

        # Allow edit data to be recorded on a model that isn't @subject
        def log_edit_on
          @subject
        end
        
        def log_edit_on_relation
          log_edit_on.events
        end
        
        def additional_data
          {}
        end

        def raw_changes
          @subject.changes
        end

        def process_changes
          process_datetime_columns
          process_date_columns
          process_boolean_columns
          process_relations
        end

        def process_datetime_columns
          Array(columns[:date]).select { |col| @data[col] }.each do |col|
            @data[col] = format_timestamps(@data[col], time: false)
          end
        end

        def process_date_columns
          Array(columns[:datetime]).select { |col| @data[col] }.each do |col|
            @data[col] = format_timestamps(@data[col], time: true)
          end
        end

{'fraction_non_alphanumeric': 0.08027797747423916, 'fraction_numerical': 0.000958543014617781, 'mean_word_length': 1.6963824289405685, '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': '2124860', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1142, 'n_words': 303}