<commit_before># Monkeypatch clarat_base Email
require ClaratBase::Engine.root.join('app', 'models', 'email')

class Email < ActiveRecord::Base
  # Associations
  has_many :offer_mailings, inverse_of: :email
  has_many :known_offers, source: :offer, through: :offer_mailings,
                          inverse_of: :informed_emails

  # State Machine
  aasm do
    event :inform_offers, guard: :informable_offers? do
      # First check if email needs to be blocked
      transitions from: :uninformed, to: :blocked, guard: :should_be_blocked?
      # Else send email if there are approved offers
      transitions from: :uninformed, to: :informed,
                  after: :send_offer_information
    end

    event :inform_orga, guard: :belongs_to_unique_orga_with_orga_contact? do
      # First check if email needs to be blocked
      transitions from: :uninformed, to: :blocked, guard: :should_be_blocked?
      # Else send email if there are approved offers
      transitions from: :uninformed, to: :informed,
                  after: :send_orga_information
    end
  end

  # Methods
  def not_yet_but_soon_known_offers
    offers.approved.by_mailings_enabled_organization.all - known_offers.all
  end

  def create_offer_mailings offers, mailing_type
    offers.each do |offer|
      OfferMailing.create! offer_id: offer.id, email_id: id,
                           mailing_type: mailing_type
    end
  end

  def send_offer_information
    regenerate_security_code
    OfferMailer.inform(self).deliver
  end

  def send_orga_information
    OrgaMailer.inform(self).deliver
  end

  # required for both offer and orga mailer
  def vague_contact_title?<commit_msg>hotfix: also generate security_code for orga_mails (required to be valid and send mails)<commit_after># Monkeypatch clarat_base Email
require ClaratBase::Engine.root.join('app', 'models', 'email')

class Email < ActiveRecord::Base
  # Associations
  has_many :offer_mailings, inverse_of: :email
  has_many :known_offers, source: :offer, through: :offer_mailings,
                          inverse_of: :informed_emails

  # State Machine
  aasm do
    event :inform_offers, guard: :informable_offers? do
      # First check if email needs to be blocked
      transitions from: :uninformed, to: :blocked, guard: :should_be_blocked?
      # Else send email if there are approved offers
      transitions from: :uninformed, to: :informed,
                  after: :send_offer_information
    end

    event :inform_orga, guard: :belongs_to_unique_orga_with_orga_contact? do
      # First check if email needs to be blocked
      transitions from: :uninformed, to: :blocked, guard: :should_be_blocked?
      # Else send email if there are approved offers
      transitions from: :uninformed, to: :informed,
                  after: :send_orga_information
    end
  end

  # Methods
  def not_yet_but_soon_known_offers
    offers.approved.by_mailings_enabled_organization.all - known_offers.all
  end

  def create_offer_mailings offers, mailing_type
    offers.each do |offer|
      OfferMailing.create! offer_id: offer.id, email_id: id,
                           mailing_type: mailing_type
    end
  end

  def send_offer_information
    regenerate_security_code
    OfferMailer.inform(self).deliver
  end

  def send_orga_information
    regenerate_security_code
    OrgaMailer.inform(self).deliver
  end

  # required for both offer and orga mailer
  def vague_contact_title?
{'fraction_non_alphanumeric': 0.0664924506387921, 'fraction_numerical': 0.0, 'mean_word_length': 3.327889447236181, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6771420', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1043, 'n_words': 294}