<commit_before>class Premailer
  module Rails
    class Hook
      attr_reader :message

      class << self
        def perform(message)
          new(message).perform
          message
        end

        alias_method :delivering_email, :perform
        alias_method :previewing_email, :perform
      end

      def initialize(message)
        @message = message
      end

      def perform
        if skip_premailer_header_present?
          remove_skip_premailer_header
        elsif message_contains_html?
          replace_html_part(generate_html_part_replacement)
        end
      end

      private

      def skip_premailer_header_present?
        message.header[:skip_premailer]
      end

      def remove_skip_premailer_header
        message.header[:skip_premailer] = nil
      end

      def message_contains_html?
        html_part.present?
      end

      # Returns true if the message itself has a content type of text/html, thus
      # it does not contain other parts such as alternatives and attachments.
      def pure_html_message?
        message.content_type.include?('text/html')
      end

      def generate_html_part_replacement
        if generate_text_part?
          generate_alternative_part
        else
          generate_html_part
        end
      end

      def generate_text_part?
        Rails.config[:generate_text_part] && !message.text_part
      end

      def generate_alternative_part
        part = Mail::Part.new(content_type: 'multipart/alternative')
        part.add_part(generate_text_part)
        part.add_part(generate_html_part)

        part<commit_msg>Guard agains nil content type in Hook#pure_html_message?<commit_after>class Premailer
  module Rails
    class Hook
      attr_reader :message

      class << self
        def perform(message)
          new(message).perform
          message
        end

        alias_method :delivering_email, :perform
        alias_method :previewing_email, :perform
      end

      def initialize(message)
        @message = message
      end

      def perform
        if skip_premailer_header_present?
          remove_skip_premailer_header
        elsif message_contains_html?
          replace_html_part(generate_html_part_replacement)
        end
      end

      private

      def skip_premailer_header_present?
        message.header[:skip_premailer]
      end

      def remove_skip_premailer_header
        message.header[:skip_premailer] = nil
      end

      def message_contains_html?
        html_part.present?
      end

      # Returns true if the message itself has a content type of text/html, thus
      # it does not contain other parts such as alternatives and attachments.
      def pure_html_message?
        message.content_type && message.content_type.include?('text/html')
      end

      def generate_html_part_replacement
        if generate_text_part?
          generate_alternative_part
        else
          generate_html_part
        end
      end

      def generate_text_part?
        Rails.config[:generate_text_part] && !message.text_part
      end

      def generate_alternative_part
        part = Mail::Part.new(content_type: 'multipart/alternative')
        part.add_part(generate_text_part)
        part.add_part(generate_html_part)

        part
{'fraction_non_alphanumeric': 0.048921300516560316, 'fraction_numerical': 0.0, 'mean_word_length': 2.265873015873016, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7029416', 'n_tokens_mistral': 938, 'n_tokens_neox': 892, 'n_words': 208}