Class: ProjectUnification::SpecialHandlers::ProjectSourceHandler

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

Overview

Handler for ProjectSource with duplicate detection.

ProjectSource is a denormalized index of "sources cited in this project", maintained automatically by Citation#after_create via find_or_create_by. When a source_id already exists in the target project's project_sources, the source record is redundant — delete it and bulk-update the rest. delete_all bypasses the before_destroy :check_for_use callback (which would block deletion while citations still reference the source in the source project).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ProjectSourceHandler.



113
114
115
116
# File 'lib/project_unification/special_handlers.rb', line 113

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.



111
112
113
# File 'lib/project_unification/special_handlers.rb', line 111

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



111
112
113
# File 'lib/project_unification/special_handlers.rb', line 111

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflict_idsObject (private)



144
145
146
147
148
# File 'lib/project_unification/special_handlers.rb', line 144

def find_conflict_ids
  ProjectSource.where(project_id: source_project_id)
               .where(source_id: ProjectSource.where(project_id: target_project_id).select(:source_id))
               .pluck(:id)
end

#migrateObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/project_unification/special_handlers.rb', line 118

def migrate
  conflict_ids = find_conflict_ids

  ProjectSource.where(id: conflict_ids).delete_all if conflict_ids.any?

  migrated = ProjectSource.where(project_id: source_project_id)
                          .update_all(project_id: target_project_id)

  {
    track: :special,
    model: 'ProjectSource',
    migrated: migrated,
    duplicates_deleted: conflict_ids.size,
    errors: []
  }
rescue => e
  {
    track: :special,
    model: 'ProjectSource',
    migrated: 0,
    errors: [{ error: e.message, backtrace: e.backtrace.first(3) }]
  }
end