Sha256: 61f1e56bb15bf023b5b7ee0b62b33944b7d63a0bc7e870a41341b8a5f1d1c9e4

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

module Pay
  module Billable
    module SyncEmail
      # Sync email address changes from the model to the processor.
      # This way they're kept in sync and email notifications are
      # always sent to the correct email address after an update.
      #
      # Processor classes simply need to implement a method named:
      #
      # update_PROCESSOR_email!
      #
      # This method should take the email address on the billable
      # object and update the associated API record.

      extend ActiveSupport::Concern

      included do
        after_update :enqeue_sync_email_job, if: :should_sync_email_with_processor?
      end

      def should_sync_email_with_processor?
        try(:saved_change_to_email?)
      end

      def sync_email_with_processor
        send("update_#{processor}_email!")
      end

      private

      def enqeue_sync_email_job
        # Only update if the processor id is the same
        # This prevents duplicate API hits if this is their first time
        if processor_id? && !saved_change_to_processor_id? && saved_change_to_email?
          EmailSyncJob.perform_later(id, self.class.name)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
pay-2.5.0 lib/pay/billable/sync_email.rb
pay-2.4.4 lib/pay/billable/sync_email.rb
pay-2.4.3 lib/pay/billable/sync_email.rb
pay-2.4.2 lib/pay/billable/sync_email.rb
pay-2.4.0 lib/pay/billable/sync_email.rb
pay-2.3.1 lib/pay/billable/sync_email.rb
pay-2.3.0 lib/pay/billable/sync_email.rb
pay-2.2.2 lib/pay/billable/sync_email.rb
pay-2.2.1 lib/pay/billable/sync_email.rb
pay-2.2.0 lib/pay/billable/sync_email.rb
pay-2.1.3 lib/pay/billable/sync_email.rb
pay-2.1.2 lib/pay/billable/sync_email.rb
pay-2.1.1 lib/pay/billable/sync_email.rb
pay-2.1.0 lib/pay/billable/sync_email.rb