Skip to content

Commit

Permalink
Updated merge_controller to use merge_service
Browse files Browse the repository at this point in the history
  • Loading branch information
razztech committed Apr 19, 2024
1 parent 7fb905a commit 70e98f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
37 changes: 2 additions & 35 deletions app/controllers/merge_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,17 @@ class MergeController < ApplicationController
def preview
@from_teacher = Teacher.find(params[:from])
@into_teacher = Teacher.find(params[:into])
@result_teacher = merge_teachers(@from_teacher, @into_teacher)
@result_teacher = MergeService.get_merged_teacher(@from_teacher, @into_teacher)
end

def execute
@from_teacher = Teacher.find(params[:from])
@into_teacher = Teacher.find(params[:into])
@merged_teacher = merge_teachers(@from_teacher, @into_teacher)
@merged_teacher = MergeService.get_merged_teacher(@from_teacher, @into_teacher)

merged_attributes = @merged_teacher.attributes.except("id")
@into_teacher.update!(merged_attributes)
@from_teacher.destroy
redirect_to teachers_path, notice: "Teachers merged successfully."
end

private
# Returns a merged teacher without saving it to the database.
# Rendered in the preview, and only saved to the DB in a call to merge.
def merge_teachers(from_teacher, into_teacher)
merged_attributes = {}
into_teacher.attributes.each do |attr_name, attr_value|
from_teacher_attr_value = from_teacher.attributes[attr_name]
if attr_value.blank?
merged_attributes[attr_name] = from_teacher_attr_value
elsif from_teacher_attr_value.blank?
merged_attributes[attr_name] = attr_value
else
case attr_name
when "session_count"
merged_attributes[attr_name] = attr_value + from_teacher_attr_value
when "ip_history"
merged_attributes[attr_name] = (attr_value + from_teacher_attr_value).uniq
when "last_session_at"
# The resulting last session time is the most recent one
merged_attributes[attr_name] = attr_value > from_teacher_attr_value ? attr_value : from_teacher_attr_value
when "created_at"
# The resulting record creation time is the least recent one
merged_attributes[attr_name] = attr_value < from_teacher_attr_value ? attr_value : from_teacher_attr_value
else
merged_attributes[attr_name] = attr_value
end
end
end

merged_teacher = Teacher.new(merged_attributes)
merged_teacher
end
end
33 changes: 33 additions & 0 deletions app/services/merge_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module MergeService
def self.get_merged_teacher(from_teacher, into_teacher)
merged_attributes = {}
into_teacher.attributes.each do |attr_name, attr_value|
from_teacher_attr_value = from_teacher.attributes[attr_name]
if attr_value.blank?
merged_attributes[attr_name] = from_teacher_attr_value
elsif from_teacher_attr_value.blank?
merged_attributes[attr_name] = attr_value
else
case attr_name
when "session_count"
merged_attributes[attr_name] = attr_value + from_teacher_attr_value
when "ip_history"
merged_attributes[attr_name] = (attr_value + from_teacher_attr_value).uniq
when "last_session_at"
# The resulting last session time is the most recent one
merged_attributes[attr_name] = attr_value > from_teacher_attr_value ? attr_value : from_teacher_attr_value
when "created_at"
# The resulting record creation time is the least recent one
merged_attributes[attr_name] = attr_value < from_teacher_attr_value ? attr_value : from_teacher_attr_value
else
merged_attributes[attr_name] = attr_value
end
end
end

merged_teacher = Teacher.new(merged_attributes)
merged_teacher
end
end

0 comments on commit 70e98f5

Please sign in to comment.