-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated merge_controller to use merge_service
- Loading branch information
Showing
2 changed files
with
35 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |