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.



212
213
214
215
# File 'lib/project_unification/special_handlers.rb', line 212

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.



210
211
212
# File 'lib/project_unification/special_handlers.rb', line 210

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



210
211
212
# File 'lib/project_unification/special_handlers.rb', line 210

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflictsObject (private)



251
252
253
254
255
256
257
258
# File 'lib/project_unification/special_handlers.rb', line 251

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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/project_unification/special_handlers.rb', line 217

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