<commit_before>class User::SubscriptionPreferencesForm
  include ActiveModel::Model

  attr_accessor(
    :user,
    :site,
    :notification_frequency,
    :modules,
    :gobierto_people_people,
    :gobierto_budget_consultations_consultations,
    :site_to_subscribe
  )

  validates :user, :site, :notification_frequency, presence: true

  def save
    save_subscriptions if valid?
  end

  private

  def save_subscriptions
    @user = user.tap do |user_attributes|
      user_attributes.notification_frequency = notification_frequency
    end

    if @user.valid?
      @user.save

      update_subscriptions_to_modules(modules)
      update_subscriptions_to_people(gobierto_people_people)
      update_subscription_to_site(site_to_subscribe)
      update_subscriptions_to_consultations(gobierto_budget_consultations_consultations)

     @user
    else
      promote_errors(@user.errors)

      false
    end
  end

  def promote_errors(errors_hash)
    errors_hash.each do |attribute, message|
      errors.add(attribute, message)
    end
  end

  def update_subscriptions_to_modules(modules)
    modules.each do |module_name|
      next if module_name.blank?

      gobierto_module = module_name.classify.pluralize.constantize
      @user.subscribe_to!(gobierto_module, site)
    end

    (site.configuration.modules.map(&:underscore) - modules).each do |module_name|
      next if module_name.blank?

      gobierto_module = module_name.classify.pluralize.constantize
      @user.unsubscribe_from!(gobierto_module, site)
    end
  end

  def update_subscriptions_to_people(people)
    people.each do |person_id|
      next if person_id.blank?

      person = GobiertoPeople::Person.find(person_id)
      @user.subscribe_to!(person, site)
    end

    (site.people.active.pluck(:id).map(&:to_s) - people).each do |person_id|
      next if person_id.blank?

      person = site.people.find(person_id)
      @user.unsubscribe_from!(person, site)
    end
  end

  def update_subscription_to_site(site_to_subscribe_id)
    if site_to_subscribe_id != "0"
      @user.subscribe_to!(site, site)
    else
      @user.unsubscribe_from!(site, site)
    end
  end

  def update_subscriptions_to_consultations(budget_consultations)
    budget_consultations.each do |consultation_id|
      next if consultation_id.blank?
<commit_msg>Ensure preferences exist<commit_after>class User::SubscriptionPreferencesForm
  include ActiveModel::Model

  attr_accessor(
    :user,
    :site,
    :notification_frequency,
    :modules,
    :gobierto_people_people,
    :gobierto_budget_consultations_consultations,
    :site_to_subscribe
  )

  validates :user, :site, :notification_frequency, presence: true

  def save
    save_subscriptions if valid?
  end

  private

  def save_subscriptions
    @user = user.tap do |user_attributes|
      user_attributes.notification_frequency = notification_frequency
    end

    if @user.valid?
      @user.save

      update_subscriptions_to_modules(modules)
      update_subscriptions_to_people(gobierto_people_people)
      update_subscription_to_site(site_to_subscribe)
      update_subscriptions_to_consultations(gobierto_budget_consultations_consultations)

     @user
    else
      promote_errors(@user.errors)

      false
    end
  end

  def promote_errors(errors_hash)
    errors_hash.each do |attribute, message|
      errors.add(attribute, message)
    end
  end

  def update_subscriptions_to_modules(modules)
    modules = Array(modules)
    modules.each do |module_name|
      next if module_name.blank?

      gobierto_module = module_name.classify.pluralize.constantize
      @user.subscribe_to!(gobierto_module, site)
    end

    (site.configuration.modules.map(&:underscore) - modules).each do |module_name|
      next if module_name.blank?

      gobierto_module = module_name.classify.pluralize.constantize
      @user.unsubscribe_from!(gobierto_module, site)
    end
  end

  def update_subscriptions_to_people(people)
    people = Array(people)

    people.each do |person_id|
      next if person_id.blank?

      person = GobiertoPeople::Person.find(person_id)
      @user.subscribe_to!(person, site)
    end

    (site.people.active.pluck(:id).map(&:to_s) - people).each do |person_id|
      next if person_id.blank?

      person = site.people.find(person_id)
      @user.unsubscribe_from!(person, site)
    end
  end

  def update_subscription_to_site(site_to_subscribe_id)
    if site_to_subscribe_id != "0"
      @user.subscribe_to!(site, site)
    else
      @user.unsubscribe_from!(site, site)
    end
  end

  def update_subscriptions_to_consultations(budget_consultations)
    budget_consultations = Array(budget_consultations)

    budget_consultations.each do |consultation_id|
      next if consultation_id.blank?

{'fraction_non_alphanumeric': 0.07764755127668481, 'fraction_numerical': 0.0004185851820845542, 'mean_word_length': 3.722332015810277, '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': '5928523', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1522, 'n_words': 266}