Module: StoreAttribute::MutationTracker

Defined in:
lib/store_attribute/active_record/mutation_tracker.rb

Overview

Upgrade mutation tracker to return partial changes for typed stores

Instance Method Summary collapse

Instance Method Details

#change_to_attribute(attr_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/store_attribute/active_record/mutation_tracker.rb', line 6

def change_to_attribute(attr_name)
  return super unless attributes.is_a?(ActiveModel::AttributeSet)
  return super unless attributes[attr_name].type.is_a?(ActiveRecord::Type::TypedStore)

  orig_changes = super

  return unless orig_changes

  prev_store, new_store = orig_changes.map(&:dup)

  prev_store&.each do |key, value|
    if new_store&.dig(key) == value
      prev_store.except!(key)
      new_store&.except!(key)
    end
  end

  [prev_store, new_store]
end