<commit_before># frozen_string_literal: true

module Folio::HasNewsletterSubscription
  extend ActiveSupport::Concern

  included do
    has_one :newsletter_subscription, class_name: "Folio::NewsletterSubscription",
                                      as: :subscribable,
                                      dependent: :destroy

    after_save :update_newsletter_subscription
  end

  def requires_subscription_confirmation?
    true
  end

  def update_newsletter_subscription
    if newsletter_subscription.present? || should_subscribe_to_newsletter?
      build_newsletter_subscription if newsletter_subscription.nil?

      newsletter_subscription.update!(email: subscription_email,
                                      merge_vars: subscription_merge_vars,
                                      tags: subscription_tags,
                                      active: should_subscribe_to_newsletter?)
    end
  end

  private
    def subscription_email
      email
    end<commit_msg>don't fail user actions due to invalid newsletter subscription<commit_after># frozen_string_literal: true

module Folio::HasNewsletterSubscription
  extend ActiveSupport::Concern

  included do
    has_one :newsletter_subscription, class_name: "Folio::NewsletterSubscription",
                                      as: :subscribable,
                                      dependent: :destroy

    after_save :update_newsletter_subscription
  end

  def requires_subscription_confirmation?
    true
  end

  def update_newsletter_subscription
    if newsletter_subscription.present? || should_subscribe_to_newsletter?
      build_newsletter_subscription if newsletter_subscription.nil?

      did_update = newsletter_subscription.update(email: subscription_email,
                                                  merge_vars: subscription_merge_vars,
                                                  tags: subscription_tags,
                                                  active: should_subscribe_to_newsletter?)
      unless did_update
        Raven.capture_message("NewsletterSubscription for #{self.class.model_name.human} ##{id} - email \"#{subscription_email}\" - failed to update.") if defined?(Raven)
      end
    end
  end

  private
    def subscription_email
      email
    end
{'fraction_non_alphanumeric': 0.050394390885188434, 'fraction_numerical': 0.0, 'mean_word_length': 2.215492957746479, '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': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5104016', 'n_tokens_mistral': 542, 'n_tokens_neox': 537, 'n_words': 116}