Class: ActiveRecordMerger::RecordMerger

Inherits:
Object
  • Object
show all
Includes:
SimpleCommand
Defined in:
lib/active_record_merger/record_merger.rb

Overview

This class performs the merging of two ActiveRecord objects of the same type. It updates associated records to reflect the merge and can optionally destroy the merged record.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second, **options) ⇒ RecordMerger

Initializes the RecordMerger service object.

Parameters:

  • first (ActiveRecord::Base)

    The record to merge into.

  • second (ActiveRecord::Base)

    The record to merge from.

  • options (Hash)

    The options to customize the merge process.

Options Hash (**options):

  • :primary_record_resolver (Proc) — default: nil

    A lambda or Proc that determines which record is considered primary.

  • :merge_logic (Proc) — default: nil

    A lambda or Proc that defines how to merge data from the secondary record into the primary record.

  • :destroy_merged_record (Boolean) — default: false

    Whether to destroy the merged (secondary) record after the merge.

  • :filter (Proc) — default: ->(assoc) { true }

    A lambda or Proc that filters which associations should be updated.

  • :update_logic (Proc) — default: nil

    A lambda or Proc that provides custom logic for updating associations.



27
28
29
30
31
32
# File 'lib/active_record_merger/record_merger.rb', line 27

def initialize(first, second, **options)
  @_first        = first
  @_second       = second
  @options       = default_options.merge(options)
  @update_counts = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/active_record_merger/record_merger.rb', line 14

def options
  @options
end

#primary_recordObject (readonly)

Returns the value of attribute primary_record.



14
15
16
# File 'lib/active_record_merger/record_merger.rb', line 14

def primary_record
  @primary_record
end

#secondary_recordObject (readonly)

Returns the value of attribute secondary_record.



14
15
16
# File 'lib/active_record_merger/record_merger.rb', line 14

def secondary_record
  @secondary_record
end

#update_countsObject (readonly)

Returns the value of attribute update_counts.



14
15
16
# File 'lib/active_record_merger/record_merger.rb', line 14

def update_counts
  @update_counts
end

Instance Method Details

#callHash

Executes the merge operation. Returns a hash containing the counts of updated records for each association, and the destruction status of the merged record.

Returns:

  • (Hash)

    The result of merge operation with update counts and destruction status.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_record_merger/record_merger.rb', line 39

def call
  ::ActiveRecord::Base.transaction do
    ensure_same_class
    resolve_primary_and_secondary
    apply_merge_logic
    update_associations
    destroy_secondary_record if options[:destroy_merged_record]
  end
  @update_counts
rescue => e
  errors.add(:base, "Failed to merge records: #{e.message}")
  nil
end