Class: ProjectUnification::SpecialHandlers::RangedLotCategoryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/project_unification/special_handlers.rb

Overview

Handler for RangedLotCategory with name-based duplicate detection.

Phase 1 (migrate): source categories whose name collides with a target category have their name replaced with a per-record sentinel string so the bulk SQL update can move them without a uniqueness conflict. They are registered in merge_registry for Phase 2.

Phase 2 (run_cleanup): target.unify(sentinel) reroutes ranged_lots (CollectionObjects) from the sentinel to the surviving target category, then destroys the sentinel.

Constant Summary collapse

SENTINEL_PREFIX =
'UNIFICATION TO PROJECT'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_project_id:, target_project_id:, options: {}) ⇒ RangedLotCategoryHandler

Returns a new instance of RangedLotCategoryHandler.



165
166
167
168
# File 'lib/project_unification/special_handlers.rb', line 165

def initialize(source_project_id:, target_project_id:, options: {})
  @source_project_id = source_project_id
  @target_project_id = target_project_id
end

Instance Attribute Details

#source_project_idObject (readonly)

Returns the value of attribute source_project_id.



163
164
165
# File 'lib/project_unification/special_handlers.rb', line 163

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



163
164
165
# File 'lib/project_unification/special_handlers.rb', line 163

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflictsObject (private)



204
205
206
207
208
209
210
211
# File 'lib/project_unification/special_handlers.rb', line 204

def find_conflicts
  target_names = RangedLotCategory.where(project_id: target_project_id).pluck(:name, :id).to_h

  RangedLotCategory.where(project_id: source_project_id).filter_map do |rlc|
    target_id = target_names[rlc.name]
    [rlc, target_id] if target_id
  end
end

#migrateObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/project_unification/special_handlers.rb', line 170

def migrate
  result = {
    track: :special,
    model: 'RangedLotCategory',
    migrated: 0,
    duplicates_found: [],
    errors: [],
    merge_registry: []
  }

  find_conflicts.each do |source_rlc, target_id|
    source_rlc.update_columns(
      name: "#{SENTINEL_PREFIX} #{target_project_id} #{source_rlc.id}"
    )
    result[:duplicates_found] << { source_id: source_rlc.id, target_id: target_id }
    result[:merge_registry] << {
      model: 'RangedLotCategory', renamed_id: source_rlc.id, target_id: target_id
    }
  end

  result[:migrated] = RangedLotCategory.where(project_id: source_project_id)
                                       .update_all(project_id: target_project_id)
  result
rescue => e
  {
    track: :special,
    model: 'RangedLotCategory',
    migrated: 0,
    errors: [{ error: e.message, backtrace: e.backtrace.first(3) }]
  }
end