Class: ProjectUnification::SpecialHandlers::ProjectSourceHandler
- Inherits:
-
Object
- Object
- ProjectUnification::SpecialHandlers::ProjectSourceHandler
- 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
-
#source_project_id ⇒ Object
readonly
Returns the value of attribute source_project_id.
-
#target_project_id ⇒ Object
readonly
Returns the value of attribute target_project_id.
Instance Method Summary collapse
- #find_conflict_ids ⇒ Object private
-
#initialize(source_project_id:, target_project_id:, options: {}) ⇒ ProjectSourceHandler
constructor
A new instance of ProjectSourceHandler.
- #migrate ⇒ Object
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_id ⇒ Object (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_id ⇒ Object (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_ids ⇒ Object (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 |
#migrate ⇒ Object
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., backtrace: e.backtrace.first(3) }] } end |