Class: SpreeCmCommissioner::Integrations::Base::SyncResult::RecordTracker

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/base/sync_result.rb

Overview

Helper class for explicit record tracking and saving

Instance Method Summary collapse

Constructor Details

#initialize(sync_result, type, record) ⇒ RecordTracker



97
98
99
100
101
102
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 97

def initialize(sync_result, type, record)
  @sync_result = sync_result
  @type = type
  @record = record
  @was_new = record.new_record?
end

Instance Method Details

#save_if_changed!(record = @record) ⇒ Object

Save record if it's new or has changes, and track the operation Automatically detects create vs update based on initial state



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 108

def save_if_changed!(record = @record)
  return unless record.new_record? || record.changed? || default_price_changed?(record)

  record.save!

  # Track based on initial state
  if @was_new
    @sync_result.increment_metric(@type, :created)
  else
    @sync_result.increment_metric(@type, :updated)
  end
end