<commit_before>module Spree::Chimpy
  class Subscription
    delegate :configured?, :enqueue, to: Spree::Chimpy

    def initialize(user)
      @user      = user
    end

    def subscribe(source = nil)
      chimpy_action = Spree::Chimpy::Action.new(email: @user.email, source: source, action: :subscribe)
      if @user.valid? && !@user.subscribed? && chimpy_action.save
        @user.update_column(:subscribed, true)
        defer(:subscribe)
        true
      else
        false
      end
    end

    def update_member_info
      if @user.subscribed?
        defer(:subscribe)
      end
    end

    def unsubscribe(source = nil)
      chimpy_action = Spree::Chimpy::Action.new(email: @user.email, source: source, action: :unsubscribe)
      if @user.subscribed? && chimpy_action.save
        @user.update_column(:subscribed, false)
        defer(:unsubscribe) 
        true
      else
        false
      end
    end

    def resubscribe(&block)
      block.call if block

      return unless configured?

      if unsubscribing?
        defer(:unsubscribe) 
      elsif @user.subscribed?
        defer(:subscribe)
      end
    end

  private
    def defer(event)
      enqueue(event, @user)
    end

    def subscribing?<commit_msg>Adding an action for resubscribe<commit_after>module Spree::Chimpy
  class Subscription
    delegate :configured?, :enqueue, to: Spree::Chimpy

    def initialize(user)
      @user      = user
    end

    def subscribe(source = nil)
      chimpy_action = Spree::Chimpy::Action.new(email: @user.email, source: source, action: :subscribe)
      if @user.valid? && !@user.subscribed? && chimpy_action.save
        @user.update_column(:subscribed, true)
        defer(:subscribe)
        true
      else
        false
      end
    end

    def update_member_info
      if @user.subscribed?
        defer(:subscribe)
      end
    end

    def unsubscribe(source = nil)
      chimpy_action = Spree::Chimpy::Action.new(email: @user.email, source: source, action: :unsubscribe)
      if @user.subscribed? && chimpy_action.save
        @user.update_column(:subscribed, false)
        defer(:unsubscribe) 
        true
      else
        false
      end
    end

    def resubscribe(&block)
      block.call if block

      return unless configured?

      if unsubscribing?
        chimpy_action = Spree::Chimpy::Action.create(email: @user.email, source: 'Website - Manual subscribe', action: :unsubscribe)
        defer(:unsubscribe) 
      elsif @user.subscribed?
        chimpy_action = Spree::Chimpy::Action.create(email: @user.email, source: 'Website - Manual unsubscribe', action: :subscribe)
        defer(:subscribe)
      end
    end

  private
    def defer(event)
      enqueue(event, @user)
    end

    def subscribing?
{'fraction_non_alphanumeric': 0.09974701843151428, 'fraction_numerical': 0.0, 'mean_word_length': 2.4643304130162704, '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': '7198965', 'n_tokens_mistral': 871, 'n_tokens_neox': 832, 'n_words': 197}