<commit_before>module MessageTrain
  module Mixin
    extend ActiveSupport::Concern
    class_methods do
      def message_train(options = {})
        table_sym = table_name.to_sym

        relationships = options[:only] ? [options[:only]].flatten : [:sender, :recipient]
        if options[:except]
          relationships = relationships - [options[:except]].flatten
        end

        if relationships.include? :sender
          has_many :messages, as: :sender, class_name: 'MessageTrain::Message'
        end

        if relationships.include? :recipient
          has_many :receipts, as: :recipient, class_name: 'MessageTrain::Receipt'
        end

        MessageTrain.configure(MessageTrain.configuration) do |config|
          if options[:name_column].present?
            config.name_columns[table_sym] = options[:name_column]
          end

          if options[:slug_column].present?
            config.slug_columns[table_sym] = options[:slug_column]
          end

          if options[:address_book_method].present?
            config.address_book_methods[table_sym] = options[:address_book_method]
          end

          if relationships.include? :recipient
            config.recipient_tables[table_sym] = name
          end

          if options[:collectives_for_recipient].present?
            config.collectives_for_recipient_methods[table_sym] = options[:collectives_for_recipient]
          end

          if options[:valid_senders].present?
            config.valid_senders_methods[table_sym] = options[:valid_senders]
          end

          if options[:valid_recipients].present?
            config.valid_recipients_methods[table_sym] = options[:valid_recipients]
          end
        end

        send(:define_method, :slug_part) {
          send(MessageTrain.configuration.slug_columns[table_sym])
        }

        send(:define_method, :path_part) {
          if MessageTrain.configuration.valid_senders_methods[table_sym].present?
            # This must mean it's a collective
            "#{self.class.table_name}:#{slug_part}"
          end
        }

        send(:define_method, :valid_senders) {
          valid_senders_method = MessageTrain.configuration.valid_senders_methods[self.class.table_name.to_sym] || :self_collection
          send(valid_senders_method)
        }

        send(:define_method, :allows_sending_by?) { |sender|
          valid_senders.include? sender
        }

        send(:define_method, :valid_recipients) {
          valid_recipients_method = MessageTrain.configuration.valid_recipients_methods[self.class.table_name.to_sym] || :self_collection
          send(valid_recipients_method)
        }

        send(:define_method, :allows_receiving_by?) { |recipient|
          valid_recipients.include? recipient
        }

        send(:define_method, :self_collection) { # This turns a single record into an active record collection.
          model = self.class
          model.where(id: self.id)
        }

        if relationships.include? :recipient
          send(:define_method, :box) { |*args|
            case args.count
              when 0
                division = :in
                participant = self
              when 1
                division = args[0] || :in
                participant = self
              when 2
                division = args[0] || :in
                participant = args[1] || self
              else<commit_msg>Fixed "Can't dup NilClass" in mixin<commit_after>module MessageTrain
  module Mixin
    extend ActiveSupport::Concern
    class_methods do
      def message_train(options = {})
        table_sym = table_name.to_sym

        relationships = options[:only] ? [options[:only]].flatten : [:sender, :recipient]
        if options[:except]
          relationships = relationships - [options[:except]].flatten
        end

        if relationships.include? :sender
          has_many :messages, as: :sender, class_name: 'MessageTrain::Message'
        end

        if relationships.include? :recipient
          has_many :receipts, as: :recipient, class_name: 'MessageTrain::Receipt'
        end

        MessageTrain.configure(MessageTrain.configuration) do |config|
          if options[:name_column].present?
            config.name_columns[table_sym] = options[:name_column]
          end

          if options[:slug_column].present?
            config.slug_columns[table_sym] = options[:slug_column]
          end

          if options[:address_book_method].present?
            config.address_book_methods[table_sym] = options[:address_book_method]
          end

          if relationships.include? :recipient
            config.recipient_tables[table_sym] = name
          end

          if options[:collectives_for_recipient].present?
            config.collectives_for_recipient_methods[table_sym] = options[:collectives_for_recipient]
          end

          if options[:valid_senders].present?
            config.valid_senders_methods[table_sym] = options[:valid_senders]
          end

          if options[:valid_recipients].present?
            config.valid_recipients_methods[table_sym] = options[:valid_recipients]
          end
        end

        send(:define_method, :slug_part) {
          send(MessageTrain.configuration.slug_columns[table_sym])
        }

        send(:define_method, :path_part) {
          if MessageTrain.configuration.valid_senders_methods[table_sym].present?
            # This must mean it's a collective
            "#{self.class.table_name}:#{slug_part}"
          end
        }

        send(:define_method, :valid_senders) {
          valid_senders_method = MessageTrain.configuration.valid_senders_methods[self.class.table_name.to_sym] || :self_collection
          send(valid_senders_method)
        }

        send(:define_method, :allows_sending_by?) { |sender|
          valid_senders.include? sender
        }

        send(:define_method, :valid_recipients) {
          valid_recipients_method = MessageTrain.configuration.valid_recipients_methods[self.class.table_name.to_sym] || :self_collection
          send(valid_recipients_method)
        }

        send(:define_method, :allows_receiving_by?) { |recipient|
          if valid_recipients.empty?
            return false
          end
          valid_recipients.include? recipient
        }

        send(:define_method, :self_collection) { # This turns a single record into an active record collection.
          model = self.class
          model.where(id: self.id)
        }

        if relationships.include? :recipient
          send(:define_method, :box) { |*args|
            case args.count
              when 0
                division = :in
                participant = self
              when 1
                division = args[0] || :in
                participant = self
              when 2
                division = args[0] || :in
                participant = args[1] || self
              else
{'fraction_non_alphanumeric': 0.08832853025936599, 'fraction_numerical': 0.001729106628242075, 'mean_word_length': 2.225371747211896, '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': '4595189', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1853, 'n_words': 341}