class TeamMerger
Constants
- Error
- SIMPLE_MODELS
Attributes
Public Class Methods
Source
# File app/lib/team_merger.rb, line 26 def initialize(source_teams:, new_team_attrs:) @source_teams = source_teams @new_team_attrs = new_team_attrs end
Initializes a merger for the given source teams and attributes for the new
team that will replace them.
The source_teams attribute is a list of the teams to be merged.
The new_team_attrs hash is used to create the merged team, and should contain all attributes necessary to create a new team.
Public Instance Methods
Source
# File app/lib/team_merger.rb, line 57 def call! raise Error, @errors.join("; ") unless valid? result = ActiveRecord::Base.transaction do merged_team = Team.create!(new_team_attrs) migrate_simple_tables(merged_team) migrate_batches(merged_team) migrate_archive_reasons(merged_team) migrate_important_notices(merged_team) migrate_subteams(merged_team) migrate_generic_locations(merged_team) migrate_team_locations(merged_team) migrate_teams_users(merged_team) Rails.logger.debug "Migrating patients..." PatientTeamUpdater.call( team_scope: Team.where(id: source_teams.map(&:id)).or( Team.where(id: merged_team.id) ) ) Rails.logger.debug "Destroying old teams..." source_teams.each(&:destroy!) merged_team end refresh_materialized_views result end
Source
# File app/lib/team_merger.rb, line 41 def dry_run @dry_run_report = [] append_migration_counts append_batch_skips append_archive_reason_merges if valid? @dry_run_report << "Merge would succeed." else @dry_run_report << "Merge would ABORT with #{@errors.size} error(s):" @dry_run_report += @errors.map { " ERROR: #{it}" } end @dry_run_report end
Source
# File app/lib/team_merger.rb, line 31 def valid? @errors = [] validate_minimum_teams validate_same_organisation validate_same_type detect_subteam_conflicts detect_team_location_conflicts @errors.empty? end