Sha256: 34eebe3811bbf231915d65266054e0baebd1126adaff50ff37f3322e017a4099

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 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)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pay-2.0.3 lib/pay/billable/sync_email.rb
pay-2.0.2 lib/pay/billable/sync_email.rb
pay-2.0.1 lib/pay/billable/sync_email.rb
pay-2.0.0 lib/pay/billable/sync_email.rb