Class: ActiveRecordMerger::RecordMerger
- Inherits:
-
Object
- Object
- ActiveRecordMerger::RecordMerger
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#primary_record ⇒ Object
readonly
Returns the value of attribute primary_record.
-
#secondary_record ⇒ Object
readonly
Returns the value of attribute secondary_record.
-
#update_counts ⇒ Object
readonly
Returns the value of attribute update_counts.
Instance Method Summary collapse
-
#call ⇒ Hash
Executes the merge operation.
-
#initialize(first, second, **options) ⇒ RecordMerger
constructor
Initializes the RecordMerger service object.
Constructor Details
#initialize(first, second, **options) ⇒ RecordMerger
Initializes the RecordMerger service object.
27 28 29 30 31 32 |
# File 'lib/active_record_merger/record_merger.rb', line 27 def initialize(first, second, **) @_first = first @_second = second @options = .merge() @update_counts = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/active_record_merger/record_merger.rb', line 14 def @options end |
#primary_record ⇒ Object (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_record ⇒ Object (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_counts ⇒ Object (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
#call ⇒ Hash
Executes the merge operation. Returns a hash containing the counts of updated records for each association, and the destruction status of the merged record.
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 [:destroy_merged_record] end @update_counts rescue => e errors.add(:base, "Failed to merge records: #{e.}") nil end |