Skip to content

Commit

Permalink
Refactored merge controller through 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 ad38df9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
38 changes: 2 additions & 36 deletions app/controllers/merge_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,16 @@ 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_attributes = MergeService.get_merged_attributes(@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
39 changes: 39 additions & 0 deletions app/services/merge_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class MergeService
# Returns a new Teacher object with merged attributes but does not save it
def self.get_merged_teacher(from_teacher, into_teacher)
merged_attributes = get_merged_attributes(from_teacher, into_teacher)
Teacher.new(merged_attributes)
end

# Returns the merged attributes hash excluding the "id" attribute
def self.get_merged_attributes(from_teacher, into_teacher)
merged_attributes = {}
into_teacher.attributes.each do |attr_name, attr_value|
next if attr_name == "id"

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"
merged_attributes[attr_name] = [attr_value, from_teacher_attr_value].max
when "created_at"
merged_attributes[attr_name] = [attr_value, from_teacher_attr_value].min
else
merged_attributes[attr_name] = attr_value
end
end
end

merged_attributes
end
end

0 comments on commit ad38df9

Please sign in to comment.