<commit_before>require 'ruby2xlsx'

module AbAdmin
  module Utils
    class XlsDocument < Ruby2xlsx::Base
      include AbAdmin::Utils::EvalHelpers

      def default_columns
        @default_columns ||= @klass.column_names
      end

      def column_data
        @columns_names ||= @options[:column_data] || default_columns
      end

      def columns_names
        (@options[:column_names] || default_columns).map { |column| column.is_a?(Symbol) ? @klass.human_attribute_name(column) : column }
      end

      def filename
        @filename ||= [@options[:filename] || "#{@klass.model_name.plural}-#{Time.now.strftime('%Y-%m-%d')}", '.xls'].join
      end

      def render
        date_format = workbook.add_format(num_format: 'dd.mm.yyyy')
        time_format = workbook.add_format(num_format: 'dd.mm.yyyy HH:MM')

        each_with_index do |item, index|
          row = index + 1

          column_data.each_with_index do |column, num|
            value = call_method_or_proc_on(item, column, exec: false)

            case value
              when Date
                worksheet.write_string(row, num, value.strftime('%Y-%m-%dT'), date_format)
              when DateTime, Time
                worksheet.write_date_time(row, num, value.strftime('%Y-%m-%dT%H:%M:%S.%L'), time_format)
              when String
                worksheet.write_string(row, num, value)
              else
                worksheet.write(row, num, AbAdmin.pretty_data(value))
            end
          end
        end

        bold = workbook.add_format(bold: 1)
        worksheet.write('A1', columns_names, bold)

        super

      end

      def worksheet_name
        @worksheet_name ||= (@options[:worksheet_name] || @klass.model_name.human)
      end

      def each_with_index
        count = 0
        if @source.is_a?(::ActiveRecord::Relation)
          @klass ||= @source.klass

          @source.find_each do |item|
            yield item, count
            count += 1
          end
        else
          items = Array.wrap(@source)
          @klass ||= items.first.class unless items.empty?
          @klass ||= Default

          items.each do |item|
            yield item, count
            count += 1
          end
        end
      end

    end
  end
end<commit_msg>fix xml document to render elastic_ransack collection<commit_after>require 'ruby2xlsx'

module AbAdmin
  module Utils
    class Default
      extend ActiveModel::Naming

      def self.human_attribute_name(*)
        ''
      end
    end

    class XlsDocument < Ruby2xlsx::Base
      include AbAdmin::Utils::EvalHelpers

      def default_columns
        @default_columns ||= @klass.column_names
      end

      def column_data
        @columns_names ||= @options[:column_data] || default_columns
      end

      def columns_names
        (@options[:column_names] || default_columns).map { |column| column.is_a?(Symbol) ? @klass.human_attribute_name(column) : column }
      end

      def filename
        @filename ||= [@options[:filename] || "#{@klass.model_name.plural}-#{Time.now.strftime('%Y-%m-%d')}", '.xls'].join
      end

      def render
        date_format = workbook.add_format(num_format: 'dd.mm.yyyy')
        time_format = workbook.add_format(num_format: 'dd.mm.yyyy HH:MM')

        each_with_index do |item, index|
          row = index + 1

          column_data.each_with_index do |column, num|
            value = call_method_or_proc_on(item, column, exec: false)

            case value
              when Date
                worksheet.write_string(row, num, value.strftime('%Y-%m-%dT'), date_format)
              when DateTime, Time
                worksheet.write_date_time(row, num, value.strftime('%Y-%m-%dT%H:%M:%S.%L'), time_format)
              when String
                worksheet.write_string(row, num, value)
              else
                worksheet.write(row, num, AbAdmin.pretty_data(value))
            end
          end
        end

        bold = workbook.add_format(bold: 1)
        worksheet.write('A1', columns_names, bold)

        super

      end

      def worksheet_name
        @worksheet_name ||= (@options[:worksheet_name] || @klass.model_name.human)
      end

      def each_with_index
        count = 0
        if @source.is_a?(::ActiveRecord::Relation)
          @klass ||= @source.klass

          @source.find_each do |item|
            yield item, count
            count += 1
          end
        else
          items = @source.respond_to?(:to_a) ? @source.to_a : Array.wrap(@source)
          @klass ||= items.first.class unless items.empty?
          @klass ||= Default

          items.each do |item|
            yield item, count
            count += 1
          end
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.10935856992639327, 'fraction_numerical': 0.0033648790746582543, 'mean_word_length': 1.9595519601742377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '2117740', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1420, 'n_words': 303}