Created
March 4, 2009 20:53
-
-
Save rsl/73998 to your computer and use it in GitHub Desktop.
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
class User < ActiveRecord::Base | |
def merge(other) | |
%w{ | |
attribute_1 | |
attribute_2 | |
attribute_3 | |
attribute_4 | |
attribute_5 | |
attribute_6 | |
}.each do |attribute| | |
self.write_attribute(attribute, oldest(attribute, other)) | |
end | |
%w{ | |
attribute_7 | |
attribute_8 | |
attribute_9 | |
}.each do |attribute| | |
self.write_attribute(attribute, first_non_blank(attribute, other)) | |
end | |
# Unrelated code here ... | |
end | |
private | |
def oldest(attribute, other) | |
oldest ||= created_at < other.created_at ? self : other | |
oldest.send(attribute) | |
end | |
def first_non_blank(attribute, other) | |
value = self.send(attribute) | |
value.blank? ? other.send(attribute) : value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment